R/pronto_parse.R

#' Parse a response from a query to the Pronto API
#'
#' @param req The results of a \code{\link{pronto_GET}} command
#' @param simplifyDataFrame Coerce JSON arrays containing only records (JSON objects) into a data frame (default: \code{TRUE}))
#'
#' @return A list containing information about the state of all Pronto stations
#' @seealso Data format description: \url{http://www.prontocycleshare.com/assets/pdf/JSON.pdf}
#' @export
#'
#' @examples
#' r <- pronto_GET()
#' pronto_parse(r)
#'
pronto_parse <- function(req, simplifyDataFrame = TRUE) {
    if (httr::status_code(req) != 200) {
        stop("HTTP failure: ", req$status_code, "\n", call. = FALSE)
    }

    text <- httr::content(req, as = "text")
    if (identical(text, "")) stop("No output to parse", call. = FALSE)
    jsonlite::fromJSON(text, simplifyVector = FALSE,
                       simplifyDataFrame = simplifyDataFrame)
}
briandconnelly/pronto documentation built on May 13, 2019, 5:14 a.m.