Browse Source

response object helpers

master
boB Rudis 3 years ago
parent
commit
5b3457fbbf
11 changed files with 329 additions and 8 deletions
  1. +2
    -2
      DESCRIPTION
  2. +16
    -0
      NAMESPACE
  3. +53
    -0
      R/docker.r
  4. +92
    -0
      R/helpers.r
  5. +19
    -0
      README.Rmd
  6. +21
    -2
      README.md
  7. +20
    -0
      man/get_content_size.Rd
  8. +52
    -0
      man/get_content_type.Rd
  9. +15
    -1
      man/install_splash.Rd
  10. +20
    -2
      man/start_splash.Rd
  11. +19
    -1
      man/stop_splash.Rd

+ 2
- 2
DESCRIPTION View File

@@ -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 <bob@rud.is>


+ 16
- 0
NAMESPACE View File

@@ -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)


+ 53
- 0
R/docker.r View File

@@ -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)
}

+ 92
- 0
R/helpers.r View File

@@ -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))
}


+ 19
- 0
README.Rmd View File

@@ -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


+ 21
- 2
README.md View File

@@ -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/")


+ 20
- 0
man/get_content_size.Rd View File

@@ -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
}

+ 52
- 0
man/get_content_type.Rd View File

@@ -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
}

+ 15
- 1
man/install_splash.Rd View File

@@ -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)
}
}

+ 20
- 2
man/start_splash.Rd View File

@@ -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)
}
}

+ 19
- 1
man/stop_splash.Rd View File

@@ -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)
}
}

Loading…
Cancel
Save