Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split functions and other smaller improvements for 1.6.3 #53

Merged
merged 23 commits into from
Jul 25, 2015
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ RSocrata.Rcheck
^\.travis\.yml$
appveyor.yml
CONTRIBUTING.md
NEWS.md
NEWS.md
note.md
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Description: Provides easier interaction with
returns an R data frame.
Converts dates to 'POSIX' format.
Manages throttling by 'Socrata'.
Version: 1.6.2-10
Date: 2015-7-12
Version: 1.6.3
Date: 2015-07-15
Author: Hugh Devlin, Ph. D., Tom Schenk, Jr., and John Malc
Maintainer: "Tom Schenk Jr." <developers@cityofchicago.org>
Depends:
Expand Down
247 changes: 0 additions & 247 deletions R/RSocrata.R

This file was deleted.

44 changes: 44 additions & 0 deletions R/errorHandling.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#' Provides error handling functionality
#'
#' @description Based on \url{http://dev.socrata.com/docs/response-codes.html}
#'
#' @section TODO: Add messages that alert the user on the URL being valid,
#' but one that is not compatible with RSocrata.
#' See \url{https://github.com/Chicago/RSocrata/issues/16}
#'
#' @param rsp - \code{\link{httr}} response
#' @importFrom httr stop_for_status
#'
#' @noRd
errorHandling <- function(rsp = NULL) {

if (rsp$status_code == 200) {
invisible("OK. Your request was successful.")

} else if(rsp$status_code == 202) {
warning("202 Request processing. You can retry your request, and when it's complete, you'll get a 200 instead.")

} else if(rsp$status_code == 400) {
stop("400 Bad request. Most probably was your request malformed.")

} else if(rsp$status_code == 401) {
# only necessary when accessing datasets that have been marked as private or when making write requests (PUT, POST, and DELETE)
stop("Unauthorized. You attempted to authenticate but something went wrong.")

} else if(rsp$status_code == 403) {
stop("Forbidden. You're not authorized to access this resource. Make sure you authenticate to access private datasets.")

} else if(rsp$status_code == 404) {
stop("Not found. The resource requested doesn't exist.")

} else if(rsp$status_code == 429) {
stop("Too Many Requests. Your client is currently being rate limited. Make sure you're using an app token.")

} else if(rsp$status_code == 500) {
stop("Server error.")

} else {
httr::stop_for_status(rsp)
}

}
29 changes: 29 additions & 0 deletions R/fourByFour.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#' Checks the validity of the syntax for a potential Socrata dataset Unique Identifier, also known as a 4x4.
#'
#' @description Will check the validity of a potential dataset unique identifier
#' supported by Socrata. It will provide an exception if the syntax
#' does not align to Socrata unique identifiers. It only checks for
#' the validity of the syntax, but does not check if it actually exists.
#'
#' @param fourByFour - a string; character vector of length one
#' @return TRUE if is valid Socrata unique identifier, FALSE otherwise
#' @author Tom Schenk Jr \email{tom.schenk@@cityofchicago.org}
#' @examples
#' isFourByFour(fourByFour = "4334-bgaj")
#' isFourByFour("433-bgaj")
#' isFourByFour(fourByFour = "4334-!gaj")
#'
#' @export
isFourByFour <- function(fourByFour = "") {

if (nchar(fourByFour) == 9) {
if(identical(grepl("[[:alnum:]]{4}-[[:alnum:]]{4}", fourByFour), TRUE)) {
return(TRUE)
} else {
return(FALSE)
}
} else {
return(FALSE)
}

}
32 changes: 32 additions & 0 deletions R/listDatasets.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#' List datasets available from a Socrata domain
#'
#' @param url - A Socrata URL. This simply points to the site root.
#' @return an R data frame containing a listing of datasets along with
#' various metadata.
#' @author Peter Schmiedeskamp \email{pschmied@@uw.edu}
#' @note URLs such as \code{"soda.demo.socrata.com"} are not supported
#' @examples
#' df <- ls.socrata(url = "http://soda.demo.socrata.com")
#' ## df.ny <- ls.socrata("https://data.ny.gov/")
#'
#' @importFrom jsonlite fromJSON
#' @importFrom httr parse_url build_url
#'
#' @export
ls.socrata <- function(url = "") {

parsedUrl <- httr::parse_url(url)

if(is.null(parsedUrl$scheme) | is.null(parsedUrl$hostname)) {
stop(url, " does not appear to be a valid URL.")
}
parsedUrl$path <- "data.json"

df <- jsonlite::fromJSON(httr::build_url(parsedUrl))
df <- df$dataset
df$issued <- as.POSIXct(df$issued)
df$modified <- as.POSIXct(df$modified)
df$theme <- as.character(df$theme)

return(df)
}
Loading