From 950c555c9862510c5e828f8522ce20eb3b7c118e Mon Sep 17 00:00:00 2001 From: Bob Rudis Date: Tue, 21 Feb 2017 13:41:16 -0500 Subject: [PATCH] ran CRAN checks and cleaned the code up a bit --- .Rbuildignore | 1 + DESCRIPTION | 7 ++++++- NAMESPACE | 5 +++++ R/aaa.r | 9 +++++++++ R/docker-splash.r | 1 - R/execute.r | 2 +- R/helpers.r | 16 ++++++++-------- R/render-har.r | 4 ++-- R/render-png.r | 6 +++--- R/render_file.R | 3 ++- R/splashr-package.R | 4 ++++ man/as_har.Rd | 2 +- man/execute_lua.Rd | 4 ++-- man/get_content_type.Rd | 4 ++-- man/render_file.Rd | 4 +++- man/render_har.Rd | 2 +- man/render_png.Rd | 4 ++-- man/stop_splash.Rd | 2 -- 18 files changed, 52 insertions(+), 28 deletions(-) diff --git a/.Rbuildignore b/.Rbuildignore index ad0bf8f..7fd5ab6 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -7,3 +7,4 @@ ^NOTES\.*html$ ^img$ ^CONDUCT\.md$ +^README_files$ diff --git a/DESCRIPTION b/DESCRIPTION index 36f410c..107d0dd 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -34,6 +34,11 @@ Imports: HARtools, openssl, lubridate, - scales + formatR, + scales, + harbor, + stats, + utils, + curl RoxygenNote: 6.0.0 Remotes: wch/harbor diff --git a/NAMESPACE b/NAMESPACE index e0c6281..bac57c0 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -79,12 +79,17 @@ importFrom(HARtools,HARviewerOutput) importFrom(HARtools,renderHARviewer) importFrom(HARtools,writeHAR) importFrom(clipr,read_clip) +importFrom(curl,curl_unescape) +importFrom(formatR,tidy_source) importFrom(jsonlite,fromJSON) importFrom(lubridate,ymd_hms) importFrom(openssl,base64_decode) importFrom(scales,comma) +importFrom(stats,setNames) importFrom(stringi,stri_detect_regex) importFrom(stringi,stri_split_fixed) importFrom(stringi,stri_split_regex) +importFrom(utils,capture.output) +importFrom(utils,str) importFrom(xml2,read_html) importFrom(xml2,url_parse) diff --git a/R/aaa.r b/R/aaa.r index deefb46..d8e67f2 100644 --- a/R/aaa.r +++ b/R/aaa.r @@ -6,3 +6,12 @@ trunc_string <- function (x, maxlen = 20, justify = "left") { for (i in 1:length(x)) if (toolong[i]) chopx[i] <- paste(chopx[i], "...", sep = "") return(formatC(chopx, width = maxlen, flag = ifelse(justify == "left", "-", " "))) } + + +parse_query <- function(query) { + params <- vapply(stri_split_regex(query, "&", omit_empty=TRUE)[[1]], + stri_split_fixed, "=", 2, simplify=TRUE, + FUN.VALUE=character(2)) + purrr::set_names(as.list(curl::curl_unescape(params[2,])), + curl::curl_unescape(params[1,])) +} \ No newline at end of file diff --git a/R/docker-splash.r b/R/docker-splash.r index a7d8c8b..9eb702b 100644 --- a/R/docker-splash.r +++ b/R/docker-splash.r @@ -55,7 +55,6 @@ start_splash <- function(host = harbor::localhost, add_tempdir=FALSE) { #' Stop a running a Splash server Docker container #' -#' @param splash_container saved Splash container id from [start_splash] #' @param splash_container Docker `container` object created by [start_splash] #' @note you need Docker running on your system and have pulled the container with #' [install_spash] and started the Splash container with [start_splash] for this diff --git a/R/execute.r b/R/execute.r index 758451b..8cfaab2 100644 --- a/R/execute.r +++ b/R/execute.r @@ -2,7 +2,7 @@ #' #' @md #' @param splash_obj Object created by a call to [splash] -#' @param lua_sourc Browser automation script. See [Splash Script](http://splash.readthedocs.io/en/stable/scripting-tutorial.html#scripting-tutorial) Tutorial for more info. +#' @param lua_source Browser automation script. See [Splash Script](http://splash.readthedocs.io/en/stable/scripting-tutorial.html#scripting-tutorial) Tutorial for more info. #' @param timeout A timeout (in seconds) for the render (defaults to 30). #' @param allowed_domains Comma-separated list of allowed domain names. If present, Splash won’t load anything neither from domains not in this list nor from subdomains of domains not in this list. #' @param proxy Proxy profile name or proxy URL. diff --git a/R/helpers.r b/R/helpers.r index 8c87f00..e6f246a 100644 --- a/R/helpers.r +++ b/R/helpers.r @@ -12,7 +12,6 @@ get_response_body <- function(har_resp_obj) { #' Retrieve or test content type of a HAR request object #' -#' @param har_resp_obj HAR response object #' @export get_content_type <- function(har_resp_obj) { ctype <- har_resp_obj$response$content$mimeType @@ -30,6 +29,7 @@ is_content_type <- function(har_resp_obj, type="application/json") { } #' @rdname get_content_type +#' @param har_resp_obj a reponse object from [render_har]() or [execute_lua]() #' @export is_json <- function(har_resp_obj) { is_content_type(har_resp_obj) } @@ -78,13 +78,13 @@ is_gif <- function(har_resp_obj) { is_content_type(har_resp_obj, type="image/gif #' @rdname get_content_type #' @export -is_xhr <- function(x) { +is_xhr <- function(har_resp_obj) { - if (is.null(x$request$headers)) return(NA) - if (length(x$request$headers)==0) return(NA) + if (is.null(har_resp_obj$request$headers)) return(NA) + if (length(har_resp_obj$request$headers)==0) return(NA) - y <- map(x$request$headers, "value") - names(y) <- tolower(map_chr(x$request$headers, "name")) + y <- map(har_resp_obj$request$headers, "value") + names(y) <- tolower(map_chr(har_resp_obj$request$headers, "name")) if ("x-requested-with" %in% names(y)) { return(tolower("xmlhttprequest") == tolower(y[["x-requested-with"]])) @@ -116,8 +116,8 @@ get_request_type <- function(har_resp_obj) { #' @rdname get_request_type #' @export -is_get <- function(har_resp_obj) { get_requet_type(har_resp_obj) == "GET" } +is_get <- function(har_resp_obj) { get_request_type(har_resp_obj) == "GET" } #' @rdname get_request_type #' @export -is_post <- function(har_resp_obj) { get_requet_type(har_resp_obj) == "POST" } +is_post <- function(har_resp_obj) { get_request_type(har_resp_obj) == "POST" } diff --git a/R/render-har.r b/R/render-har.r index a40644e..a72f86c 100644 --- a/R/render-har.r +++ b/R/render-har.r @@ -6,7 +6,7 @@ #' @md #' @param response_body When `TRUE`, response content is included in the HAR records #' @inheritParams render_html -#' @return a [HARtools::har] object +#' @return a `HARtools` `har` object #' @references [Splash docs](http://splash.readthedocs.io/en/stable/index.html) #' @export render_har <- function(splash_obj = splash_local, url, base_url, response_body=FALSE, timeout=30, resource_timeout, wait=0, @@ -47,7 +47,7 @@ render_har <- function(splash_obj = splash_local, url, base_url, response_body=F #' Turn a generic Splash HAR response into a HAR object #' -#' @param as_har splash response object as returned by `splash:har()` lua script +#' @param splash_resp splash response object as returned by `splash::har()` lua script #' @export as_har <- function(splash_resp) { diff --git a/R/render-png.r b/R/render-png.r index f716bc7..29572de 100644 --- a/R/render-png.r +++ b/R/render-png.r @@ -8,7 +8,7 @@ #' @inheritParams render_html #' @export render_png <- function( - splash_obj = splash_local, url, base_url=NULL, quality=75, width=1024, height=768, + splash_obj = splash_local, url, base_url=NULL, width=1024, height=768, timeout=30, resource_timeout, wait=0, render_all=FALSE, proxy, js, js_src, filters, allowed_domains, allowed_content_types, forbidden_content_types, viewport="1024x768", images, headers, body, @@ -16,8 +16,8 @@ render_png <- function( params <- list(url=url, timeout=timeout, wait=if (render_all & wait == 0) 0.5 else wait, - viewport=viewport, - quality=quality, width=width, height=height, render_all=as.numeric(render_all)) + viewport=viewport, width=width, height=height, + render_all=as.numeric(render_all)) if (!missing(base_url)) params$base_url <- base_url if (!missing(resource_timeout)) params$resource_timeout <- resource_timeout diff --git a/R/render_file.R b/R/render_file.R index 28d7e6e..a0ae42f 100644 --- a/R/render_file.R +++ b/R/render_file.R @@ -8,12 +8,13 @@ #' @md #' @param splash_obj Object created by a call to [splash]() #' @param file_path Absolute path to a filename on the local host. **This only works with a locally running Splash instance started with [start_splash]().** +#' @param wait seconds to wait #' @param output either `html` or `png` to get the page content or an image capture #' @param viewport View width and height (in pixels) of the browser viewport to render the web page. Format is "`x`". e.g. 800x600. Default value is 1024x768. #' @param ... other params to [render_html]() or [render_png]() #' @return An XML document or `magick` object #' @export -render_file <- function(splash_obj = slpash_local, file_path, output=c("html", "png"), wait=0, viewport="1024x768", ...) { +render_file <- function(splash_obj = splash_local, file_path, output=c("html", "png"), wait=0, viewport="1024x768", ...) { output <- match.arg(output, c("html", "png")) diff --git a/R/splashr-package.R b/R/splashr-package.R index 3aba8e2..740ded3 100644 --- a/R/splashr-package.R +++ b/R/splashr-package.R @@ -23,6 +23,10 @@ #' @importFrom clipr read_clip #' @importFrom lubridate ymd_hms #' @importFrom scales comma +#' @importFrom stats setNames +#' @importFrom formatR tidy_source +#' @importFrom utils capture.output str +#' @importFrom curl curl_unescape NULL diff --git a/man/as_har.Rd b/man/as_har.Rd index f245879..8ca450b 100644 --- a/man/as_har.Rd +++ b/man/as_har.Rd @@ -7,7 +7,7 @@ as_har(splash_resp) } \arguments{ -\item{as_har}{splash response object as returned by `splash:har()` lua script} +\item{splash_resp}{splash response object as returned by `splash::har()` lua script} } \description{ Turn a generic Splash HAR response into a HAR object diff --git a/man/execute_lua.Rd b/man/execute_lua.Rd index 30a9f3c..76cbdf8 100644 --- a/man/execute_lua.Rd +++ b/man/execute_lua.Rd @@ -10,6 +10,8 @@ execute_lua(splash_obj, lua_source, timeout = 30, allowed_domains, proxy, \arguments{ \item{splash_obj}{Object created by a call to \link{splash}} +\item{lua_source}{Browser automation script. See \href{http://splash.readthedocs.io/en/stable/scripting-tutorial.html#scripting-tutorial}{Splash Script} Tutorial for more info.} + \item{timeout}{A timeout (in seconds) for the render (defaults to 30).} \item{allowed_domains}{Comma-separated list of allowed domain names. If present, Splash won’t load anything neither from domains not in this list nor from subdomains of domains not in this list.} @@ -21,8 +23,6 @@ execute_lua(splash_obj, lua_source, timeout = 30, allowed_domains, proxy, \item{save_args}{A list of argument names to put in cache.} \item{load_args}{Parameter values to load from cache} - -\item{lua_sourc}{Browser automation script. See \href{http://splash.readthedocs.io/en/stable/scripting-tutorial.html#scripting-tutorial}{Splash Script} Tutorial for more info.} } \value{ \code{raw} content from the \code{httr} call. Given the vast diversity of possible return values, it's up to the caller to handle the return value. diff --git a/man/get_content_type.Rd b/man/get_content_type.Rd index db6f412..4672e6b 100644 --- a/man/get_content_type.Rd +++ b/man/get_content_type.Rd @@ -43,10 +43,10 @@ is_svg(har_resp_obj) is_gif(har_resp_obj) -is_xhr(x) +is_xhr(har_resp_obj) } \arguments{ -\item{har_resp_obj}{HAR response object} +\item{har_resp_obj}{a reponse object from [render_har]() or [execute_lua]()} \item{type}{content type to compare to (default: "\code{application/json}")} } diff --git a/man/render_file.Rd b/man/render_file.Rd index 6056807..1e72ea4 100644 --- a/man/render_file.Rd +++ b/man/render_file.Rd @@ -4,7 +4,7 @@ \alias{render_file} \title{Return the HTML or image (png) of the javascript-rendered page in a local file} \usage{ -render_file(splash_obj = slpash_local, file_path, output = c("html", "png"), +render_file(splash_obj = splash_local, file_path, output = c("html", "png"), wait = 0, viewport = "1024x768", ...) } \arguments{ @@ -14,6 +14,8 @@ render_file(splash_obj = slpash_local, file_path, output = c("html", "png"), \item{output}{either \code{html} or \code{png} to get the page content or an image capture} +\item{wait}{seconds to wait} + \item{viewport}{View width and height (in pixels) of the browser viewport to render the web page. Format is "\code{x}". e.g. 800x600. Default value is 1024x768.} \item{...}{other params to \url{render_html} or \url{render_png}} diff --git a/man/render_har.Rd b/man/render_har.Rd index 1a60744..8554402 100644 --- a/man/render_har.Rd +++ b/man/render_har.Rd @@ -54,7 +54,7 @@ render_har(splash_obj = splash_local, url, base_url, response_body = FALSE, \item{load_args}{Parameter values to load from cache} } \value{ -a \link[HARtools:har]{HARtools::har} object +a \code{HARtools} \code{har} object } \description{ It includes information about requests made, responses received, timings, headers, etc and diff --git a/man/render_png.Rd b/man/render_png.Rd index f36ada8..f175977 100644 --- a/man/render_png.Rd +++ b/man/render_png.Rd @@ -4,8 +4,8 @@ \alias{render_png} \title{Return a image (in PNG format) of the javascript-rendered page.} \usage{ -render_png(splash_obj = splash_local, url, base_url = NULL, quality = 75, - width = 1024, height = 768, timeout = 30, resource_timeout, wait = 0, +render_png(splash_obj = splash_local, url, base_url = NULL, width = 1024, + height = 768, timeout = 30, resource_timeout, wait = 0, render_all = FALSE, proxy, js, js_src, filters, allowed_domains, allowed_content_types, forbidden_content_types, viewport = "1024x768", images, headers, body, http_method, save_args, load_args) diff --git a/man/stop_splash.Rd b/man/stop_splash.Rd index 85a163a..8f08062 100644 --- a/man/stop_splash.Rd +++ b/man/stop_splash.Rd @@ -7,8 +7,6 @@ stop_splash(splash_container) } \arguments{ -\item{splash_container}{saved Splash container id from [start_splash]} - \item{splash_container}{Docker `container` object created by [start_splash]} } \description{