Skip to content

Commit

Permalink
improved documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
njahn82 committed Jan 2, 2017
1 parent 88b7cb9 commit 0ddd852
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 44 deletions.
78 changes: 43 additions & 35 deletions R/oadoi_fetch.r
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,50 @@
#' This is the main function to retrieve comprehensive open acccess status
#' information from the oaDOI service. Please play nice with the API. At the
#' moment only 10k request are allowed per user and day.
#' For more info see \url{http://oadoi.org/api}
#' For more info see \url{http://oadoi.org/api}.
#'
#' @param dois character vector, search by a single DOI or many DOIs. A maximum
#' of 10,000 DOIs are allowed per request.
#' @param email character verctor, tell oaDOI your email adress and get notified
#' @param dois character vector, search by a single DOI or many DOIs. The API is limited
#' to 10,000 requests per day. If you need more, get in touch with
#' [email protected].
#' @param email character verctor, tell oaDOI your email adress to get notified
#' if something breaks. It also helps oaDOI to keep track of usage!
#' @param .progress Shows the \code{plyr}-style progress bar. Options are "none", "text",
#' "tk", "win", and "time". See \code{\link[plyr]{create_progress_bar}} for details
#' of each. By default, no progress bar is displayed.
#' "tk", "win", and "time". See \code{\link[plyr]{create_progress_bar}} for details
#' of each. By default, no progress bar is displayed.
#'
#' @return A tibble
#'
#' @examples \dontrun{
#' oadoi_fetch("10.1016/j.shpsc.2013.03.020")
#' oadoi_fetch(dois = c("10.1016/j.jbiotec.2010.07.030",
#' "10.1186/1471-2164-11-245"))
#' }
#'
#' @export
oadoi_fetch <- function(dois = NULL, email = NULL, .progress = "none") {
# limit
if (length(dois) > api_limit)
stop("The rate limit is 10k requests per day.
Get in touch with [email protected] to get an upgrade.", .call = FALSE)
# validate dois
dois <- plyr::ldply(dois, doi_validate)
if (nrow(dois[dois$is_valid == FALSE,]) > 0)
warning("Found mal-formed DOIs, which will not be send to oaDOI")
dois <- dplyr::filter(dois, is_valid == TRUE) %>%
.$doi %>%
as.character()
plyr::ldply(dois, oadoi_api_, .progress = .progress) %>%
# wrap as tibble
dplyr::as_data_frame()
}
oadoi_fetch <-
function(dois = NULL,
email = NULL,
.progress = "none") {
# limit
if (length(dois) > api_limit)
stop(
"The rate limit is 10k requests per day.
Get in touch with [email protected] to get an upgrade.",
.call = FALSE
)
# validate dois
dois <- plyr::ldply(dois, doi_validate)
if (nrow(dois[dois$is_valid == FALSE, ]) > 0)
warning("Found mal-formed DOIs, which will not be send to oaDOI")
dois <- dplyr::filter(dois, is_valid == TRUE) %>%
.$doi %>%
as.character()
plyr::ldply(dois, oadoi_api_, .progress = .progress) %>%
# wrap as tibble
dplyr::as_data_frame()
}

#' Post one DOI to access open access status information.
#' Get open access status information.
#'
#' In general, use oadoi_fetch instead. It calls this method, returning open
#' access status information from all your requests.
Expand All @@ -53,17 +61,17 @@ oadoi_api_ <- function(dois = NULL, email = NULL) {
u <- httr::modify_url(oadoi_baseurl(),
query = args_(email = email),
path = dois)
resp <- httr::GET(
u,
ua,
# be explicit about the API version roadoi has to request
add_headers(
Accept = paste0("application/x.oadoi.", oadoi_api_version(), "+json")))
resp <- httr::GET(u,
ua,
# be explicit about the API version roadoi has to request
add_headers(
Accept = paste0("application/x.oadoi.", oadoi_api_version(), "+json")
))

# parse json
if (httr::http_type(resp) != "application/json") {
stop("Ups, something went wrong, because API did not return json",
call. = FALSE)
}
jsonlite::fromJSON(content(resp, "text", encoding = "UTF-8"))$results
# parse json
if (httr::http_type(resp) != "application/json") {
stop("Ups, something went wrong, because API did not return json",
call. = FALSE)
}
jsonlite::fromJSON(content(resp, "text", encoding = "UTF-8"))$results
}
9 changes: 5 additions & 4 deletions man/oadoi_api_.Rd

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

12 changes: 7 additions & 5 deletions man/oadoi_fetch.Rd

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

1 change: 1 addition & 0 deletions roadoi.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ StripTrailingWhitespace: Yes
BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
3 changes: 3 additions & 0 deletions tests/testthat/test_oadoi_fetch.r
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ test_that("oadoi_fetch returns", {
a <- oadoi_fetch(dois = "10.7717/peerj.2323")
b <- oadoi_fetch(dois = c("10.1038/ng.919", "10.1105/tpc.111.088682"),
email = "[email protected]")
c <- oadoi_fetch("10.1016/j.shpsc.2013.03.020")

# correct classes
expect_is(a, "tbl_df")
expect_is(b, "tbl_df")
expect_is(c, "tbl_df")

# correct dimensions
expect_equal(nrow(a), 1)
expect_equal(nrow(b), 2)
expect_equal(nrow(c), 1)


expect_warning(oadoi_fetch(dois = c("ldld", "10.1038/ng.3260")))
Expand Down

0 comments on commit 0ddd852

Please sign in to comment.