Skip to content

Commit

Permalink
Refactor code to prep for paths (#43)
Browse files Browse the repository at this point in the history
* Clarify main inputs.

* Import external functions.

* Refactor existing code.
  • Loading branch information
jonthegeek authored Mar 27, 2024
1 parent abc9b51 commit 44eaddd
Show file tree
Hide file tree
Showing 27 changed files with 391 additions and 291 deletions.
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Description: Automatically generate R package skeletons from 'application
License: MIT + file LICENSE
URL: https://beekeeper.api2r.org, https://github.com/jonthegeek/beekeeper
BugReports: https://github.com/jonthegeek/beekeeper/issues
Depends:
R (>= 3.5.0)
Imports:
cli,
desc,
Expand All @@ -27,6 +29,7 @@ Imports:
rprojroot,
S7,
snakecase,
stringr,
styler,
testthat,
usethis,
Expand All @@ -36,7 +39,6 @@ Suggests:
covr,
knitr,
rmarkdown,
stringr,
withr
VignetteBuilder:
knitr
Expand All @@ -46,4 +48,4 @@ Remotes:
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
35 changes: 35 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,46 @@ export(generate_pkg)
export(generate_pkg_agent)
export(use_beekeeper)
if (getRversion() < "4.3.0") importFrom("S7", "@")
importFrom(S7,class_any)
importFrom(cli,cli_abort)
importFrom(cli,cli_warn)
importFrom(desc,desc)
importFrom(fs,is_dir)
importFrom(fs,path)
importFrom(fs,path_dir)
importFrom(fs,path_file)
importFrom(fs,path_rel)
importFrom(glue,glue)
importFrom(glue,glue_collapse)
importFrom(httptest2,use_httptest2)
importFrom(lubridate,now)
importFrom(lubridate,parse_date_time)
importFrom(nectar,call_api)
importFrom(nectar,stabilize_string)
importFrom(purrr,map)
importFrom(purrr,map2)
importFrom(purrr,map_chr)
importFrom(purrr,pmap)
importFrom(purrr,quietly)
importFrom(rapid,as_rapid)
importFrom(rapid,class_api_key_security_scheme)
importFrom(rapid,class_security_scheme_details)
importFrom(rapid,class_security_schemes)
importFrom(rlang,.data)
importFrom(rlang,set_names)
importFrom(rlang,try_fetch)
importFrom(rprojroot,find_package_root_file)
importFrom(snakecase,to_snake_case)
importFrom(stringr,str_remove)
importFrom(styler,style_file)
importFrom(testthat,test_that)
importFrom(usethis,proj_get)
importFrom(usethis,proj_path)
importFrom(usethis,use_build_ignore)
importFrom(usethis,use_directory)
importFrom(usethis,use_package)
importFrom(usethis,use_template)
importFrom(usethis,use_testthat)
importFrom(utils,capture.output)
importFrom(yaml,read_yaml)
importFrom(yaml,write_yaml)
59 changes: 28 additions & 31 deletions R/as_bk_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ as_bk_data <- S7::new_generic(
dispatch_args = "x"
)

S7::method(as_bk_data, rapid::class_security_schemes) <- function(x) {
S7::method(as_bk_data, class_security_schemes) <- function(x) {
if (!length(x)) {
return(list())
}
Expand All @@ -22,7 +22,7 @@ S7::method(as_bk_data, rapid::class_security_schemes) <- function(x) {
}

.security_schemes_collect <- function(x) {
purrr::pmap(
pmap(
list(
x@name,
x@details,
Expand All @@ -35,7 +35,7 @@ S7::method(as_bk_data, rapid::class_security_schemes) <- function(x) {
.security_scheme_rotate <- function(name, details, description) {
security_scheme <- c(
list(
name = snakecase::to_snake_case(name),
name = to_snake_case(name),
description = description
),
as_bk_data(details)
Expand All @@ -51,21 +51,19 @@ S7::method(as_bk_data, rapid::class_security_schemes) <- function(x) {
if (is.na(description)) {
return(
switch(type,
api_key = .security_scheme_description_api_key(),
api_key = .security_scheme_description_api_key,
NA_character_
)
)
}
return(description) # nocov
}

.security_scheme_description_api_key <- function() {
paste(
"An API key provided by the API provider.",
"This key is not clearly documented in the API description.",
"Check the API documentation for details."
)
}
.security_scheme_description_api_key <- paste(
"An API key provided by the API provider.",
"This key is not clearly documented in the API description.",
"Check the API documentation for details."
)

.security_scheme_collection_finalize <- function(security_schemes) {
security_scheme_data <- c(
Expand All @@ -79,49 +77,48 @@ S7::method(as_bk_data, rapid::class_security_schemes) <- function(x) {
}

.security_args_compile <- function(security_schemes) {
security_args <- sort(unique(purrr::map_chr(security_schemes, "arg_name")))
security_args <- sort(unique(map_chr(security_schemes, "arg_name")))
return(list(
security_arg_names = security_args,
security_arg_list = .collapse_comma(
glue::glue("{security_args} = {security_args}")
glue("{security_args} = {security_args}")
),
security_arg_helps = .security_arg_help_generate(
security_arg_helps = .generate_security_arg_help(
security_schemes,
security_args
)
))
}

.security_arg_help_generate <- function(security_schemes, security_args) {
security_arg_description <- rlang::set_names(
purrr::map_chr(security_schemes, "description"),
purrr::map_chr(security_schemes, "arg_name")
.generate_security_arg_help <- function(security_schemes, security_args) {
security_arg_description <- set_names(
map_chr(security_schemes, "description"),
map_chr(security_schemes, "arg_name")
)
security_arg_description <- unname(security_arg_description[security_args])
return(
purrr::map2(
map2(
security_arg_description,
security_args,
function(arg_description, arg_name) {
list(name = arg_name, description = arg_description)
}
.security_arg_description_clean
)
)
}

S7::method(as_bk_data, rapid::class_security_scheme_details) <- function(x) {
purrr::map(x, as_bk_data)
.security_arg_description_clean <- function(arg_description, arg_name) {
list(name = arg_name, description = arg_description)
}

S7::method(as_bk_data, class_security_scheme_details) <- function(x) {
map(x, as_bk_data)
}

S7::method(as_bk_data, rapid::class_api_key_security_scheme) <- function(x) {
S7::method(as_bk_data, class_api_key_security_scheme) <- function(x) {
if (length(x)) {
return(
list(
parameter_name = x@parameter_name,
arg_name = stringr::str_remove(
snakecase::to_snake_case(x@parameter_name),
"^x_"
),
arg_name = str_remove(to_snake_case(x@parameter_name), "^x_"),
location = x@location,
type = "api_key",
api_key = TRUE
Expand All @@ -131,8 +128,8 @@ S7::method(as_bk_data, rapid::class_api_key_security_scheme) <- function(x) {
return(list())
}

S7::method(as_bk_data, S7::class_any) <- function(x) {
cli::cli_warn(
S7::method(as_bk_data, class_any) <- function(x) {
cli_warn(
"No method for as_bk_data() for class {.cls {class(x)}}."
)
return(list())
Expand Down
36 changes: 36 additions & 0 deletions R/beekeeper-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,48 @@
"_PACKAGE"

## usethis namespace: start
#' @importFrom cli cli_abort
#' @importFrom cli cli_warn
#' @importFrom desc desc
#' @importFrom fs is_dir
#' @importFrom fs path
#' @importFrom fs path_dir
#' @importFrom fs path_file
#' @importFrom fs path_rel
#' @importFrom glue glue
#' @importFrom glue glue_collapse
#' @importFrom httptest2 use_httptest2
#' @importFrom lubridate now
#' @importFrom lubridate parse_date_time
#' @importFrom nectar call_api
#' @importFrom nectar stabilize_string
#' @importFrom purrr map
#' @importFrom purrr map_chr
#' @importFrom purrr map2
#' @importFrom purrr pmap
#' @importFrom purrr quietly
#' @importFrom rapid as_rapid
#' @importFrom rapid class_api_key_security_scheme
#' @importFrom rapid class_security_scheme_details
#' @importFrom rapid class_security_schemes
#' @importFrom rlang .data
#' @importFrom rlang set_names
#' @importFrom rlang try_fetch
#' @importFrom rprojroot find_package_root_file
#' @importFrom S7 class_any
#' @importFrom snakecase to_snake_case
#' @importFrom stringr str_remove
#' @importFrom styler style_file
#' @importFrom testthat test_that
#' @importFrom usethis proj_get
#' @importFrom usethis proj_path
#' @importFrom usethis use_build_ignore
#' @importFrom usethis use_directory
#' @importFrom usethis use_package
#' @importFrom usethis use_template
#' @importFrom usethis use_testthat
#' @importFrom utils capture.output
#' @importFrom yaml read_yaml
#' @importFrom yaml write_yaml
## usethis namespace: end
NULL
92 changes: 0 additions & 92 deletions R/generate.R

This file was deleted.

19 changes: 12 additions & 7 deletions R/generate_pkg_agent.R → R/generate_pkg-agent.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,24 @@
#'
#' @export
generate_pkg_agent <- function(path = ".") {
if (!fs::is_dir(path) && fs::path_file(path) != "DESCRIPTION") {
path <- fs::path_dir(path) # nocov
if (!is_dir(path) && path_file(path) != "DESCRIPTION") {
path <- path_dir(path) # nocov
}
pkg_desc <- desc::desc(file = path)
pkg_desc <- desc(file = path)
pkg_name <- pkg_desc$get_field("Package")
pkg_url <- .get_pkg_url(pkg_desc)
return(
glue("{pkg_name}{pkg_url}")
)
}

.get_pkg_url <- function(pkg_desc) {
pkg_url_glue <- ""
pkg_url <- pkg_desc$get_urls()
if (length(pkg_url)) {
pkg_url_glue <- glue::glue(
pkg_url_glue <- glue(
" ({pkg_url[[1]]})"
)
}
return(
glue::glue("{pkg_name}{pkg_url_glue}")
)
return(pkg_url_glue)
}
Loading

0 comments on commit 44eaddd

Please sign in to comment.