Skip to content
This repository has been archived by the owner on May 10, 2022. It is now read-only.

Commit

Permalink
Changed get_institutions() to ref_institutions() to be more aligned w…
Browse files Browse the repository at this point in the history
…ith ROpenSci style (#17). Using the httr package with a bit more error checking (#5). Changed dontrun to donttest (#13).

There will likely be a fair bit of duplicate code as this goes on, so it will likely be moved to a helper function (#9).
  • Loading branch information
perrystephenson committed Jan 29, 2017
1 parent 93448a8 commit a0a42d2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Depends:
R (>= 3.2.5)
Imports:
curl (>= 2.1),
httr (>= 1.2.1),
jsonlite (>= 1.1),
tibble (>= 1.2)
RoxygenNote: 5.0.1
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(get_case_studies)
export(get_institutions)
export(get_tag_types)
export(get_tag_values)
export(get_units_of_assessment)
export(ref_institutions)
35 changes: 27 additions & 8 deletions R/get_institutions.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,39 @@
#' and returns a list of institutions and associated metadata, including the
#' UKPRN.
#'
#' @return Returns a data_frame (from the \code{tibble} package).
#' @return Returns a tibble (from the \code{tibble} package).
#'
#' @examples
#' \dontrun{
#' get_institutions()
#' \donttest{
#' ref_institutions()
#' }
#'
#' @export
get_institutions <- function() {
ref_institutions <- function() {

##### Call the API #####
query_url <- "http://impact.ref.ac.uk/casestudiesapi/REFAPI.svc/ListInstitutions"
tmp <- tibble::as_data_frame(jsonlite::fromJSON(query_url))
ua <- httr::user_agent("http://github.com/perrystephenson/refimpact")

return(tmp)
r <- httr::GET(
"http://impact.ref.ac.uk/casestudiesapi/REFAPI.svc/ListInstitutions", ua)

if (httr::http_type(r) != "application/json") {
stop("API did not return json", call. = FALSE)
}

parsed <- jsonlite::fromJSON(httr::content(r, as = "text"))

if (httr::http_error(r)) {
stop(
sprintf(
"API request failed [%s]\n%s\n<%s>",
httr::status_code(r),
parsed$message,
parsed$documentation_url
),
call. = FALSE
)
}

tibble::as_tibble(parsed)

}
12 changes: 6 additions & 6 deletions man/get_institutions.Rd → man/ref_institutions.Rd

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

0 comments on commit a0a42d2

Please sign in to comment.