Skip to content

Commit

Permalink
hack fix failed fetch, closes #110 protects against #117
Browse files Browse the repository at this point in the history
  • Loading branch information
maxheld83 committed Feb 16, 2021
1 parent 67305b5 commit 4e83729
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,5 @@ Remotes:
subugoe/[email protected],
subugoe/rcrossref@51d036808c1dc1f46cf1bad1b136e97ad213e162,
rstudio/shinyvalidate@420aec079dbd2c0e12bf8350f4452d105a9cc45c,
subugoe/biblids@d44010e7d0b7d2eef016ef33a05d29e67417dfe6
subugoe/biblids@e7517c4a3d9a424616adca3282abd30fa994f713
Config/testthat/edition: 3
31 changes: 31 additions & 0 deletions R/cr_md_fetch.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,34 @@ license_normalise <- function(cr) {
mutate(license = gsub("/", "", license)) %>%
mutate(license = tolower(license))
}

#' Test whether DOI as metadata on Crossref
#'
#' @param doi [biblids::doi()] of length 1
#'
#' @noRd
has_cr_md <- function(doi) {
# TODO this is a bad hackjob, and should be replaced by proper biblids code asap
res <- suppressWarnings(rcrossref::cr_works(biblids::as_doi(doi)))
# TODO this is a very bad proxy; we actually mean the http response code, but rcrossref doesn't readily give that
nrow(res$data) != 0
}

#' @describeIn has_cr_md Vectorised version
#' @param dois [biblids::doi()]
#' @noRd
have_cr_md <- function(dois) purrr::map_lgl(dois, has_cr_md)

assert_cr_md <- function(dois) {
with_cr_md <- have_cr_md(dois)
if (any(with_cr_md)) {
warning(
"Omitted some DOIs for which no metadata on Crossref could be found."
)
}
if (all(!with_cr_md)) {
warning()
}

return(NULL)
}
32 changes: 28 additions & 4 deletions inst/app/index.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,41 @@ session_id <- as.character(floor(runif(1) * 1e20))
# get dois
dois <- biblids::doiEntryServer(id = "dois")
dois2 <- reactive({
with_cr_md <- have_cr_md(dois())
if (sum(with_cr_md) < 3) {
shiny::showNotification(
ui = c(
"Please provide at least 2 DOIs that can be found on crossref."
),
type = "error"
)
return(NULL)
} else if (any(!with_cr_md)) {
shiny::showNotification(
ui = c(
"Omitted some DOIs for which no metadata on Crossref could be found."
),
type = "warning"
)
return(dois()[with_cr_md])
} else {
dois()
}
})
# email
iv <- InputValidator$new()
iv$add_rule("email", sv_required())
iv$add_rule("email", ~ if (!is_valid_email(.)) "Please provide a valid email")
iv$enable()
html_email <- eventReactive(input$create, {
if (length(dois()) > 1) {
print(dois2())
if (isTruthy(dois2())) {
withProgress(
expr = {
render_email(dois = dois(), session_id = session_id)$html_html
render_email(dois = dois2(), session_id = session_id)$html_html
},
message = "Generating report..."
)
Expand All @@ -60,12 +84,12 @@ html_email <- eventReactive(input$create, {
output$draft <- renderUI(html_email())
observeEvent(input$send, {
if (length(dois() > 1 & iv$is_valid())) {
if (isTruthy(dois2())) {
withProgress(
expr = {
send_email(
to = input$email,
email = render_email(dois = dois(), session_id = session_id)
email <- render_email(dois = dois2(), session_id = session_id)
)
},
message = "Generating report and sending e-mail. You can close this window."
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-cr_md_fetch.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test_that("metadata can be retrieved", {
)
})


test_that("returns NULL if no metadata was retrieved", {
expect_null(get_cr_md("10.1000/foo"))
test_that("DOIs not on CR are identified", {
expect_true(has_cr_md("10.5194/wes-2019-70"))
expect_equal(has_cr_md("10.1000/foo"), FALSE)
})

0 comments on commit 4e83729

Please sign in to comment.