diff --git a/.Rbuildignore b/.Rbuildignore index 286e081..833658c 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -3,7 +3,7 @@ ^LICENSE\.md$ ^\.github$ ^README\.Rmd$ -^man/universe_query\.Rd$ ^_pkgdown\.yml$ ^docs$ ^pkgdown$ +^man/universe_query\.Rd$ diff --git a/DESCRIPTION b/DESCRIPTION index b428730..60b385a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -13,7 +13,8 @@ Depends: R (>= 4.2.0) Imports: cli, - httr2 + httr2, + rlang URL: https://docs.ropensci.org/starchart/, https://github.com/ropenscilabs/starchart BugReports: https://github.com/ropenscilabs/starchart/issues Config/Needs/build: moodymudskipper/devtag diff --git a/R/global-search.R b/R/global-search.R index 3c44a2f..4c546a3 100644 --- a/R/global-search.R +++ b/R/global-search.R @@ -10,6 +10,8 @@ #' global_search(query = '"weather data"', limit = 1) #' global_search(query = 'needs:httr2', limit = 1) global_search <- function(query, limit = 100) { + assert_character(query, "query") + httr2::request("https://r-universe.dev") |> httr2::req_url_path("api") |> httr2::req_url_path_append("search") |> diff --git a/R/universe.R b/R/universe.R index a9344e5..a13dad4 100644 --- a/R/universe.R +++ b/R/universe.R @@ -10,9 +10,7 @@ #' universe_ls("ropensci") #' @family universe universe_ls <- function(universe) { - if (!is.character(universe) || length(universe) != 1) { - cli::cli_abort("{.arg universe} must be a character of length 1.") - } + assert_character(universe, "universe") # TODO assert that universe is an universe universe_query( @@ -35,9 +33,7 @@ universe_ls <- function(universe) { #' universe_packages("ropensci") #' @family universe universe_packages <- function(universe, limit = 100) { - if (!is.character(universe) || length(universe) != 1) { - cli::cli_abort("{.arg universe} must be a character of length 1.") - } + assert_character(universe, "universe") # TODO assert that universe is an universe universe_query( @@ -60,9 +56,8 @@ universe_packages <- function(universe, limit = 100) { #' universe_package("jeroen", package = "curl") #' @family universe universe_package <- function(universe, package) { - if (!is.character(universe) || length(universe) != 1) { - cli::cli_abort("{.arg universe} must be a character of length 1.") - } + assert_character(universe, "universe") + assert_character(package, "package") # TODO assert that universe is an universe if (!package %in% universe_ls(universe)) { @@ -90,12 +85,8 @@ universe_package <- function(universe, package) { #' universe_search("ropensci", query = 'needs:httr2') #' @family universe universe_search <- function(universe, query, limit) { - if (!is.character(universe) || length(universe) != 1) { - cli::cli_abort("{.arg universe} must be a character of length 1.") - } - if (!is.character(query) || length(query) != 1) { - cli::cli_abort("{.arg query} must be a character of length 1.") - } + assert_character(universe, "universe") + assert_character(query, "query") # TODO assert that universe is an universe universe_query( diff --git a/R/utils.R b/R/utils.R index 97c08bc..d6b90f1 100644 --- a/R/utils.R +++ b/R/utils.R @@ -20,3 +20,9 @@ universe_query <- function(universe_url, path, query_params = NULL) { httr2::req_perform(request) |> httr2::resp_body_json() } + +assert_character <- function(x, name, call = rlang::caller_env()) { + if (!is.character(x) || length(x) != 1) { + cli::cli_abort("{.arg name} must be a character of length 1.", call = call) + } +} diff --git a/tests/testthat/_snaps/utils.md b/tests/testthat/_snaps/utils.md new file mode 100644 index 0000000..0acbe0d --- /dev/null +++ b/tests/testthat/_snaps/utils.md @@ -0,0 +1,13 @@ +# assert_character() works + + Code + assert_character(1, "universe") + Condition + Error: + ! `name` must be a character of length 1. + Code + assert_character(c("bla", "blop"), "universe") + Condition + Error: + ! `name` must be a character of length 1. + diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R new file mode 100644 index 0000000..54435b5 --- /dev/null +++ b/tests/testthat/test-utils.R @@ -0,0 +1,6 @@ +test_that("assert_character() works", { + expect_snapshot(error = TRUE, { + assert_character(1, "universe") + assert_character(c("bla", "blop"), "universe") + }) +})