Skip to content

Commit

Permalink
style entire package
Browse files Browse the repository at this point in the history
  • Loading branch information
jennybc committed Feb 27, 2018
1 parent ba715a2 commit b6e9799
Show file tree
Hide file tree
Showing 33 changed files with 186 additions and 152 deletions.
4 changes: 1 addition & 3 deletions R/aaa.R
Expand Up @@ -8,7 +8,7 @@
#' @usage lhs \%>\% rhs
NULL

if (getRversion() >= "2.15.1") utils::globalVariables(c(":="))
if (getRversion() >= "2.15.1") utils::globalVariables(c(":="))

# environment to hold data about the Drive API
.drive <- new.env(parent = emptyenv())
Expand Down Expand Up @@ -43,7 +43,6 @@ if (getRversion() >= "2.15.1") utils::globalVariables(c(":="))
)

.onLoad <- function(libname, pkgname) {

set_auth_active(TRUE)
set_api_key(.state[["tidyverse_api_key"]])
set_oauth_app(.state[["tidyverse_app"]])
Expand All @@ -58,7 +57,6 @@ if (getRversion() >= "2.15.1") utils::globalVariables(c(":="))
}

invisible()

}

## This function is never called
Expand Down
27 changes: 17 additions & 10 deletions R/dribble.R
Expand Up @@ -39,16 +39,20 @@ validate_dribble <- function(x) {
if (!has_dribble_cols(x)) {
missing_cols <- setdiff(dribble_cols, colnames(x))
stop_collapse(
c("Invalid dribble. These required column names are missing:",
missing_cols)
c(
"Invalid dribble. These required column names are missing:",
missing_cols
)
)
}

if (!has_dribble_coltypes(x)) {
mistyped_cols <- dribble_cols[!dribble_coltypes_ok(x)]
stop_collapse(
c("Invalid dribble. These columns have the wrong type:",
mistyped_cols)
c(
"Invalid dribble. These columns have the wrong type:",
mistyped_cols
)
)
}

Expand Down Expand Up @@ -78,9 +82,9 @@ dribble <- function(x = NULL) {

maybe_dribble <- function(x) {
if (is.data.frame(x) &&
has_dribble_cols(x) &&
has_dribble_coltypes(x) &&
has_drive_resource(x)) {
has_dribble_cols(x) &&
has_dribble_coltypes(x) &&
has_drive_resource(x)) {
new_dribble(x)
} else {
as_tibble(x)
Expand All @@ -103,9 +107,11 @@ has_dribble_cols <- function(x) {
}

dribble_coltypes_ok <- function(x) {
c(name = is.character(x$name),
c(
name = is.character(x$name),
id = is.character(x$id),
drive_resource = inherits(x$drive_resource, "list"))
drive_resource = inherits(x$drive_resource, "list")
)
}

has_dribble_coltypes <- function(x) {
Expand Down Expand Up @@ -135,7 +141,8 @@ as_parent <- function(d) {
if (!is_parental(d)) {
stop_glue(
"Requested parent {bt(in_var)} is invalid: neither a folder ",
"nor a Team Drive.")
"nor a Team Drive."
)
}
d
}
Expand Down
44 changes: 22 additions & 22 deletions R/drive_auth.R
Expand Up @@ -75,7 +75,6 @@ drive_auth <- function(oauth_token = NULL,
cache = getOption("httr_oauth_cache"),
use_oob = getOption("httr_oob_default"),
verbose = TRUE) {

if (reset) {
drive_deauth(clear_cache = TRUE, verbose = verbose)
}
Expand Down Expand Up @@ -120,7 +119,6 @@ drive_auth <- function(oauth_token = NULL,
#' drive_deauth()
#' }
drive_deauth <- function(clear_cache = TRUE, verbose = TRUE) {

if (clear_cache && file.exists(".httr-oauth")) {
if (verbose) {
message("Disabling .httr-oauth by renaming to .httr-oauth-SUSPENDED")
Expand All @@ -138,7 +136,6 @@ drive_deauth <- function(clear_cache = TRUE, verbose = TRUE) {
}

invisible(NULL)

}

#' View or set auth config
Expand Down Expand Up @@ -195,7 +192,7 @@ drive_auth_config <- function(active = TRUE,
structure(
list(
active = auth_active(),
oauth_app_name = oauth_app()[['appname']],
oauth_app_name = oauth_app()[["appname"]],
api_key = drive_api_key(),
token = access_cred()
),
Expand All @@ -206,14 +203,15 @@ drive_auth_config <- function(active = TRUE,
#' @export
print.auth_config <- function(x, ...) {
cat(
glue("googledrive auth state: ",
"{if (x[['active']]) 'active' else 'inactive'}\n",
"oauth app: ",
"{x[['oauth_app_name']]}\n",
"API key: ",
"{if (is.null(x[['api_key']])) 'unset' else 'set'}\n",
"token: ",
"{if (is.null(x[['token']])) 'not loaded' else 'loaded'}"
glue(
"googledrive auth state: ",
"{if (x[['active']]) 'active' else 'inactive'}\n",
"oauth app: ",
"{x[['oauth_app_name']]}\n",
"API key: ",
"{if (is.null(x[['api_key']])) 'unset' else 'set'}\n",
"token: ",
"{if (is.null(x[['token']])) 'not loaded' else 'loaded'}"
),
sep = ""
)
Expand Down Expand Up @@ -346,15 +344,19 @@ token_available <- function(verbose = TRUE) {
if (is.null(access_cred())) {
if (verbose) {
if (file.exists(".httr-oauth")) {
message("A .httr-oauth file exists in current working ",
"directory.\nWhen/if needed, the credentials cached in ",
".httr-oauth will be used for this session.\nOr run drive_auth() ",
"for explicit authentication and authorization.")
message(
"A .httr-oauth file exists in current working ",
"directory.\nWhen/if needed, the credentials cached in ",
".httr-oauth will be used for this session.\nOr run drive_auth() ",
"for explicit authentication and authorization."
)
} else {
message("No .httr-oauth file exists in current working directory.\n",
"When/if needed, 'googledrive' will initiate authentication ",
"and authorization.\nOr run drive_auth() to trigger this ",
"explicitly.")
message(
"No .httr-oauth file exists in current working directory.\n",
"When/if needed, 'googledrive' will initiate authentication ",
"and authorization.\nOr run drive_auth() to trigger this ",
"explicitly."
)
}
}
return(FALSE)
Expand All @@ -366,7 +368,6 @@ token_available <- function(verbose = TRUE) {
#'
#' @keywords internal
is_legit_token <- function(x, verbose = FALSE) {

if (!inherits(x, "Token2.0")) {
if (verbose) message("Not a Token2.0 object.")
return(FALSE)
Expand All @@ -388,5 +389,4 @@ is_legit_token <- function(x, verbose = FALSE) {
}

TRUE

}
2 changes: 1 addition & 1 deletion R/drive_cp.R
Expand Up @@ -97,7 +97,7 @@ drive_cp <- function(file, path = NULL, name = NULL, ..., verbose = TRUE) {
params[["name"]] <- name
}

request <- generate_request(
request <- generate_request(
endpoint = "drive.files.copy",
params = params
)
Expand Down
6 changes: 4 additions & 2 deletions R/drive_fields.R
Expand Up @@ -57,8 +57,10 @@ drive_fields <- function(fields = NULL,
if (!setequal(fields, out)) {
bad_fields <- setdiff(fields, out)
warning_collapse(
c("Ignoring fields that are non-standard for the Files resource:",
glue(" * {bad_fields}"))
c(
"Ignoring fields that are non-standard for the Files resource:",
glue(" * {bad_fields}")
)
)
}
out
Expand Down
1 change: 0 additions & 1 deletion R/drive_find.R
Expand Up @@ -108,7 +108,6 @@ drive_find <- function(pattern = NULL,
corpus = NULL,
...,
verbose = TRUE) {

if (!is.null(pattern) && !(is_string(pattern))) {
stop_glue("`pattern` must be a character string.")
}
Expand Down
4 changes: 2 additions & 2 deletions R/drive_get.R
Expand Up @@ -108,8 +108,8 @@ drive_path_exists <- function(path, verbose = TRUE) {

confirm_clear_path <- function(path, name) {
if (is.null(name) &&
!has_slash(path) &&
drive_path_exists(append_slash(path))) {
!has_slash(path) &&
drive_path_exists(append_slash(path))) {
stop_glue(
"Unclear if `path` specifies parent folder or full path\n",
"to the new file, including its name. ",
Expand Down
4 changes: 1 addition & 3 deletions R/drive_mime_type.R
Expand Up @@ -31,7 +31,6 @@
#' drive_mime_type(expose())
#' @export
drive_mime_type <- function(type = NULL) {

if (is.null(type)) {
return(invisible())
}
Expand Down Expand Up @@ -86,7 +85,6 @@ drive_mime_type <- function(type = NULL) {
#' drive_extension(c("text/plain", "pdf", "image/gif"))
#' @export
drive_extension <- function(type = NULL) {

if (is.null(type)) {
return(invisible())
}
Expand All @@ -99,7 +97,7 @@ drive_extension <- function(type = NULL) {

one_ext <- function(type) {
m <- which(.drive$mime_tbl$mime_type %in% type &
is_true(.drive$mime_tbl$default))
is_true(.drive$mime_tbl$default))
if (length(m) == 0L) {
m <- NA_integer_
}
Expand Down
1 change: 0 additions & 1 deletion R/drive_publish.R
Expand Up @@ -50,7 +50,6 @@ drive_change_publish <- function(file,
publish = TRUE,
...,
verbose = TRUE) {

file <- as_dribble(file)
file <- confirm_some_files(file)

Expand Down
7 changes: 4 additions & 3 deletions R/drive_reveal.R
Expand Up @@ -61,8 +61,10 @@
#' drive_get(id = "root") %>% drive_reveal("path")
#' }
drive_reveal <- function(file,
what = c("path", "trashed", "mime_type",
"permissions", "published")) {
what = c(
"path", "trashed", "mime_type",
"permissions", "published"
)) {
file <- as_dribble(file)
what <- match.arg(what)
reveal <- switch(
Expand All @@ -75,4 +77,3 @@ drive_reveal <- function(file,
)
reveal(file)
}

3 changes: 1 addition & 2 deletions R/drive_reveal_path.R
Expand Up @@ -89,12 +89,11 @@ pathify_one_path <- function(op, nodes, root_id) {
get_nodes <- function(path,
team_drive = NULL,
corpus = NULL) {

path_parts <- purrr::map(path, partition_path, maybe_name = TRUE)
## workaround for purrr <= 0.2.2.2
name <- purrr::map(path_parts, "name")
name <- purrr::flatten_chr(purrr::map_if(name, is.null, ~ NA_character_))
#name <- purrr::map_chr(path_parts, "name", .default = NA)
# name <- purrr::map_chr(path_parts, "name", .default = NA)
names <- unique(name)
names <- names[!is.na(names)]
names <- glue("name = {sq(names)}")
Expand Down
6 changes: 4 additions & 2 deletions R/drive_share.R
Expand Up @@ -61,8 +61,10 @@
#' drive_rm(file)
#' }
drive_share <- function(file,
role = c("reader", "commenter", "writer",
"owner", "organizer"),
role = c(
"reader", "commenter", "writer",
"owner", "organizer"
),
type = c("user", "group", "domain", "anyone"),
...,
verbose = TRUE) {
Expand Down
3 changes: 2 additions & 1 deletion R/drive_trash.R
Expand Up @@ -60,7 +60,8 @@ toggle_trash_one <- function(id, trash = TRUE) {
params = list(
fileId = id,
trashed = trash,
fields = "*")
fields = "*"
)
)
response <- make_request(request, encode = "json")
proc_res <- process_response(response)
Expand Down
8 changes: 4 additions & 4 deletions R/drive_upload.R
Expand Up @@ -74,7 +74,6 @@ drive_upload <- function(media,
type = NULL,
...,
verbose = TRUE) {

if (!file.exists(media)) {
stop_glue("\nFile does not exist:\n * {media}")
}
Expand Down Expand Up @@ -134,9 +133,10 @@ drive_upload <- function(media,
out <- as_dribble(list(process_response(response)))

if (verbose) {
message_glue("\nLocal file:\n * {media}\n",
"uploaded into Drive file:\n * {out$name}: {out$id}\n",
"with MIME type:\n * {out$drive_resource[[1]]$mimeType}"
message_glue(
"\nLocal file:\n * {media}\n",
"uploaded into Drive file:\n * {out$name}: {out$id}\n",
"with MIME type:\n * {out$drive_resource[[1]]$mimeType}"
)
}
invisible(out)
Expand Down
1 change: 0 additions & 1 deletion R/generate_request.R
Expand Up @@ -129,7 +129,6 @@ build_request <- function(path = "",
params = list(),
body = list(),
token = NULL) {

params <- partition_params(params, extract_path_names(path))

out <- list(
Expand Down
26 changes: 15 additions & 11 deletions R/make_request.R
Expand Up @@ -29,16 +29,20 @@
#' @export
#' @family low-level API functions
make_request <- function(x, ...) {
method <- list("GET" = httr::GET,
"POST" = httr::POST,
"PATCH" = httr::PATCH,
"PUT" = httr::PUT,
"DELETE" = httr::DELETE)[[x$method]]
method(url = x$url,
x$token,
drive_ua(),
query = x$query,
body = x$body, ...)
method <- list(
"GET" = httr::GET,
"POST" = httr::POST,
"PATCH" = httr::PATCH,
"PUT" = httr::PUT,
"DELETE" = httr::DELETE
)[[x$method]]
method(
url = x$url,
x$token,
drive_ua(),
query = x$query,
body = x$body, ...
)
}

#' @rdname make_request
Expand Down Expand Up @@ -126,7 +130,7 @@ drive_ua <- function() {
httr::user_agent(paste0(
"googledrive/", utils::packageVersion("googledrive"), " ",
## TO DO: uncomment this once we use gargle
#"gargle/", utils::packageVersion("gargle"), " ",
# "gargle/", utils::packageVersion("gargle"), " ",
"httr/", utils::packageVersion("httr")
))
}

0 comments on commit b6e9799

Please sign in to comment.