Skip to content

Commit

Permalink
Add support for updating D1_FORMATS and checking pkg vers
Browse files Browse the repository at this point in the history
Closes #44
  • Loading branch information
amoeba committed Sep 30, 2017
1 parent 7482748 commit eed63b4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
8 changes: 4 additions & 4 deletions R/editing.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ publish_object <- function(mn,
}

# Check if format ID is valid
if (!(format_id %in% D1_FORMATS)) {
if (!is.null(D1_FORMATS) && !(format_id %in% D1_FORMATS)) {
stop(call. = FALSE,
paste0("The format_id of '", format_id, "' is not a valid format ID. See https://cn.dataone.org/cn/v2/formats for the current list. This package stores a copy and may be out of date with that list so please email the author if needed."))
paste0("The provided format_id of '", format_id, "' is not a valid format ID. Check what you entered against the list of format IDs on https://cn.dataone.org/cn/v2/formats. Note that this list is cached when arcticdatautils is loaded so if you haven't restarted your R session in a while you may have an outdated copy and restarting your session may fix this."))
}

# Set up some variables for use later on
Expand Down Expand Up @@ -138,9 +138,9 @@ update_object <- function(mn, pid, path, format_id=NULL, new_pid=NULL, sid=NULL)
}

# Check if format ID is valid
if (!(format_id %in% D1_FORMATS)) {
if (!is.null(D1_FORMATS) && !(format_id %in% D1_FORMATS)) {
stop(call. = FALSE,
paste0("The format_id of '", format_id, "' is not a valid format ID. See https://cn.dataone.org/cn/v2/formats for the current list. This package stores a copy and may be out of date with that list so please email the author if needed."))
paste0("The provided format_id of '", format_id, "' is not a valid format ID. Check what you entered against the list of format IDs on https://cn.dataone.org/cn/v2/formats. Note that this list is cached when arcticdatautils is loaded so if you haven't restarted your R session in a while you may have an outdated copy and restarting your session may fix this."))
}

message(paste0("Updating object ", pid, " with the file at ", path, "."))
Expand Down
Binary file removed R/sysdata.rda
Binary file not shown.
51 changes: 51 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
.onLoad <- function(libname, pkgname) {
load_d1_formats_list("https://cn.dataone.org/cn/v2/formats")
warn_if_outdated()

invisible()
}

# Warning: This function produces a side-effect of assigning into the package
# env with <<- to update the value of D1_FORMATS
D1_FORMATS <- NULL
load_d1_formats_list <- function(url) {
req <- httr::GET(url)

if (httr::status_code(req) != 200) {
warning(paste0("Failed to load an up-to-date list of format IDs from ", url, " because the request to the CN failed. Checking of format IDs is disabled."))
return(invisible())
}

formats_content <- httr::content(req, encoding = "UTF-8")
format_id_nodes <- xml2::xml_find_all(formats_content, "//formatId")

if (length(format_id_nodes) == 0) {
warning(paste0("Failed to load an up-to-date list of format IDs from ", url, " because the response returned from the CN was not formatted as expected. Checking of format IDs is disabled."))
return(invisible())
}

D1_FORMATS <<- vapply(format_id_nodes, function(x) { xml2::xml_text(x) }, "")
}

warn_if_outdated <- function() {
installed_version <- packageVersion("arcticdatautils")

req <- httr::GET("https://api.github.com/repos/nceas/arcticdatautils/releases/latest",
httr::add_headers("Accept", "application/vnd.github.v3+json"))

release <- httr::content(req)

if (httr::status_code(req) != 200) {
warning(paste0("The request to check whether the version of arcticdatautils you have installed is the latest, the response from GitHub's API failed. The response was: \n\n", req))
return(invisible())
}

if (!("tag_name" %in% names(release))) {
warning(paste0("While checking to see if your installed version of arcticdatautils is the latest available, the response from GitHub's API was not as expected so checking was not done."))
return(invisible())
}

if (!stringr::str_detect(release$tag_name, paste0("^v", stringr::str_replace_all(as.character(installed_version), "\\.", "\\\\."), "$"))) {
warning(paste0("You do not have the most recent version of arcticdatautils installed. This is just a reminder to update next time you get the chance. Visit https://github.com/NCEAS/arcticdatautils/releases to find the latest release."))
}
}

0 comments on commit eed63b4

Please sign in to comment.