Skip to content

Commit

Permalink
fix #106, using rappdirs now for caching
Browse files Browse the repository at this point in the history
  • Loading branch information
sckott committed Oct 21, 2016
1 parent a2fec15 commit df28a46
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 60 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ Imports:
R6,
shiny,
miniUI,
stringr
stringr,
rappdirs
Suggests:
roxygen2 (>= 5.0.1),
testthat,
Expand Down
50 changes: 28 additions & 22 deletions R/cr_ft_text.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
#' @export
#' @param url (character) A URL.
#' @param type (character) One of xml, plain, pdf, or all
#' @param path (character) Path to store pdfs in. Default: \code{~/.crossref/}
#' @param path (character) Path to store pdfs in. By default we use
#' \code{paste0(rappdirs::user_cache_dir(), "/crossref")}, but you can
#' set this directory to something different. Ignored unless getting
#' pdf
#' @param overwrite (logical) Overwrite file if it exists already?
#' Default: \code{TRUE}
#' @param read (logical) If reading a pdf, this toggles whether we extract
Expand Down Expand Up @@ -155,7 +158,7 @@
#' # }
#' }

cr_ft_text <- function(url, type='xml', path = "~/.crossref", overwrite = TRUE,
cr_ft_text <- function(url, type='xml', path = cr_cache_path(), overwrite = TRUE,
read=TRUE, verbose=TRUE, cache=TRUE, ...) {

auth <- cr_auth(url, type)
Expand All @@ -167,6 +170,8 @@ cr_ft_text <- function(url, type='xml', path = "~/.crossref", overwrite = TRUE,
)
}

cr_cache_path <- function() paste0(rappdirs::user_cache_dir(), "/crossref")

get_url <- function(a, b){
url <- if (inherits(a, "tdmurl")) a[[1]] else a[[b]]
if (grepl("pensoft", url)) {
Expand All @@ -178,7 +183,7 @@ get_url <- function(a, b){

#' @export
#' @rdname cr_ft_text
cr_ft_plain <- function(url, path = "~/.crossref", overwrite = TRUE, read=TRUE,
cr_ft_plain <- function(url, path = cr_cache_path(), overwrite = TRUE, read=TRUE,
verbose=TRUE, ...) {
if (is.null(url$plain[[1]])) {
stop("no plain text link found", call. = FALSE)
Expand All @@ -188,7 +193,7 @@ cr_ft_plain <- function(url, path = "~/.crossref", overwrite = TRUE, read=TRUE,

#' @export
#' @rdname cr_ft_text
cr_ft_xml <- function(url, path = "~/.crossref", overwrite = TRUE, read=TRUE,
cr_ft_xml <- function(url, path = cr_cache_path(), overwrite = TRUE, read=TRUE,
verbose=TRUE, ...) {
if (is.null(url$xml[[1]])) {
stop("no xml link found", call. = FALSE)
Expand All @@ -198,7 +203,7 @@ cr_ft_xml <- function(url, path = "~/.crossref", overwrite = TRUE, read=TRUE,

#' @export
#' @rdname cr_ft_text
cr_ft_pdf <- function(url, path = "~/.crossref", overwrite = TRUE, read=TRUE,
cr_ft_pdf <- function(url, path = cr_cache_path(), overwrite = TRUE, read=TRUE,
cache=FALSE, verbose=TRUE, ...) {
if (is.null(url$pdf[[1]])) {
stop("no pdf link found", call. = FALSE)
Expand All @@ -207,7 +212,7 @@ cr_ft_pdf <- function(url, path = "~/.crossref", overwrite = TRUE, read=TRUE,
read, verbose, cache, ...)
}

pick_type <- function(x, z){
pick_type <- function(x, z) {
x <- match.arg(x, c("xml","plain","pdf"))
if (length(z) == 1) {
avail <- attr(z[[1]], which = "type")
Expand All @@ -228,22 +233,23 @@ cr_auth <- function(url, type) {
plain = "text/plain",
pdf = "application/pdf"
)
switch(mem_num,
`78` = {
key <- Sys.getenv("CROSSREF_TDM_ELSEVIER")
#add_headers(`X-ELS-APIKey` = key, Accept = type)
add_headers(`CR-Clickthrough-Client-Token` = key, Accept = type)
},
`263` = {
key <- Sys.getenv("CROSSREF_TDM")
add_headers(`CR-TDM-Client_Token` = key, Accept = type)
# add_headers(`CR-Clickthrough-Client-Token` = key, Accept = type)
},
`311` = {
add_headers(
`CR-Clickthrough-Client-Token` = Sys.getenv("CROSSREF_TDM"),
Accept = type)
}
switch(
mem_num,
`78` = {
key <- Sys.getenv("CROSSREF_TDM_ELSEVIER")
#add_headers(`X-ELS-APIKey` = key, Accept = type)
add_headers(`CR-Clickthrough-Client-Token` = key, Accept = type)
},
`263` = {
key <- Sys.getenv("CROSSREF_TDM")
add_headers(`CR-TDM-Client_Token` = key, Accept = type)
# add_headers(`CR-Clickthrough-Client-Token` = key, Accept = type)
},
`311` = {
add_headers(
`CR-Clickthrough-Client-Token` = Sys.getenv("CROSSREF_TDM"),
Accept = type)
}
)
# add_headers(`CR-TDM-Client_Token` = key, Accept = type)
# add_headers(`CR-Clickthrough-Client-Token` = key, Accept = type)
Expand Down
13 changes: 8 additions & 5 deletions man/cr_ft_text.Rd

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

67 changes: 35 additions & 32 deletions tests/testthat/test_cr_ft_text.R
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
# # context("testing cr_ft_text")
#
# links <- cr_ft_links("10.3897/phytokeys.42.7604", "all")
context("cr_ft_text")

# out <- cr_works(
# filter = list(has_full_text = TRUE, license_url = "http://creativecommons.org/licenses/by/4.0/"),
# limit = 100
# )

# links <- cr_ft_links("10.7717/peerj.1282")
# xml1 <- cr_ft_text(links, 'xml')
# pdf_read <- cr_ft_text(links, "pdf", read=FALSE, verbose = FALSE)
# pdf <- cr_ft_text(links, "pdf", verbose = FALSE)
#
# test_that("cr_ft_text returns correct class", {
# expect_is(xml1, "XMLInternalDocument")
# expect_is(pdf_read, "character")
# expect_is(pdf, "xpdf_char")
# })
#
# test_that("cr_ft_text dimensions are correct", {
# expect_equal(length(xml1), 1)
# expect_equal(length(pdf_read), 1)
# expect_equal(length(pdf), 2)
# expect_equal(length(pdf$meta), 14)
# expect_equal(length(pdf$data), 1)
# })
#

test_that("cr_ft_text works: pdf", {
links <- cr_ft_links("10.1155/mbd.1994.183", "all")
pdf_read <- cr_ft_text(links, "pdf", read = FALSE, verbose = FALSE)
pdf <- cr_ft_text(links, "pdf", verbose = FALSE)
#expect_is(xml1, "xml_document")
expect_is(pdf_read, "character")
expect_is(pdf, "xpdf_char")
#expect_equal(length(xml1), 2)
expect_equal(length(pdf_read), 1)
expect_equal(length(pdf), 2)
expect_equal(length(pdf$meta), 14)
expect_equal(length(pdf$data), 1)
})

# test_that("cr_ft_text gives back right values", {
# library("XML")
# expect_match(xpathApply(xml1, "//ref", xmlValue)[[1]], "Ake AssiL")
# library("xml2")
# expect_match(xml2::xml_find_all(xml1, "//ref")[[1]], "Ake AssiL")
# expect_match(pdf_read, "~/.crossref")
#
# # nms <- c('Title','Author','Creator','Producer','CreationDate','ModDate','Tagged','Form',
# # 'Pages','Encrypted','Page size','File size','Optimized','PDF version')
# # expect_named(pdf$meta, nms)
# })
#
# test_that("cr_ft_text fails correctly", {
# expect_error(cr_ft_text(), 'argument "url" is missing')
# expect_error(cr_ft_text("3434"), "Chosen type value not available in links")
# expect_error(cr_ft_text(links, type = "adfasf"), "'arg' should be one of")
# })

test_that("cr_ft_text fails correctly", {
expect_error(cr_ft_text(), 'argument "url" is missing')
expect_error(cr_ft_text("3434"), "a character vector argument expected")

links <- cr_ft_links("10.1155/mbd.1994.183", "all")
expect_error(cr_ft_text(links, type = "adfasf"), "'arg' should be one of")
})

0 comments on commit df28a46

Please sign in to comment.