Skip to content
This repository has been archived by the owner on Oct 17, 2020. It is now read-only.

Commit

Permalink
updated man files for new roxygen2 - tidied a bit of code - fixed bro…
Browse files Browse the repository at this point in the history
…ken test, fixed description ripley nitpicking
  • Loading branch information
sckott committed Apr 22, 2015
1 parent 6c557ab commit 122caa6
Show file tree
Hide file tree
Showing 12 changed files with 23 additions and 43 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Package: enigma
Title: R Client for the Enigma API
Description: Enigma (https://enigma.io) holds many public datasets from
Description: The company Enigma (https://enigma.io) holds many public datasets from
governments, companies, universities, and organizations. Enigma provides an
API for data, metadata, and statistics on each of the datasets. enigma is
an R client to interact with the Enigma API, including getting the data
and metadata for datasets in Enigma, as well as collecting statistics on
datasets. In addition, you can download a gzipped csv file of a dataset
if you want the whole dataset. An API key from Enigma is required to use
enigma.
Version: 0.1.2.99
Date: 2015-03-18
Version: 0.1.3.99
Date: 2015-04-22
Authors@R: c(person("Scott", "Chamberlain", role = c("aut", "cre"),
email = "myrmecocystus@gmail.com"))
License: MIT + file LICENSE
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by roxygen2 (4.1.0): do not edit by hand
# Generated by roxygen2 (4.1.1): do not edit by hand

S3method(print,enigma_fetch)
S3method(print,enigma_rate)
Expand Down
11 changes: 5 additions & 6 deletions R/enigma_data.r
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,20 @@
#' }

enigma_data <- function(dataset=NULL, limit=50, select=NULL, sort=NULL, page=NULL, where=NULL,
search=NULL, key=NULL, ...)
{
search=NULL, key=NULL, ...) {
key <- check_key(key)
check_dataset(dataset)
if(!is.null(select)) select <- paste(select, collapse = ",")
if (!is.null(select)) select <- paste(select, collapse = ",")

url <- sprintf('%s/data/%s/%s', en_base(), key, dataset)
args <- engigma_compact(list(limit=limit, select=select, sort=sort, page=page,
where=where, search=search))
args <- engigma_compact(list(limit = limit, select = select, sort = sort, page = page,
where = where, search = search))
json <- enigma_GET(url, args, ...)
meta <- json$info
json$result <- lapply(json$result, as.list)
dat2 <- do.call(rbind.fill,
lapply(json$result, function(x){
x[sapply(x, is.null)] <- NA; data.frame(x, stringsAsFactors = FALSE)
}))
structure(list(success = json$success, datapath = json$datapath, info = meta, result = dat2), class="enigma")
structure(list(success = json$success, datapath = json$datapath, info = meta, result = dat2), class = "enigma")
}
15 changes: 6 additions & 9 deletions R/zzz.r
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
engigma_compact <- function (l) Filter(Negate(is.null), l)
engigma_compact <- function(l) Filter(Negate(is.null), l)

#' Error handler function for enigma_* functions
#' @param x A response object from a call to \code{httr::GET}
#' @keywords internal
error_handler <- function(x){
res_info <- content(x)$info
if(x$status_code %in% c(400,500)){
if (x$status_code %in% c(400, 500)) {
stop(sprintf("%s : %s", res_info$message, gsub('\"', "'", res_info$additional)), call. = FALSE)
}
stopifnot(x$headers$`content-type` == 'application/json; charset=utf-8')
Expand All @@ -14,17 +11,17 @@ error_handler <- function(x){
}

enigma_GET <- function(url, args, ...){
res <- GET(url, query=args, ...)
res <- GET(url, query = args, ...)
error_handler(res)
}

check_dataset <- function(dataset){
if(is.null(dataset)) stop("You must provide a dataset") else dataset
if (is.null(dataset)) stop("You must provide a dataset") else dataset
}

check_key <- function(x){
tmp <- if(is.null(x)) Sys.getenv("ENIGMA_KEY", "") else x
if(tmp == "") getOption("enigmaKey", stop("need an API key for the Enigma API")) else tmp
tmp <- if (is.null(x)) Sys.getenv("ENIGMA_KEY", "") else x
if (tmp == "") getOption("enigmaKey", stop("need an API key for the Enigma API")) else tmp
}

en_base <- function() 'https://api.enigma.io/v2'
2 changes: 1 addition & 1 deletion man/enigma.Rd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Generated by roxygen2 (4.1.1): do not edit by hand
% Please edit documentation in R/enigma-package.r
\docType{package}
\name{enigma}
Expand Down
2 changes: 1 addition & 1 deletion man/enigma_data.Rd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Generated by roxygen2 (4.1.1): do not edit by hand
% Please edit documentation in R/enigma_data.r
\name{enigma_data}
\alias{enigma_data}
Expand Down
2 changes: 1 addition & 1 deletion man/enigma_fetch.Rd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Generated by roxygen2 (4.1.1): do not edit by hand
% Please edit documentation in R/enigma_fetch.r
\name{enigma_fetch}
\alias{enigma_fetch}
Expand Down
2 changes: 1 addition & 1 deletion man/enigma_metadata.Rd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Generated by roxygen2 (4.1.1): do not edit by hand
% Please edit documentation in R/enigma_metadata.r
\name{enigma_metadata}
\alias{enigma_metadata}
Expand Down
2 changes: 1 addition & 1 deletion man/enigma_stats.Rd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Generated by roxygen2 (4.1.1): do not edit by hand
% Please edit documentation in R/enigma_stats.r
\name{enigma_stats}
\alias{enigma_stats}
Expand Down
16 changes: 0 additions & 16 deletions man/error_handler.Rd

This file was deleted.

2 changes: 1 addition & 1 deletion man/rate_limit.Rd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Generated by roxygen2 (4.1.1): do not edit by hand
% Please edit documentation in R/rate_limit.R
\name{rate_limit}
\alias{rate_limit}
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-enigma_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ test_that("enigma_data works correctly for sorting data", {
})

test_that("enigma_data works correctly to get data subset", {
res3 <- enigma_data(dataset='com.nike.manufacturing-disclosures', where='total_workers > 1000')
res3 <- enigma_data(dataset = 'us.gov.whitehouse.visitor-list', where = 'total_people > 5')
expect_is(res3, "enigma")
expect_true(res3$success)
expect_is(res3$datapath, "character")
expect_is(res3$info, "list")
expect_is(res3$result, "data.frame")
expect_more_than(sample(as.numeric(res3$result$total_workers), 1), 1000)
expect_more_than(min(as.numeric(res3$result$total_people)), 5)
})

0 comments on commit 122caa6

Please sign in to comment.