Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Share contact infos with Crossref #145

Merged
merged 8 commits into from
Oct 23, 2017
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions R/rcrossref-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
#' <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.
#' <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 `.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
Expand Down
45 changes: 43 additions & 2 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@njahn82 did you mean to leave this here?

headers = list(
`User-Agent` = rcrossref_ua(),
`X-USER-AGENT` = rcrossref_ua()
Expand Down Expand Up @@ -154,3 +157,41 @@ 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
} else {
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})$"

21 changes: 21 additions & 0 deletions man/rcrossref-package.Rd

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