Skip to content

Commit

Permalink
Implement stabilize_string().
Browse files Browse the repository at this point in the history
Closes #13.
  • Loading branch information
jonthegeek committed Oct 26, 2023
1 parent 46640b3 commit 6b6c10b
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 4 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Imports:
httr2 (>= 0.2.3.9000),
jsonlite,
purrr,
rlang
rlang,
stbl
Suggests:
covr,
testthat (>= 3.0.0)
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ S3method(.add_body,multipart)
export(call_api)
export(compact_nested_list)
export(security_api_key)
export(stabilize_string)
importFrom(fs,path)
importFrom(rlang,":=")
3 changes: 0 additions & 3 deletions R/security.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# @param ... Additional arguments, which depend on the location of the API key.


#' Authenticate with an API key
#'
#' Many APIs provide API keys that can be used to authenticate requests (or,
Expand Down
42 changes: 42 additions & 0 deletions R/stabilize.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#' Ensure an argument is a length-1 character
#'
#' Calls to APIs often require a string argument. This function ensures that
#' those arguments are length-1, non-`NA` character vectors, or length-1,
#' non-`NA` vectors that can be coerced to character vectors. This is intended
#' to ensure that calls to the API will not fail with predictable errors, thus
#' avoiding unnecessary internet traffic.
#'
#' @inheritParams rlang::args_error_context
#' @inheritParams stbl::stabilize_chr_scalar
#' @inheritDotParams stbl::stabilize_chr_scalar x_class
#'
#' @return `x` coerced to a length-1 character vector, if possible.
#' @export
#'
#' @examples
#' stabilize_string("a")
#' stabilize_string(1.1)
#' x <- letters
#' try(stabilize_string(x))
#' x <- NULL
#' try(stabilize_string(x))
#' x <- character()
#' try(stabilize_string(x))
#' x <- NA
#' try(stabilize_string(x))
stabilize_string <- function(x,
...,
regex = NULL,
arg = rlang::caller_arg(x),
call = rlang::caller_env()) {
stbl::stabilize_chr_scalar(
x,
allow_null = FALSE,
allow_zero_length = FALSE,
allow_na = FALSE,
regex = regex,
x_arg = arg,
call = call,
...
)
}
60 changes: 60 additions & 0 deletions man/stabilize_string.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tests/testthat/test-stabilize.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
test_that("stabilize_string() checks strings", {
expect_error(stabilize_string(letters), class = "stbl_error_non_scalar")
expect_error(stabilize_string(NULL), class = "stbl_error_bad_null")
expect_error(stabilize_string(character()), class = "stbl_error_bad_empty")
expect_error(stabilize_string(NA), class = "stbl_error_bad_na")
expect_identical(
stabilize_string("a"),
"a"
)
})

0 comments on commit 6b6c10b

Please sign in to comment.