-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add expand_servers() function. (#68)
Closes #66.
- Loading branch information
1 parent
f3f4ed3
commit c343ec9
Showing
12 changed files
with
111 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#' @include zz-rapid.R | ||
NULL | ||
|
||
#' Expand server urls to absolute paths | ||
#' | ||
#' `expand_servers()` uses the `origin` property of a `rapid` object to expand | ||
#' the `servers` `url` property to an absolute path. | ||
#' | ||
#' @inheritParams rlang::args_error_context | ||
#' @inheritParams rlang::args_dots_empty | ||
#' @param x The object to update. Must be a `rapid`. | ||
#' | ||
#' @return A `rapid` object as returned by [rapid()], with absolute server | ||
#' paths. | ||
#' @export | ||
expand_servers <- S7::new_generic("expand_servers", dispatch_args = "x") | ||
|
||
S7::method(expand_servers, rapid) <- function(x) { | ||
if (length(x@servers@url)) { | ||
relative_servers <- .is_relative_url(x@servers@url) | ||
origin_url <- x@info@origin@url | ||
if (length(origin_url)) { | ||
base_url <- url_absolute("..", origin_url) | ||
x@servers@url[relative_servers] <- paste0( | ||
base_url, | ||
x@servers@url[relative_servers] | ||
) | ||
x@servers@url[relative_servers] <- gsub( | ||
pattern = "(?<!\\:)//", | ||
replacement = "/", | ||
x = x@servers@url[relative_servers], | ||
perl = TRUE | ||
) | ||
} | ||
} | ||
return(x) | ||
} | ||
|
||
S7::method(expand_servers, class_any) <- function(x, | ||
arg = rlang::caller_arg(x), | ||
call = rlang::caller_env()) { | ||
cli::cli_abort( | ||
"{.arg {arg}} {.cls {class(x)}} must be a {.cls rapid}.", | ||
call = call | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,3 +34,7 @@ | |
} | ||
x | ||
} | ||
|
||
.is_relative_url <- function(x) { | ||
grepl("^/", x) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ reference: | |
contents: | ||
- rapid | ||
- as_rapid | ||
- expand_servers | ||
- title: info class | ||
contents: | ||
- info | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
test_that("expand_servers can expand servers", { | ||
# test_url <- "https://api.apis.guru/v2/specs/fec.gov/1.0/openapi.yaml" | ||
# input_rapid <- suppressWarnings(as_rapid(url(test_url))) | ||
# input_rapid@servers <- servers( | ||
# url = c(input_rapid@servers@url, "https://example.com") | ||
# ) | ||
# saveRDS(input_rapid, test_path("fixtures", "fec_guru_rapid.rds")) | ||
input_rapid <- readRDS(test_path("fixtures", "fec_guru_rapid.rds")) | ||
output_rapid <- expand_servers(input_rapid) | ||
expect_identical( | ||
output_rapid@servers@url, | ||
c("https://api.open.fec.gov/v1", "https://example.com") | ||
) | ||
}) |