-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add gna_data_sources() and make gnr_datasources( defunct
relates to #942
- Loading branch information
1 parent
1f4871b
commit 92e7541
Showing
7 changed files
with
144 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#' Get metadata about GNA data sources | ||
#' | ||
#' Downloads metadata about Global Names Architecture (GNA) data sources | ||
#' available to be used in other GNA functions. | ||
#' | ||
#' @param output_type What format of output to return. Either `'json'`, | ||
#' `'list'`, or `'table'`. | ||
#' @param ... Passed to [crul::HttpClient]. | ||
#' | ||
#' @author Zachary S.L. Foster | ||
#' | ||
#' @examples \dontrun{ | ||
#' | ||
#' gna_data_sources() | ||
#' } | ||
#' | ||
#' @export | ||
gna_data_sources <- function(output_type = 'table', ...) { | ||
gna_data_sources_url <- 'https://verifier.globalnames.org/api/v1/data_sources' | ||
|
||
# Check arguments | ||
possible_output_types <- c('json', 'table', 'list') | ||
if (! output_type %in% possible_output_types) { | ||
stop(call. = FALSE, 'The `output_type` value must be one of: ', paste0(possible_output_types, collapse = ', ')) | ||
} | ||
|
||
# Make and parse API call | ||
api <- crul::HttpClient$new(gna_data_sources_url, headers = tx_ual, opts = list(...)) | ||
response <- api$get() | ||
response$raise_for_status() | ||
response_json <- response$parse("UTF-8") | ||
if (output_type == 'json') { | ||
return(response_json) | ||
} | ||
response_data <- jsonlite::fromJSON(response_json, FALSE) | ||
|
||
# Reformat response data to a list | ||
if (output_type == 'list') { | ||
return(response_data) | ||
} | ||
|
||
# Reformat response data to a table | ||
if (output_type == 'table') { | ||
used_cols <- c( | ||
'id', | ||
'titleShort', | ||
'title', | ||
'description', | ||
'curation', | ||
'hasTaxonData', | ||
'recordCount', | ||
'updatedAt', | ||
'homeURL', | ||
'uuid', | ||
'version', | ||
'isOutlinkReady', | ||
'doi' | ||
) | ||
response_table <- do.call(rbind, lapply(response_data, function(x) { | ||
out <- x[used_cols] | ||
names(out) <- used_cols | ||
out[unlist(lapply(out, is.null))] <- NA | ||
as.data.frame(out) | ||
})) | ||
response_table <- tibble::as_tibble(response_table) | ||
} | ||
return(response_table) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
context("gna_data_sources") | ||
|
||
test_that("gna_data_sources returns a table", { | ||
skip_on_cran() | ||
vcr::use_cassette("gna_data_sources", { | ||
tmp <- gna_data_sources() | ||
}) | ||
expect_is(tmp, "data.frame") | ||
expect_equal(NCOL(tmp), 13) | ||
}) | ||
|
This file was deleted.
Oops, something went wrong.
92e7541
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is R/dna_data_sources.r a typo? should it be R/gna_data_sources.r? Does not affect functionality.