Skip to content

Commit

Permalink
version 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
csgillespie authored and cran-robot committed Dec 2, 2016
1 parent ceda956 commit 6be58d3
Show file tree
Hide file tree
Showing 15 changed files with 174 additions and 78 deletions.
11 changes: 5 additions & 6 deletions DESCRIPTION
@@ -1,22 +1,21 @@
Package: rtypeform
Type: Package
Title: Interface to 'typeform' Results
Version: 0.1.1
Date: 2016-03-31
Version: 0.2.0
Authors@R: person(given="Colin", family="Gillespie",
email="csgillespie@gmail.com", role = c("aut", "cre"))
Maintainer: Colin Gillespie <csgillespie@gmail.com>
URL: https://github.com/csgillespie/rtypeform
BugReports: https://github.com/csgillespie/rtypeform/issues
Description: An R interface to the 'typeform' (https://typeform.com) application program interface.
Description: An R interface to the 'typeform' <https://typeform.com> application program interface.
Also provides functions for downloading your results.
License: GPL-2 | GPL-3
LazyData: TRUE
Imports: jsonlite
Imports: jsonlite, httr
Suggests: testthat, curl
RoxygenNote: 5.0.1
NeedsCompilation: no
Packaged: 2016-03-31 15:55:40 UTC; ncsg3
Packaged: 2016-12-02 10:56:54 UTC; ncsg3
Author: Colin Gillespie [aut, cre]
Repository: CRAN
Date/Publication: 2016-03-31 18:47:59
Date/Publication: 2016-12-02 22:58:14
25 changes: 14 additions & 11 deletions MD5
@@ -1,12 +1,15 @@
cc2ba845bd73fdca690299994b49ee72 *DESCRIPTION
c4e46b96e22fb8be3831a513018b9c24 *NAMESPACE
4f05e1680d3a6a8f4735be3c6a63b168 *NEWS.md
b80a8bbf5aef0e34d6d3632f01785fdb *R/get_all_typeforms.R
b72aadda9d4f6b480f559b62fa0c6d34 *R/get_api.R
0ab5a91a83e043bd3b9100b27362f072 *R/get_results.R
6a89bb4d38d44f5f669e1e15ffa542ec *README.md
acb9dfa390e67e3b98a8c73d4115d190 *man/get_all_typeforms.Rd
8b7e2fe9e9ae256e4dcd4a1191014e28 *man/get_results.Rd
9ff4897f952966d24ba6777b21f5f32d *DESCRIPTION
bb60b191f125f7aa492c9af9a2e443fc *NAMESPACE
3171597ec652db90a31f77afa39b5571 *NEWS.md
9552be8f9fd8523388defe07d9b145db *R/check_api_response.R
88620aa48eb2508be344e2b5fdf92e1c *R/get_all_typeforms.R
434b2a2e3ce3aba877ac1c1163eeec0f *R/get_api.R
d11bd13ef9ee5822fb1ecaa2441ff34f *R/get_results.R
c5cff99fb27d183d319afca0307bf3a3 *README.md
befc087425a6f523e1919ab858cdeb05 *man/get_all_typeforms.Rd
2ff748b1861859a7c925c54ce89a9764 *man/get_api.Rd
522f6ef35dee256fdfef1efdd8a089d4 *man/get_results.Rd
fdd4d6b2f737f7cb02bf00b0bb2af68b *tests/testthat.R
cae894e4e6ac407d503fed6a0b3fb3f0 *tests/testthat/test_get_all_typeforms.R
b73e105dbda820de57fb7eec20a95933 *tests/testthat/test_get_results.R
5457a05a5e3cc8e34c78c75e7364f7c9 *tests/testthat/test_get_all_typeforms.R
5fdd66d530dd268dcda6eeeea8f10b8b *tests/testthat/test_get_linux_time.R
89af0afa90d2b0493d7630858d0670a5 *tests/testthat/test_get_results.R
5 changes: 5 additions & 0 deletions NAMESPACE
@@ -1,5 +1,10 @@
# Generated by roxygen2: do not edit by hand

export(get_all_typeforms)
export(get_api)
export(get_results)
importFrom(httr,GET)
importFrom(httr,content)
importFrom(httr,status_code)
importFrom(httr,user_agent)
importFrom(jsonlite,fromJSON)
5 changes: 5 additions & 0 deletions NEWS.md
@@ -1,3 +1,8 @@
# Version 0.2.0
* Adding user agent to API call.
* Better error handling.
* Export get_api function.

# Version 0.1.1
* Minor tweaks for CRAN.

Expand Down
11 changes: 11 additions & 0 deletions R/check_api_response.R
@@ -0,0 +1,11 @@
check_api_response = function(resp, cont) {
if (httr::status_code(resp) != 200) {
stop(
sprintf(
"rtypeform API request failed [%s]\n",
status_code(resp)
),
call. = FALSE
)
}
}
23 changes: 17 additions & 6 deletions R/get_all_typeforms.R
Expand Up @@ -5,17 +5,28 @@
#' @importFrom jsonlite fromJSON
#' @param api Default \code{NULL}. Your private api key. If \code{api} is \code{NULL}
#' we use the environment variable \code{Sys.getenv("typeform_api")}.
#' @return A two column data frame.
#' @return A list containing content and the response.
#' @importFrom httr user_agent GET status_code content
#' @export
#' @examples
#' \dontrun{
#' api = "XXXXX"
#' get_all_typeforms(api)
#' tf = get_all_typeforms(api)
#' tf$content
#' }
get_all_typeforms = function(api=NULL) {
api = get_api(api)
url = paste0("https://api.typeform.com/v1/forms?key=", api)
jsonlite::fromJSON(url)
}

ua = httr::user_agent("https://github.com/csgillespie/rtypeform")
url = "https://api.typeform.com/v1/forms"
resp = httr::GET(url,query=list(key=api), ua)
cont = httr::content(resp, "text")

check_api_response(resp, cont)
structure(
list(
content = jsonlite::fromJSON(cont),
response = resp
),
class = "rtypeform_all_typeforms"
)
}
10 changes: 8 additions & 2 deletions R/get_api.R
@@ -1,7 +1,13 @@
get_api = function(api){
#' Retrieve API key from Renviron
#'
#' If the entry \code{typeform_api} exists in your
#' \code{.Renviron} return that value. Otherwise, raise an error.
#' @inheritParams get_all_typeforms
#' @export
get_api = function(api=NULL){
if(is.null(api))
api = Sys.getenv("typeform_api")
if(nchar(api) == 0)
stop("Invalid api key.")
api
}
}
33 changes: 23 additions & 10 deletions R/get_results.R
Expand Up @@ -39,20 +39,21 @@ get_order_by = function(order_by) {
#' If \code{NULL} return all results.
#' @param order_by One of "completed", "date_land_desc", "date_land_incr",
#' "date_submit_desc", or "date_submit_incr".
#' @param simplify, Logical. By default, \code{TRUE}, and returns only the questionnaire
#' responses as a data frame. If \code{FALSE} return all results from the API call.
#' @return A list containing questions, stats, responses and http response.
#' @seealso https://www.typeform.com/help/data-api/
#' @export
#' @examples
#' \dontrun{
#' uid = "XXXX"
#' api = "YYYY"
#' get_results(uid, api)
#' results = get_results(uid, api)
#' results$stats
#' results$questions
#' results$responses
#' }
get_results = function(uid, api=NULL,
completed=NULL, since=NULL, until=NULL, offset=NULL, limit=NULL,
order_by = NULL,
simplify=TRUE) {
order_by = NULL) {
api = get_api(api)
url = paste0("https://api.typeform.com/v1/form/", uid, "?key=", api)
if(!is.null(completed)) {
Expand All @@ -66,9 +67,21 @@ get_results = function(uid, api=NULL,
if(!is.null(limit)) url = paste0(url, "&limit=", limit)

url = paste0(url , get_order_by(order_by))
results = jsonlite::fromJSON(url)
if(simplify)
return(results$response)
else
return(results)

ua = httr::user_agent("https://github.com/csgillespie/rtypeform")
resp = httr::GET(url, ua)
cont = httr::content(resp, "text")
check_api_response(resp, cont)

parsed = jsonlite::fromJSON(cont)
structure(
list(
stats = parsed$stats,
questions = parsed$questions,
responses = parsed$responses,
response = resp
),
class = "rtypeform_results"
)
}

64 changes: 35 additions & 29 deletions README.md
@@ -1,49 +1,54 @@
# API to typeform data sets
[![Build Status](https://travis-ci.org/csgillespie/rtypeform.svg?branch=master)](https://travis-ci.org/csgillespie/rtypeform)
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/rtypeform)](https://cran.r-project.org/package=rtypeform)
[![codecov.io](https://codecov.io/github/csgillespie/rtypeform/coverage.svg?branch=master)](https://codecov.io/github/csgillespie/rtypeform?branch=master)
<!-- README.md is generated from README.Rmd. Please edit that file -->
API to typeform data sets
=========================

[Typeform](https://typeform.com) is a company that specialises in online form building.
This R package allows users to download their form results through the exposed API.
[![Build Status](https://travis-ci.org/csgillespie/rtypeform.svg?branch=master)](https://travis-ci.org/csgillespie/rtypeform) [![Downloads](http://cranlogs.r-pkg.org/badges/rtypeform?color=brightgreen)](https://cran.r-project.org/package=rtypeform) [![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/rtypeform)](https://cran.r-project.org/package=rtypeform) [![codecov.io](https://codecov.io/github/csgillespie/rtypeform/coverage.svg?branch=master)](https://codecov.io/github/csgillespie/rtypeform?branch=master)

[Typeform](https://typeform.com) is a company that specialises in online form building. This R package allows users to download their form results through the exposed API.

## Installation
Installation
------------

The package can be installed via `devtools`
The package can be installed from CRAN

``` r
install.packages("rtypeform")
```

or you can install the development version via `devtools`

``` r
devtools::install_github("csgillespie/rtypeform")
```

The package can then be loaded in the usual way

```
``` r
library("rtypeform")
```

## Using the package
Using the package
-----------------

To use this package, you will need a [data API](https://www.typeform.com/help/data-api/)
key. With this key in position, you can then list your available forms
To use this package, you will need a [data API](https://www.typeform.com/help/data-api/) key. With this key in position, you can then list your available forms

```
``` r
api = "XXXXX"
typeforms = get_all_typeforms(api)
```

If you don't pass your `api` key as an argument, it will attempt to read the variable
`Sys.getenv("typeform_api")`.
If you don't pass your `api` key as an argument, it will attempt to read the variable `Sys.getenv("typeform_api")`.

You can download data from a particular typeform via
```
## uid can be obtained from the typeforms data set
## above

``` r
## uid can be obtained from the typeforms data set above
get_results(uid, api)
```

There are a number of options for downloading the data. For example

```
``` r
## Only completed forms
get_results(uid, api, completed=TRUE)
## Results since the 1st Jan
Expand All @@ -52,22 +57,23 @@ get_results(uid, api, since=as.Date("2016-01-01"))

See the `?get_results` help page for other options.

## Example: Multiple Filters / Order
Example: Multiple Filters / Order
---------------------------------

Imagine we only want to fetch only the last 10 completed responses.

* We only want completed results, so we add the parameter `completed=TRUE`.
* The results need to be ordered by newest results first, so we add the parameter `order_by="date_submit_desc"`
* We only want 10 results maximum, so we add the parameter `limit=10`
- We only want completed results, so we add the parameter `completed=TRUE`.
- The results need to be ordered by newest results first, so we add the parameter `order_by="date_submit_desc"`
- We only want 10 results maximum, so we add the parameter `limit=10`

This gives the function call

```
``` r
get_results(uid, api, completed=TRUE, order_by="date_submit_desc", limit=10)
```

## Other information
Other information
-----------------

* If you have any suggestions or find bugs, please use the github [issue tracker](https://github.com/csgillespie/typeform/issues).
* Feel free to submit pull requests.
* I intend to submit the package to CRAN within the next week or so.
- If you have any suggestions or find bugs, please use the github [issue tracker](https://github.com/csgillespie/rtypeform/issues).
- Feel free to submit pull requests.
5 changes: 3 additions & 2 deletions man/get_all_typeforms.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions man/get_api.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions man/get_results.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions tests/testthat/test_get_all_typeforms.R
@@ -1,6 +1,12 @@
test_that("Testing get_all_typeforms", {
skip_on_cran()
typeforms = get_all_typeforms()
expect_equal(ncol(typeforms), 2)
expect_equal(ncol(typeforms$content), 2)
}
)
)

test_that("Testing error handling", {
skip_on_cran()
testthat::expect_error(get_all_typeforms(api="XXXX"))
}
)
6 changes: 6 additions & 0 deletions tests/testthat/test_get_linux_time.R
@@ -0,0 +1,6 @@
test_that("Testing get_linux_time", {
skip_on_cran()
x = as.Date("1970-03-01")
expect_equal(rtypeform:::get_linux_time(x), 5097600)
}
)

0 comments on commit 6be58d3

Please sign in to comment.