From 5b3457fbbf16c4e441008069e31a6ffce8496c9e Mon Sep 17 00:00:00 2001 From: boB Rudis Date: Tue, 14 Feb 2017 09:17:19 -0500 Subject: [PATCH] response object helpers --- DESCRIPTION | 4 +-- NAMESPACE | 16 +++++++++ R/docker.r | 53 ++++++++++++++++++++++++++++ R/helpers.r | 92 +++++++++++++++++++++++++++++++++++++++++++++++++ README.Rmd | 19 ++++++++++ README.md | 23 +++++++++++-- man/get_content_size.Rd | 20 +++++++++++ man/get_content_type.Rd | 52 ++++++++++++++++++++++++++++ man/install_splash.Rd | 16 ++++++++- man/start_splash.Rd | 22 ++++++++++-- man/stop_splash.Rd | 20 ++++++++++- 11 files changed, 329 insertions(+), 8 deletions(-) create mode 100644 R/docker.r create mode 100644 R/helpers.r create mode 100644 man/get_content_size.Rd create mode 100644 man/get_content_type.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 5c5200f..6523e59 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: splashr Type: Package Title: Tools to Work with the 'Splash' JavaScript Rendering Service -Version: 0.1.0 -Date: 2017-02-03 +Version: 0.2.0 +Date: 2017-02-14 Encoding: UTF-8 Author: Bob Rudis (bob@rud.is) Maintainer: Bob Rudis diff --git a/NAMESPACE b/NAMESPACE index 1f6f1f2..17806b9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,7 +6,23 @@ export("%>%") export(HARviewer) export(HARviewerOutput) export(execute_lua) +export(get_body_size) +export(get_content_size) +export(get_content_type) +export(get_headers_size) export(install_splash) +export(is_binary) +export(is_content_type) +export(is_css) +export(is_gif) +export(is_html) +export(is_javascript) +export(is_jpeg) +export(is_json) +export(is_plain) +export(is_png) +export(is_svg) +export(is_xml) export(renderHARviewer) export(render_file) export(render_har) diff --git a/R/docker.r b/R/docker.r new file mode 100644 index 0000000..406539a --- /dev/null +++ b/R/docker.r @@ -0,0 +1,53 @@ +#' Retrieve the Docker image for Splash +#' +#' @md +#' @param host Docker host; defauolts to `localhost` +#' @return `harbor` `host` object +#' @export +#' @examples \dontrun{ +#' install_splash() +#' splash_container <- start_splash() +#' stop_splash(splash_container) +#' } +install_splash <- function(host = harbor::localhost) { + harbor::docker_pull(host, "scrapinghub/splash") +} + +#' Start a Splash server Docker container +#' +#' @param host Docker host; defauolts to `localhost` +#' @note you need Docker running on your system and have pulled the container with +#' [install_spash] for this to work. You should save the resultant `host` +#' object for use in [stop_splash]. +#' @return `harbor` `container` object +#' @export +#' @examples \dontrun{ +#' install_splash() +#' splash_container <- start_splash() +#' stop_splash(splash_container) +#' } +start_splash <- function(host = harbor::localhost) { + harbor::docker_run(host, + image = "scrapinghub/splash", + detach = TRUE, + docker_opts = c("-p", "5023:5023", + "-p", "8050:8050", + "-p", "8051:8051")) +} + +#' 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 +#' to work. You will need the `container` object from [start_splash] for this to work. +#' @export +#' @examples \dontrun{ +#' install_splash() +#' splash_container <- start_splash() +#' stop_splash(splash_container) +#' } +stop_splash <- function(splash_container) { + harbor::container_rm(splash_container, force=TRUE) +} diff --git a/R/helpers.r b/R/helpers.r new file mode 100644 index 0000000..dff33a3 --- /dev/null +++ b/R/helpers.r @@ -0,0 +1,92 @@ +#' 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 + if (ctype == "") return(NA_character_) + if (any(grepl(";", ctype))) ctype <- gsub(";.*$", "", ctype) + ctype +} + +#' @md +#' @rdname get_content_type +#' @param type content type to compare to (default: "`application/json`") +#' @export +is_content_type <- function(har_resp_obj, type="application/json") { + get_content_type(har_resp_obj) == type +} + +#' @rdname get_content_type +#' @export +is_json <- function(har_resp_obj) { is_content_type(har_resp_obj) } + +#' @rdname get_content_type +#' @export +is_xml <- function(har_resp_obj) { is_content_type(har_resp_obj, type="application/xml") } + +#' @rdname get_content_type +#' @export +is_css <- function(har_resp_obj) { is_content_type(har_resp_obj, type="text/css") } + +#' @rdname get_content_type +#' @export +is_plain <- function(har_resp_obj) { is_content_type(har_resp_obj, type="text/plain") } + +#' @rdname get_content_type +#' @export +is_binary <- function(har_resp_obj) { is_content_type(har_resp_obj, type="application/octet-stream") } + +#' @rdname get_content_type +#' @export +is_javascript <- function(har_resp_obj) { + is_content_type(har_resp_obj, type="text/javascript") | + is_content_type(har_resp_obj, type="text/x-javascript") +} + +#' @rdname get_content_type +#' @export +is_html <- function(har_resp_obj) { is_content_type(har_resp_obj, type="text/html") } + +#' @rdname get_content_type +#' @export +is_jpeg <- function(har_resp_obj) { is_content_type(har_resp_obj, type="image/jpeg") } + +#' @rdname get_content_type +#' @export +is_png <- function(har_resp_obj) { is_content_type(har_resp_obj, type="image/png") } + +#' @rdname get_content_type +#' @export +is_svg <- function(har_resp_obj) { is_content_type(har_resp_obj, type="image/svg+xml") } + +#' @rdname get_content_type +#' @export +is_gif <- function(har_resp_obj) { is_content_type(har_resp_obj, type="image/gif") } + +#' Retrieve size of content | body | headers +#' +#' @param har_resp_obj HAR response object +#' @export +get_content_size <- function(har_resp_obj) { + csize <- har_resp_obj$response$content$size + if (is.null(csize)) return(NA_real_) + return(as.numeric(csize)) +} + +#' @rdname get_content_size +#' @export +get_body_size <- function(har_resp_obj) { + bsize <- har_resp_obj$response$bodySize + if (is.null(bsize)) return(NA_real_) + return(as.numeric(bsize)) +} + +#' @rdname get_content_size +#' @export +get_headers_size <- function(har_resp_obj) { + hsize <- har_resp_obj$response$headersSize + if (is.null(hsize)) return(NA_real_) + return(as.numeric(hsize)) +} + diff --git a/README.Rmd b/README.Rmd index ce7a757..8c8e5b5 100644 --- a/README.Rmd +++ b/README.Rmd @@ -49,6 +49,25 @@ The following functions are implemented: - `start_splash`: Start a Splash server Docker container - `stop_splash`: Stop a running a Splash server Docker container +Helpers: + +- `get_body_size`: Retrieve size of content | body | headers +- `get_content_sie`: Retrieve size of content | body | headers +- `get_content_type` Retrieve or test content type of a HAR request object +- `get_headers_size` Retrieve size of content | body | headers +- `is_binary`: Retrieve or test content type of a HAR request object +- `is_content_type`: Retrieve or test content type of a HAR request object +- `is_css`: Retrieve or test content type of a HAR request object +- `is_gif`: Retrieve or test content type of a HAR request object +- `is_html`: Retrieve or test content type of a HAR request object +- `is_javascript`: Retrieve or test content type of a HAR request object +- `is_jpeg`: Retrieve or test content type of a HAR request object +- `is_json`: Retrieve or test content type of a HAR request object +- `is_plain`: Retrieve or test content type of a HAR request object +- `is_png`: Retrieve or test content type of a HAR request object +- `is_svg`: Retrieve or test content type of a HAR request object +- `is_xml`: Retrieve or test content type of a HAR request object + Some functions from `HARtools` are imported/exported and `%>%` is imported/exported. ### TODO diff --git a/README.md b/README.md index a517c26..5027128 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,25 @@ The following functions are implemented: - `start_splash`: Start a Splash server Docker container - `stop_splash`: Stop a running a Splash server Docker container +Helpers: + +- `get_body_size`: Retrieve size of content | body | headers +- `get_content_sie`: Retrieve size of content | body | headers +- `get_content_type` Retrieve or test content type of a HAR request object +- `get_headers_size` Retrieve size of content | body | headers +- `is_binary`: Retrieve or test content type of a HAR request object +- `is_content_type`: Retrieve or test content type of a HAR request object +- `is_css`: Retrieve or test content type of a HAR request object +- `is_gif`: Retrieve or test content type of a HAR request object +- `is_html`: Retrieve or test content type of a HAR request object +- `is_javascript`: Retrieve or test content type of a HAR request object +- `is_jpeg`: Retrieve or test content type of a HAR request object +- `is_json`: Retrieve or test content type of a HAR request object +- `is_plain`: Retrieve or test content type of a HAR request object +- `is_png`: Retrieve or test content type of a HAR request object +- `is_svg`: Retrieve or test content type of a HAR request object +- `is_xml`: Retrieve or test content type of a HAR request object + Some functions from `HARtools` are imported/exported and `%>%` is imported/exported. ### TODO @@ -85,7 +104,7 @@ library(tidyverse) packageVersion("splashr") ``` - ## [1] '0.1.0' + ## [1] '0.2.0' ``` r splash("splash", 8050L) %>% @@ -258,7 +277,7 @@ library(testthat) date() ``` - ## [1] "Fri Feb 10 14:02:35 2017" + ## [1] "Tue Feb 14 09:02:35 2017" ``` r test_dir("tests/") diff --git a/man/get_content_size.Rd b/man/get_content_size.Rd new file mode 100644 index 0000000..b101942 --- /dev/null +++ b/man/get_content_size.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/helpers.r +\name{get_content_size} +\alias{get_content_size} +\alias{get_body_size} +\alias{get_headers_size} +\title{Retrieve size of content | body | headers} +\usage{ +get_content_size(har_resp_obj) + +get_body_size(har_resp_obj) + +get_headers_size(har_resp_obj) +} +\arguments{ +\item{har_resp_obj}{HAR response object} +} +\description{ +Retrieve size of content | body | headers +} diff --git a/man/get_content_type.Rd b/man/get_content_type.Rd new file mode 100644 index 0000000..767ca77 --- /dev/null +++ b/man/get_content_type.Rd @@ -0,0 +1,52 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/helpers.r +\name{get_content_type} +\alias{get_content_type} +\alias{is_content_type} +\alias{is_json} +\alias{is_xml} +\alias{is_css} +\alias{is_plain} +\alias{is_binary} +\alias{is_javascript} +\alias{is_html} +\alias{is_jpeg} +\alias{is_png} +\alias{is_svg} +\alias{is_gif} +\title{Retrieve or test content type of a HAR request object} +\usage{ +get_content_type(har_resp_obj) + +is_content_type(har_resp_obj, type = "application/json") + +is_json(har_resp_obj) + +is_xml(har_resp_obj) + +is_css(har_resp_obj) + +is_plain(har_resp_obj) + +is_binary(har_resp_obj) + +is_javascript(har_resp_obj) + +is_html(har_resp_obj) + +is_jpeg(har_resp_obj) + +is_png(har_resp_obj) + +is_svg(har_resp_obj) + +is_gif(har_resp_obj) +} +\arguments{ +\item{har_resp_obj}{HAR response object} + +\item{type}{content type to compare to (default: "\code{application/json}")} +} +\description{ +Retrieve or test content type of a HAR request object +} diff --git a/man/install_splash.Rd b/man/install_splash.Rd index cd32992..00afc7a 100644 --- a/man/install_splash.Rd +++ b/man/install_splash.Rd @@ -1,19 +1,28 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/docker-splash.r +% Please edit documentation in R/docker-splash.r, R/docker.r \name{install_splash} \alias{install_splash} +\alias{install_splash} \title{Retrieve the Docker image for Splash} \usage{ install_splash(host = harbor::localhost) + +install_splash(host = harbor::localhost) } \arguments{ \item{host}{Docker host; defaults to \code{localhost}} + +\item{host}{Docker host; defauolts to \code{localhost}} } \value{ \code{harbor} \code{host} object + +\code{harbor} \code{host} object } \description{ Retrieve the Docker image for Splash + +Retrieve the Docker image for Splash } \examples{ \dontrun{ @@ -21,4 +30,9 @@ install_splash() splash_container <- start_splash() stop_splash(splash_container) } +\dontrun{ +install_splash() +splash_container <- start_splash() +stop_splash(splash_container) +} } diff --git a/man/start_splash.Rd b/man/start_splash.Rd index d345572..54c84a1 100644 --- a/man/start_splash.Rd +++ b/man/start_splash.Rd @@ -1,10 +1,13 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/docker-splash.r +% Please edit documentation in R/docker-splash.r, R/docker.r \name{start_splash} \alias{start_splash} +\alias{start_splash} \title{Start a Splash server Docker container} \usage{ -start_splash(host = harbor::localhost, add_tempdir = FALSE) +start_splash(host = harbor::localhost) + +start_splash(host = harbor::localhost) } \arguments{ \item{host}{Docker host; defauolts to `localhost`} @@ -15,18 +18,28 @@ If `TRUE` then a local temporary directory (made with [tempdir]()) will be added to the mount configuration for use with [render_file](). You will need to ensure the necessary system temp dirs are accessible as a mounts. For macOS this means adding `/private` to said Docker config.} + +\item{host}{Docker host; defauolts to `localhost`} } \value{ `harbor` `container` object + +`harbor` `container` object } \description{ If using this in an automation context, you should consider adding a `Sys.sleep(3)` (or higher) after starting the docker container. + +Start a Splash server Docker container } \note{ you need Docker running on your system and have pulled the container with [install_spash] for this to work. You should save the resultant `host` object for use in [stop_splash]. + +you need Docker running on your system and have pulled the container with + [install_spash] for this to work. You should save the resultant `host` + object for use in [stop_splash]. } \examples{ \dontrun{ @@ -34,4 +47,9 @@ install_splash() splash_container <- start_splash() stop_splash(splash_container) } +\dontrun{ +install_splash() +splash_container <- start_splash() +stop_splash(splash_container) +} } diff --git a/man/stop_splash.Rd b/man/stop_splash.Rd index 85a163a..89f2c80 100644 --- a/man/stop_splash.Rd +++ b/man/stop_splash.Rd @@ -1,23 +1,36 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/docker-splash.r +% Please edit documentation in R/docker-splash.r, R/docker.r \name{stop_splash} \alias{stop_splash} +\alias{stop_splash} \title{Stop a running a Splash server Docker container} \usage{ stop_splash(splash_container) + +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]} + +\item{splash_container}{saved Splash container id from [start_splash]} + +\item{splash_container}{Docker `container` object created by [start_splash]} } \description{ Stop a running a Splash server Docker container + +Stop a running a Splash server Docker container } \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 to work. You will need the `container` object from [start_splash] for this to work. + +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 + to work. You will need the `container` object from [start_splash] for this to work. } \examples{ \dontrun{ @@ -25,4 +38,9 @@ install_splash() splash_container <- start_splash() stop_splash(splash_container) } +\dontrun{ +install_splash() +splash_container <- start_splash() +stop_splash(splash_container) +} }