Skip to content

Commit

Permalink
Chicago#154 add support for $query syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
copaDanielOKeefe committed Apr 20, 2018
1 parent d825f7e commit 57439e5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Expand Up @@ -10,7 +10,7 @@ Description: Provides easier interaction with
format and manages throttling by 'Socrata'.
Users can upload data to 'Socrata' portals directly
from R.
Version: 1.8.0-8
Version: 1.8.0-9
Date: 2018-01-15
Author: Hugh Devlin, Ph. D., Tom Schenk, Jr., Gene Leynes, Nick Lucius, John Malc, Mark Silverberg, and Peter Schmeideskamp
Maintainer: "Tom Schenk Jr." <developers@cityofchicago.org>
Expand Down
18 changes: 15 additions & 3 deletions R/RSocrata.R
Expand Up @@ -330,9 +330,21 @@ read.socrata <- function(url, app_token = NULL, email = NULL, password = NULL,
if (!is.null(names(parsedUrl$query))) { # check if URL has any queries
## if there is a query, check for specific queries and handle them
orderTest <- any(names(parsedUrl$query) == "$order")
soqlQueryTest <- any(names(parsedUrl$query) == "$query")
queries <- unlist(parsedUrl$query)
countTest <- any(startsWith(queries, "count"))
if(!orderTest & !countTest) # sort by Socrata unique identifier
queryTest <- any(names(parsedUrl$query) == '$query')
# soql query strings need to be at the end, so we reparse and reorder the query list here
if(soqlQueryTest){
fullQuery <- parsedUrl$query
soqlQueryString <- fullQuery$`$query`
soqlQueryList <- list(soqlQueryString)
names(soqlQueryList) <- ('$query')
fullQuery$`$query` <- NULL
parsedUrl$query <- c(fullQuery, soqlQueryList)
validUrl <- httr::build_url(parsedUrl)
}
if(!orderTest & !countTest & !queryTest) # sort by Socrata unique identifier
validUrl <- paste(validUrl, if(is.null(parsedUrl$query)) {'?'} else {"&"}, '$order=:id', sep='')
}
else {
Expand All @@ -345,8 +357,8 @@ read.socrata <- function(url, app_token = NULL, email = NULL, password = NULL,
page <- getContentAsDataFrame(response)
result <- page
dataTypes <- getSodaTypes(response)
# parse any $limit out of the URL
if(is.null(parsedUrl$query$`$limit`) & is.null(parsedUrl$query$`$LIMIT`))
# parse any $limit out of the URL -- a soql $query is a de-facto limit
if(is.null(parsedUrl$query$`$limit`) & is.null(parsedUrl$query$`$LIMIT`) & is.null(parsedUrl$query$`$query`))
limitProvided <- FALSE
else {
limitProvided <- TRUE
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-all.R
Expand Up @@ -375,6 +375,12 @@ test_that("CSV with Token", {
expect_equal(9, ncol(df), label="columns")
})

test_that("CSV with Token and query string", {
df <- read.socrata('https://soda.demo.socrata.com/resource/4334-bgaj.csv?$query=select depth', app_token="ew2rEMuESuzWPqMkyPfOSGJgE")
expect_equal(1000, nrow(df), label="rows")
expect_equal(1, ncol(df), label="columns")
})


test_that("readSocrataHumanReadableToken", {
df <- read.socrata('https://soda.demo.socrata.com/dataset/USGS-Earthquake-Reports/4334-bgaj', app_token="ew2rEMuESuzWPqMkyPfOSGJgE")
Expand Down

0 comments on commit 57439e5

Please sign in to comment.