Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
admit that I must deal with visibility [covr]
Browse files Browse the repository at this point in the history
  • Loading branch information
jennybc committed Mar 30, 2015
1 parent cd1edc9 commit 7b38a14
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 13 deletions.
7 changes: 5 additions & 2 deletions R/consume-data.R
Expand Up @@ -37,8 +37,11 @@ get_via_csv <- function(ss, ws = 1, ...) {
this_ws <- get_ws(ss, ws)

## since gsheets_GET expects xml back, just using GET for now
req <-
httr::GET(this_ws$exportcsv, get_google_token())
if(ss$is_public) {
req <- httr::GET(this_ws$exportcsv)
} else {
req <- httr::GET(this_ws$exportcsv, get_google_token())
}

if(is.null(httr::content(req))) {
stop("Worksheet is empty. There are no cells that contain data.")
Expand Down
4 changes: 3 additions & 1 deletion R/googlesheet.R
Expand Up @@ -16,9 +16,10 @@
#' \item \code{sheet_id} the id of the spreadsheet
#' \item \code{updated} the time of last update (at time of registration)
#' \item \code{get_date} the time of registration
#' \item \code{visibility} visibility of spreadsheet (Google's confusing
#' \item \code{visibility} visibility of spreadsheet (Google's confusing
#' vocabulary); actually, does not describe a property of spreadsheet itself but
#' rather whether requests will be made with or without authentication
#' \item \code{is_public} logical indicating visibility is "public", as opposed to "private"
#' \item \code{author_name} the name of the owner
#' \item \code{author_email} the email of the owner
#' \item \code{links} data.frame of links specific to the spreadsheet
Expand All @@ -40,6 +41,7 @@ googlesheet <- function() {
updated = character() %>% as.POSIXct(),
get_date = character() %>% as.POSIXct(),
visibility = character(),
is_public = logical(),
author_name = character(),
author_email = character(),
links = character(), # initialize as data.frame?
Expand Down
7 changes: 4 additions & 3 deletions R/print.R
Expand Up @@ -19,11 +19,12 @@
#' @export
print.googlesheet <- function(x, ...) {

sprintf(" Spreadsheet title: %s\n", x$sheet_title) %>% cat
sprintf(" Spreadsheet title: %s\n", x$sheet_title) %>% cat()
sprintf(" Date of googlesheets::register_ss: %s\n",
x$get_date %>% format.POSIXct(usetz = TRUE)) %>% cat
x$get_date %>% format.POSIXct(usetz = TRUE)) %>% cat()
sprintf(" Date of last spreadsheet update: %s\n",
x$updated %>% format.POSIXct(usetz = TRUE)) %>% cat
x$updated %>% format.POSIXct(usetz = TRUE)) %>% cat()
sprintf(" visibility: %s\n", x$visibility) %>% cat()
cat("\n")

ws_output <-
Expand Down
5 changes: 2 additions & 3 deletions R/register.R
Expand Up @@ -323,9 +323,8 @@ register_ss <- function(x, key = NULL, ws_feed = NULL,
as.POSIXct(format = "%Y-%m-%dT%H:%M:%S", tz = "UTC")
ss$get_date <- req$date

## I'm ambivalent about even storing this; it's baked into the links
## holdover from an earlier stage of pkg development ... omit?
ss$visibility <- req$url %>% dirname %>% basename
ss$visibility <- req$url %>% dirname() %>% basename()
ss$is_public <- ss$visibility == "public"

ss$author_name <- req$content[["author"]][["name"]]
ss$author_email <- req$content[["author"]][["email"]]
Expand Down
36 changes: 36 additions & 0 deletions data-for-demo/02_create-published-to-the-web-sheet.R
@@ -0,0 +1,36 @@
#' ---
#' output: md_document
#' ---

library("googlesheets")
suppressPackageStartupMessages(library("dplyr"))

## damn you render and your hard-wiring of wd = dir where file lives!
## if I don't commit this abomination, existing .httr-oauth cannot be found :(
if (getwd() %>% basename == "data-for-demo") {
setwd("..")
}

## "make clean"
delete_ss(regex = "^iris_public$")

iris_ss <- new_ss("iris_public")
iris_ss

iris_ss <- iris_ss %>%
edit_cells(input = head(iris), header = TRUE, trim = TRUE)
iris_ss

iris_ss %>% get_via_lf()
iris_ss %>% get_via_csv()

## via browser, publish this sheet to the web
## in sheets, File > Publish to the web ...
## in future, do programmatically w/ googlesheets or driver
## https://github.com/jennybc/googlesheets/issues/62
## https://docs.google.com/spreadsheets/d/1cAYN-a089TSw8GF0RadQNdZiWo2RzekT-8swZeYME4A/pubhtml

iris_key <- "1cAYN-a089TSw8GF0RadQNdZiWo2RzekT-8swZeYME4A"
iris_public <- register_ss(key = iris_key, visibility = "public")
iris_public
iris_public %>% get_via_csv()
1 change: 1 addition & 0 deletions man/googlesheet.Rd
Expand Up @@ -27,6 +27,7 @@ includes the fields:
\item \code{visibility} visibility of spreadsheet (Google's confusing
vocabulary); actually, does not describe a property of spreadsheet itself but
rather whether requests will be made with or without authentication
\item \code{is_public} logical indicating visibility is "public", as opposed to "private"
\item \code{author_name} the name of the owner
\item \code{author_email} the email of the owner
\item \code{links} data.frame of links specific to the spreadsheet
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/helper01_setup-sheets.R
Expand Up @@ -29,3 +29,7 @@ old_title <- "Old Style Sheet"
## WARNING! this is not the key provided for this sheet by list_sheets()
## so it seems we cannot obtain a key from old style Sheet URLs
# old_key <- "0Audw-qi1jh3fdG9qZFVLR0NvZGRyNHFNUFBtN2tudUE"

## "Published to the web" sheet
## For accessing via visibility = "public" w/o ever having authenticated
ptw_key <- "1cAYN-a089TSw8GF0RadQNdZiWo2RzekT-8swZeYME4A"
Binary file modified tests/testthat/pts_identify_ss.rds
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/testthat/test-download-spreadsheet.R
@@ -1,4 +1,4 @@
context("Downloading spreadsheets")
context("download sheets")

test_that("Spreadsheet can be exported", {

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-edit-data.R
@@ -1,4 +1,4 @@
context("updating cell values")
context("edit cells")

ss <- register_ss(pts_title, verbose = FALSE)
ws <- "for_updating"
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-edit-spreadsheets.R
@@ -1,4 +1,4 @@
context("edit spreadsheets")
context("edit sheets")

test_that("Spreadsheet can be created and deleted", {

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-register.R
@@ -1,4 +1,4 @@
context("register a spreadsheet")
context("register sheets")

test_that("Spreadsheets visible to authenticated user can be listed", {
ss_list <- list_sheets()
Expand Down
33 changes: 33 additions & 0 deletions tests/testthat/test-zz-consume-public-data.R
@@ -0,0 +1,33 @@
context("consume data WITHOUT authenticating")
## this is a separate file, with a name that is alphabetically last, because I
## want to change working directory, w/o wrecking other tests

temp_dir <- tempdir()
old_wd <- setwd(temp_dir)

iris_public <- register_ss(key = ptw_key, visibility = "public")

iris_ish <- iris %>% head(6)
## because our data consumption m.o. is stringsAsFactors = FALSE
iris_ish$Species <- iris_ish$Species %>% as.character()

test_that("We can get data 'published to the web' sheet w/o authenticating, via csv", {

tmp <- iris_public %>% get_via_csv()
expect_equivalent(tmp, iris_ish)

})

test_that("We can get data 'published to the web' w/o authenticating, via list feed", {

iris_ish2 <- iris_ish
## because the list feed will lowercase the names
names(iris_ish2) <- names(iris_ish2) %>% tolower()

tmp <- iris_public %>% get_via_lf()
expect_equivalent(tmp, iris_ish)


})

setwd(old_wd)

0 comments on commit 7b38a14

Please sign in to comment.