Skip to content

Commit

Permalink
Fix api abbreviation support
Browse files Browse the repository at this point in the history
  • Loading branch information
willgearty committed Dec 19, 2023
1 parent 4d12a7f commit 2016530
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
21 changes: 12 additions & 9 deletions R/resolve_phylopic.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#' taxonomic name does not resolve to any node in the PhyloPic database, no
#' images will be returned for that name.
#'
#' The following APIs are available for matching (`api`):
#' The following APIs are available for querying (`api`):
#' \itemize{
#' \item{"eol.org": the \href{https://eol.org/}{Encyclopedia of Life}}
#' (note: `hierarchy = TRUE` is not currently available for this API) ("eol"
Expand Down Expand Up @@ -68,7 +68,13 @@
resolve_phylopic <- function(name, api = "gbif.org", hierarchy = FALSE,
max_ranks = 5, n = 1, filter = NULL, url = FALSE) {
url_arg <- url
# replace api abbreviations
abbrvs <- setNames(c("eol.org", "gbif.org", "marinespecies.org",
"opentreeoflife.org", "paleobiodb.org"),
c("eol", "gbif", "worms", "otol", "pbdb"))
if (api %in% names(abbrvs)) api <- abbrvs[[api]]

Check warning on line 75 in R/resolve_phylopic.R

View check run for this annotation

Codecov / codecov/patch

R/resolve_phylopic.R#L75

Added line #L75 was not covered by tests
# Check arguments ------------------------------------------------------
api <- match.arg(api, unname(abbrvs))
if (!is.character(name)) {
stop("`name` should be of class character.")
}
Expand All @@ -86,7 +92,7 @@ resolve_phylopic <- function(name, api = "gbif.org", hierarchy = FALSE,
name <- gsub("_", " ", name)
name_encode <- URLencode(name)
# Query specified API for the name -------------------------------------
if (api %in% c("eol", "eol.org")) {
if (api == "eol.org") {
# check api is online
headers <- curlGetHeaders("https://eol.org/api/search/1.0.json")
if (attr(headers, "status") != 200) {
Expand All @@ -107,7 +113,7 @@ resolve_phylopic <- function(name, api = "gbif.org", hierarchy = FALSE,
warning("`hierarchy = TRUE` is not currently available for eol.org.")
hierarchy <- FALSE
}
} else if (api %in% c("gbif", "gbif.org")) {
} else if (api == "gbif.org") {
# check api is online
headers <- curlGetHeaders("https://api.gbif.org/v1/")
if (attr(headers, "status") != 200) {
Expand All @@ -134,7 +140,7 @@ resolve_phylopic <- function(name, api = "gbif.org", hierarchy = FALSE,
jsn$order[1], jsn$class[1], jsn$phylum[1],
jsn$kingdom[1])
}
} else if (api %in% c("worms", "marinespecies.org")) {
} else if (api == "marinespecies.org") {
# check api is online
headers <- curlGetHeaders("https://www.marinespecies.org/rest/")
if (attr(headers, "status") != 200) {
Expand Down Expand Up @@ -166,7 +172,7 @@ resolve_phylopic <- function(name, api = "gbif.org", hierarchy = FALSE,
ids <- rev(ids)
name_vec <- rev(name_vec)
}
} else if (api %in% c("pbdb", "paleobiodb.org")) {
} else if (api == "paleobiodb.org") {
# check api is online
headers <- curlGetHeaders("https://paleobiodb.org/data1.2/")
if (attr(headers, "status") != 200) {
Expand Down Expand Up @@ -194,7 +200,7 @@ resolve_phylopic <- function(name, api = "gbif.org", hierarchy = FALSE,
ids <- rev(gsub("txn:", "", jsn$records$oid))
name_vec <- rev(jsn$records$nam)
}
} else if (api %in% c("otol", "opentreeoflife.org")) {
} else if (api == "opentreeoflife.org") {
# check api is online
headers <- curlGetHeaders("https://api.opentreeoflife.org/")
if (attr(headers, "status") != 200) {
Expand All @@ -217,9 +223,6 @@ resolve_phylopic <- function(name, api = "gbif.org", hierarchy = FALSE,
ids <- c(ids, jsn$lineage$ott_id)
name_vec <- c(name_vec, jsn$lineage$unique_name)
}
} else {
stop("`api` must be one of 'eol.org', 'gbif.org', 'marinespecies.org',
'opentreeoflife.org', or 'paleobiodb.org'")
}
# subset ids if more than max_ranks
ids <- ids[seq_len(min(length(ids), max_ranks))]
Expand Down
2 changes: 1 addition & 1 deletion man/resolve_phylopic.Rd

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

0 comments on commit 2016530

Please sign in to comment.