Skip to content

Commit

Permalink
Merge pull request #113 from Chicago/issue72
Browse files Browse the repository at this point in the history
Adds metadata attributes to ls.socrata(). Fixes #72
  • Loading branch information
geneorama committed Nov 3, 2016
2 parents 47d0366 + 0ed533d commit bb5e4e9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Description: Provides easier interaction with
format and manages throttling by 'Socrata'.
Users can upload data to Socrata portals directly
from R.
Version: 1.7.1-20
Date: 2016-10-26
Version: 1.7.1-23
Date: 2016-10-31
Author: Hugh Devlin, Ph. D., Tom Schenk, Jr., and John Malc
Maintainer: "Tom Schenk Jr." <developers@cityofchicago.org>
Depends:
Expand Down
23 changes: 16 additions & 7 deletions R/RSocrata.R
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,10 @@ read.socrata <- function(url, app_token = NULL, email = NULL, password = NULL,
#' various metadata.
#' @author Peter Schmiedeskamp \email{pschmied@@uw.edu}
#' @examples
#' # Download list of data sets
#' df <- ls.socrata("http://soda.demo.socrata.com")
#' # Check schema definition for metadata
#' attributes(df)
#' @importFrom jsonlite fromJSON
#' @importFrom httr parse_url
#' @export
Expand All @@ -353,15 +356,21 @@ ls.socrata <- function(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 <- as.data.frame(df$dataset)
df$issued <- as.POSIXct(df$issued)
df$modified <- as.POSIXct(df$modified)
df$theme <- as.character(df$theme)
return(df)
data_dot_json <- jsonlite::fromJSON(httr::build_url(parsedUrl))
data_df <- as.data.frame(data_dot_json$dataset)
# Assign Catalog Fields as attributes
attr(data_df, "@context") <- data_dot_json$`@context`
attr(data_df, "@id") <- data_dot_json$`@id`
attr(data_df, "@type") <- data_dot_json$`@type`
attr(data_df, "conformsTo") <- data_dot_json$conformsTo
attr(data_df, "describedBy") <- data_dot_json$describedBy
# Convert dates (strings) to POSIX-formatted dates
data_df$issued <- as.POSIXct(data_df$issued)
data_df$modified <- as.POSIXct(data_df$modified)
data_df$theme <- as.character(data_df$theme)
return(data_df)
}


#' Wrap httr PUT/POST in some diagnostics
#'
#' In case of failure, report error details from Socrata.
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-all.R
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ test_that("incorrect API Query Human Readable", {
expect_equal(9, ncol(df), label="columns")
})

context("ls.socrata functions correctly")

test_that("List datasets available from a Socrata domain", {
# Makes some potentially erroneous assumptions about availability
# of soda.demo.socrata.com
Expand All @@ -380,6 +382,11 @@ test_that("List datasets available from a Socrata domain", {
expect_equal(as.logical(rep(TRUE, length(names(df)))), names(df) %in% c(core_names))
})

test_that("Catalog Fields are assigned as attributes when listing data sets", {
df <- ls.socrata("https://soda.demo.socrata.com")
catalog_fields <- c("@context", "@id", "@type", "conformsTo", "describedBy")
expect_equal(as.logical(rep(TRUE, length(catalog_fields))), catalog_fields %in% names(attributes(df)))
})

context("Test reading private Socrata dataset with email and password")

Expand Down

0 comments on commit bb5e4e9

Please sign in to comment.