Skip to content

Commit

Permalink
test for crossref doi starts #14
Browse files Browse the repository at this point in the history
  • Loading branch information
maxheld83 committed Dec 20, 2020
1 parent d44010e commit 545b852
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ URL: https://subugoe.github.io/biblids, https://github.com/subugoe/biblids
BugReports: https://github.com/subugoe/biblids/issues
Imports:
rlang (>= 0.4.8),
checkmate (>= 2.0.0)
checkmate (>= 2.0.0),
httr (>= 1.4.2)
Suggests:
testthat,
covr,
Expand Down
13 changes: 13 additions & 0 deletions R/doi.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,16 @@ doiEntryServer <- function(id) {
}
)
}

is_doi_on_cr <- function(x) {
res <- httr::HEAD(
url = paste0("https://api.crossref.org/works/", x)
)
if (res$status_code == 200) {
return(TRUE)
} else if (res$status_code == 404) {
return(FALSE)
} else {
httr::stop_for_status(res)
}
}
9 changes: 9 additions & 0 deletions tests/testthat/test-doi.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,12 @@ test_that("DOIs are extracted", {
)
expect_snapshot(str_extract_all_doi(doimash))
})

test_that("crossref DOIs are identified", {
doi_cr <- "10.5194/wes-2019-70"
doi_not_cr <- "10.5194/wes-5-819-202"
doi_bad <- " create a url with spaces"
expect_true(is_doi_on_cr(doi_cr))
expect_false(is_doi_on_cr(doi_not_cr))
expect_error(is_doi_on_cr(doi_bad))
})

0 comments on commit 545b852

Please sign in to comment.