From eed63b43c66c117c4eae55bb09461a16da07bc6c Mon Sep 17 00:00:00 2001 From: Bryce Mecum Date: Fri, 29 Sep 2017 16:04:34 -0800 Subject: [PATCH] Add support for updating D1_FORMATS and checking pkg vers Closes #44 --- R/editing.R | 8 ++++---- R/sysdata.rda | Bin 1325 -> 0 bytes R/zzz.R | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 4 deletions(-) delete mode 100644 R/sysdata.rda create mode 100644 R/zzz.R diff --git a/R/editing.R b/R/editing.R index 187bb41..9199019 100644 --- a/R/editing.R +++ b/R/editing.R @@ -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 @@ -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, ".")) diff --git a/R/sysdata.rda b/R/sysdata.rda dele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zG=OOoCV0b(>u2?w%{%56G47&biXvHCMVHlq=mGb7fN_ z)#XZew_MrOws&)7Q`_Fjm5sN&lPjAt@8-&;%)7Z#Wm&Yph#{zZXumm5#1L};H599qU$&5Aa3vUm%=DD|}j{GKrj(k)ejhE8WY zux*6DD`^cKDi7@&!#-)f0wA=T3X~$3H-Bvxf??h#)3VNgST!Wr77HQac9!aU?Sz3!#|VEONVqa;!uk`Vv^ru2er diff --git a/R/zzz.R b/R/zzz.R new file mode 100644 index 0000000..b34d3f5 --- /dev/null +++ b/R/zzz.R @@ -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.")) + } +}