From 9cd6c364a563d2843f03041552a3d010e943f45c Mon Sep 17 00:00:00 2001 From: Najko Jahn Date: Wed, 18 Oct 2017 22:21:10 +0200 Subject: [PATCH 1/8] store email locally and optionally send it to crossref via header --- R/rcrossref-package.R | 20 ++++++++++++++++++ R/zzz.R | 44 ++++++++++++++++++++++++++++++++++++++-- man/rcrossref-package.Rd | 21 +++++++++++++++++++ 3 files changed, 83 insertions(+), 2 deletions(-) diff --git a/R/rcrossref-package.R b/R/rcrossref-package.R index 3c0215b..cf6444b 100644 --- a/R/rcrossref-package.R +++ b/R/rcrossref-package.R @@ -66,6 +66,26 @@ #' citations will be added to a file called `crossref.bib`. New citations #' will be appended to that file. Addin authored by Hao Zhu #' +#' +#' @section Be nice and share your email with Crossref: +#' The Crossref team encourage requests with appropriate contact information +#' and will forward you to a dedicated API cluster for improved performance when +#' you share your email address with them. +#' +#' +#' To pass your email address to Crossref via this client, simply store it +#' as environment variable in `.Renviron` like this: +#' +#' 1. Open file: +#' `file.edit("~/.Renviron")` +#' +#' 2. Add email address to be shared with Crossref +#' `crossref_email = name@example.com` +#' +#' 3. Save the file and restart your R session +#' +#' Don't wanna share your email any longer? Simply delete it from +#' `~/.Renviron` #' #' @importFrom methods as #' @importFrom utils modifyList packageVersion diff --git a/R/zzz.R b/R/zzz.R index 5d06d71..6dfd59e 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -16,14 +16,17 @@ rcrossref_ua <- function() { versions <- c(paste0("r-curl/", utils::packageVersion("curl")), paste0("crul/", utils::packageVersion("crul")), sprintf("rOpenSci(rcrossref/%s)", - utils::packageVersion("rcrossref"))) + utils::packageVersion("rcrossref")), + get_email()) paste0(versions, collapse = " ") } -cr_GET <- function(endpoint, args, todf = TRUE, on_error = warning, parse = TRUE, ...) { +cr_GET <- function(endpoint, args, todf = TRUE, on_error = warning, parse = TRUE, + ...) { url <- sprintf("https://api.crossref.org/%s", endpoint) cli <- crul::HttpClient$new( url = url, + opts = list(verbose = TRUE), headers = list( `User-Agent` = rcrossref_ua(), `X-USER-AGENT` = rcrossref_ua() @@ -154,3 +157,40 @@ prep_args <- function(query, filter, offset, limit, sample, sort, } `%||%` <- function(x, y) if (is.null(x) || length(x) == 0) y else x + + +#' Share email with Crossref in `.Renviron` +#' +#' @noRd +get_email <- function() { + email <- Sys.getenv("crossref_email") + if (identical(email, "")) { + NULL + } + paste0("(mailto:", val_email(email), ")") +} + +#' Email checker +#' +#' It implementents the following regex stackoverflow solution +#' http://stackoverflow.com/a/25077140 +#' +#' @param email email address (character string) +#' +#' @noRd +val_email <- function(email) { + if (!grepl(email_regex(), email)) + stop("Email address seems not properly formatted - Please check your .Renviron!", + call. = FALSE) + return(email) +} + +#' Email regex +#' +#' From \url{http://stackoverflow.com/a/25077140} +#' +#' @noRd +email_regex <- + function() + "^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,4})$" + diff --git a/man/rcrossref-package.Rd b/man/rcrossref-package.Rd index 4cfd613..390f32c 100644 --- a/man/rcrossref-package.Rd +++ b/man/rcrossref-package.Rd @@ -89,4 +89,25 @@ will be appended to that file. Addin authored by Hao Zhu \url{https://github.com/haozhu233} } +\section{Be nice and share your email with Crossref}{ + +The Crossref team encourage requests with appropriate contact information +and will forward you to a dedicated API cluster for improved performance when +you share your email address with them. +\url{https://github.com/CrossRef/rest-api-doc#good-manners--more-reliable-service} + +To pass your email address to Crossref via this client, simply store it +as environment variable in \code{.Renviron} like this: +\enumerate{ +\item Open file: +\code{file.edit("~/.Renviron")} +\item Add email address to be shared with Crossref +\code{crossref_email = name@example.com} +\item Save the file and restart your R session +} + +Don't wanna share your email any longer? Simply delete it from +\code{~/.Renviron} +} + \keyword{package} From c815eaca7e5a460d36893660372be798dddede40 Mon Sep 17 00:00:00 2001 From: Najko Jahn Date: Wed, 18 Oct 2017 22:22:36 +0200 Subject: [PATCH 2/8] don't verbose api call --- R/zzz.R | 1 - 1 file changed, 1 deletion(-) diff --git a/R/zzz.R b/R/zzz.R index 6dfd59e..015472f 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -26,7 +26,6 @@ cr_GET <- function(endpoint, args, todf = TRUE, on_error = warning, parse = TRUE url <- sprintf("https://api.crossref.org/%s", endpoint) cli <- crul::HttpClient$new( url = url, - opts = list(verbose = TRUE), headers = list( `User-Agent` = rcrossref_ua(), `X-USER-AGENT` = rcrossref_ua() From 21f5b92c494c8c4bb8f77e4fb022af9b76cb25a5 Mon Sep 17 00:00:00 2001 From: Najko Jahn Date: Thu, 19 Oct 2017 09:48:01 +0200 Subject: [PATCH 3/8] test why travis test fails --- .travis.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.travis.yml b/.travis.yml index 1a4999a..abc56bc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,11 @@ addons: packages: - poppler-utils +install: + - mkdir -p ~/Rlib + - echo 'crossref_email = name@example.com' > .Renviron + - echo 'options(repos = "http://cran.rstudio.com")' > .Rprofile + r_github_packages: - jimhester/covr From d0adfda753a74c1534ef6ee7aa2bb0688a2056fb Mon Sep 17 00:00:00 2001 From: Najko Jahn Date: Thu, 19 Oct 2017 09:55:45 +0200 Subject: [PATCH 4/8] remove custom library path --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index abc56bc..378fdc3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,8 +10,7 @@ addons: install: - mkdir -p ~/Rlib - echo 'crossref_email = name@example.com' > .Renviron - - echo 'options(repos = "http://cran.rstudio.com")' > .Rprofile - + r_github_packages: - jimhester/covr From aaab00f309d4eed16c0fcfd128fa1715c35d04b5 Mon Sep 17 00:00:00 2001 From: Najko Jahn Date: Thu, 19 Oct 2017 10:00:08 +0200 Subject: [PATCH 5/8] custom steps before install --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 378fdc3..28831a6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,7 @@ addons: packages: - poppler-utils -install: +before_install: - mkdir -p ~/Rlib - echo 'crossref_email = name@example.com' > .Renviron From d5e34dd7ffd3eea4ef35fabd7566c09976333951 Mon Sep 17 00:00:00 2001 From: Najko Jahn Date: Thu, 19 Oct 2017 10:24:28 +0200 Subject: [PATCH 6/8] =?UTF-8?q?=F0=9F=98=93=20return=20NULL=20when=20no=20?= =?UTF-8?q?email=20address=20is=20stored=20in=20.Renviron?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .travis.yml | 4 ---- R/zzz.R | 4 +++- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 28831a6..1a4999a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,10 +7,6 @@ addons: packages: - poppler-utils -before_install: - - mkdir -p ~/Rlib - - echo 'crossref_email = name@example.com' > .Renviron - r_github_packages: - jimhester/covr diff --git a/R/zzz.R b/R/zzz.R index 015472f..cb62bb9 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -26,6 +26,7 @@ cr_GET <- function(endpoint, args, todf = TRUE, on_error = warning, parse = TRUE url <- sprintf("https://api.crossref.org/%s", endpoint) cli <- crul::HttpClient$new( url = url, + opts = list(verbose = TRUE), headers = list( `User-Agent` = rcrossref_ua(), `X-USER-AGENT` = rcrossref_ua() @@ -165,8 +166,9 @@ get_email <- function() { email <- Sys.getenv("crossref_email") if (identical(email, "")) { NULL - } + } else { paste0("(mailto:", val_email(email), ")") + } } #' Email checker From 308f184730fbfe4711fbc12be41925982a4c30b0 Mon Sep 17 00:00:00 2001 From: Najko Jahn Date: Fri, 20 Oct 2017 17:10:20 +0200 Subject: [PATCH 7/8] remove verbose option --- R/zzz.R | 1 - 1 file changed, 1 deletion(-) diff --git a/R/zzz.R b/R/zzz.R index cb62bb9..a655b0a 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -26,7 +26,6 @@ cr_GET <- function(endpoint, args, todf = TRUE, on_error = warning, parse = TRUE url <- sprintf("https://api.crossref.org/%s", endpoint) cli <- crul::HttpClient$new( url = url, - opts = list(verbose = TRUE), headers = list( `User-Agent` = rcrossref_ua(), `X-USER-AGENT` = rcrossref_ua() From dd5ad2cfe89357680981c7893a75e83467a8922a Mon Sep 17 00:00:00 2001 From: Najko Jahn Date: Mon, 23 Oct 2017 12:20:19 +0200 Subject: [PATCH 8/8] adding some tests --- tests/testthat/test_cr_agency.R | 22 ++++++++++++++++++++++ tests/testthat/test_cr_funders.R | 22 ++++++++++++++++++++++ tests/testthat/test_cr_works.R | 23 +++++++++++++++++++++++ 3 files changed, 67 insertions(+) diff --git a/tests/testthat/test_cr_agency.R b/tests/testthat/test_cr_agency.R index c35ab81..51756ac 100644 --- a/tests/testthat/test_cr_agency.R +++ b/tests/testthat/test_cr_agency.R @@ -19,4 +19,26 @@ test_that("cr_ageny fails correctly", { expect_error(cr_agency(dois = cr_r(3), timeout_ms = 1)) }) +test_that("cr_agency - email works", { + skip_on_cran() + + Sys.setenv("crossref_email" = "name@example.com") + expect_is(cr_agency(dois = '10.1038/jid.2009.428'), "list") +}) + + +test_that("cr_agency - email is validated", { + skip_on_cran() + + Sys.setenv("crossref_email" = "name@example") + expect_error(cr_agency(dois = '10.1038/jid.2009.428')) +}) + +test_that("cr_agency - email NULL works", { + skip_on_cran() + + Sys.setenv("crossref_email" = "") + expect_is(cr_agency(dois = '10.1038/jid.2009.428'), "list") +}) + Sys.sleep(2) diff --git a/tests/testthat/test_cr_funders.R b/tests/testthat/test_cr_funders.R index 722d7ca..7a800e6 100644 --- a/tests/testthat/test_cr_funders.R +++ b/tests/testthat/test_cr_funders.R @@ -44,5 +44,27 @@ test_that("cr_funders fails correctly", { "Resource not found") }) +test_that("cr_works - email works", { + skip_on_cran() + + Sys.setenv("crossref_email" = "name@example.com") + expect_is(cr_funders(dois=c('10.13039/100000001')), "list") +}) + + +test_that("cr_funders - email is validated", { + skip_on_cran() + + Sys.setenv("crossref_email" = "name@example") + expect_error(cr_funders(dois=c('10.13039/100000001'))) +}) + +test_that("cr_funders - email NULL works", { + skip_on_cran() + + Sys.setenv("crossref_email" = "") + expect_is(cr_funders(dois=c('10.13039/100000001')), "list") +}) + Sys.sleep(2) diff --git a/tests/testthat/test_cr_works.R b/tests/testthat/test_cr_works.R index a4c9006..a7a4bb0 100644 --- a/tests/testthat/test_cr_works.R +++ b/tests/testthat/test_cr_works.R @@ -155,5 +155,28 @@ test_that("cr_works - parses affiliation inside authors correctly", { expect_named(aa$data$author[[1]], c("given", "family", "affiliation.name")) }) +test_that("cr_works - email works", { + skip_on_cran() + + Sys.setenv("crossref_email" = "name@example.com") + a <- cr_works(query="NSF") + expect_is(a, "list") +}) + +test_that("cr_works - email is validated", { + skip_on_cran() + + Sys.setenv("crossref_email" = "name@example") + expect_error(cr_works(query="NSF")) +}) + +test_that("cr_works - email NULL works", { + skip_on_cran() + + Sys.setenv("crossref_email" = "") + a <- cr_works(query="NSF") + expect_is(a, "list") +}) + Sys.sleep(2)