Browse Source

response object helpers

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 @@
1 1
 Package: splashr
2 2
 Type: Package
3 3
 Title: Tools to Work with the 'Splash' JavaScript Rendering Service
4
-Version: 0.1.0
5
-Date: 2017-02-03
4
+Version: 0.2.0
5
+Date: 2017-02-14
6 6
 Encoding: UTF-8
7 7
 Author: Bob Rudis (bob@rud.is)
8 8
 Maintainer: Bob Rudis <bob@rud.is>

+ 16
- 0
NAMESPACE View File

@@ -6,7 +6,23 @@ export("%>%")
6 6
 export(HARviewer)
7 7
 export(HARviewerOutput)
8 8
 export(execute_lua)
9
+export(get_body_size)
10
+export(get_content_size)
11
+export(get_content_type)
12
+export(get_headers_size)
9 13
 export(install_splash)
14
+export(is_binary)
15
+export(is_content_type)
16
+export(is_css)
17
+export(is_gif)
18
+export(is_html)
19
+export(is_javascript)
20
+export(is_jpeg)
21
+export(is_json)
22
+export(is_plain)
23
+export(is_png)
24
+export(is_svg)
25
+export(is_xml)
10 26
 export(renderHARviewer)
11 27
 export(render_file)
12 28
 export(render_har)

+ 53
- 0
R/docker.r View File

@@ -0,0 +1,53 @@
1
+#' Retrieve the Docker image for Splash
2
+#'
3
+#' @md
4
+#' @param host Docker host; defauolts to `localhost`
5
+#' @return `harbor` `host` object
6
+#' @export
7
+#' @examples \dontrun{
8
+#' install_splash()
9
+#' splash_container <- start_splash()
10
+#' stop_splash(splash_container)
11
+#' }
12
+install_splash <- function(host = harbor::localhost) {
13
+  harbor::docker_pull(host, "scrapinghub/splash")
14
+}
15
+
16
+#' Start a Splash server Docker container
17
+#'
18
+#' @param host Docker host; defauolts to `localhost`
19
+#' @note you need Docker running on your system and have pulled the container with
20
+#'       [install_spash] for this to work. You should save the resultant `host`
21
+#'       object for use in [stop_splash].
22
+#' @return `harbor` `container` object
23
+#' @export
24
+#' @examples \dontrun{
25
+#' install_splash()
26
+#' splash_container <- start_splash()
27
+#' stop_splash(splash_container)
28
+#' }
29
+start_splash <- function(host = harbor::localhost) {
30
+  harbor::docker_run(host,
31
+                     image = "scrapinghub/splash",
32
+                     detach = TRUE,
33
+                     docker_opts = c("-p", "5023:5023",
34
+                                     "-p", "8050:8050",
35
+                                     "-p", "8051:8051"))
36
+}
37
+
38
+#' Stop a running a Splash server Docker container
39
+#'
40
+#' @param splash_container saved Splash container id from [start_splash]
41
+#' @param splash_container Docker `container` object created by [start_splash]
42
+#' @note you need Docker running on your system and have pulled the container with
43
+#'       [install_spash] and started the Splash container with [start_splash] for this
44
+#'       to work. You will need the `container` object from [start_splash] for this to work.
45
+#' @export
46
+#' @examples \dontrun{
47
+#' install_splash()
48
+#' splash_container <- start_splash()
49
+#' stop_splash(splash_container)
50
+#' }
51
+stop_splash <- function(splash_container) {
52
+  harbor::container_rm(splash_container, force=TRUE)
53
+}

+ 92
- 0
R/helpers.r View File

@@ -0,0 +1,92 @@
1
+#' Retrieve or test content type of a HAR request object
2
+#'
3
+#' @param har_resp_obj HAR response object
4
+#' @export
5
+get_content_type <- function(har_resp_obj) {
6
+  ctype <- har_resp_obj$response$content$mimeType
7
+  if (ctype == "") return(NA_character_)
8
+  if (any(grepl(";", ctype))) ctype <- gsub(";.*$", "", ctype)
9
+  ctype
10
+}
11
+
12
+#' @md
13
+#' @rdname get_content_type
14
+#' @param type content type to compare to (default: "`application/json`")
15
+#' @export
16
+is_content_type <- function(har_resp_obj, type="application/json") {
17
+  get_content_type(har_resp_obj) == type
18
+}
19
+
20
+#' @rdname get_content_type
21
+#' @export
22
+is_json <- function(har_resp_obj) { is_content_type(har_resp_obj) }
23
+
24
+#' @rdname get_content_type
25
+#' @export
26
+is_xml <- function(har_resp_obj) {  is_content_type(har_resp_obj, type="application/xml") }
27
+
28
+#' @rdname get_content_type
29
+#' @export
30
+is_css <- function(har_resp_obj) {  is_content_type(har_resp_obj, type="text/css") }
31
+
32
+#' @rdname get_content_type
33
+#' @export
34
+is_plain <- function(har_resp_obj) {  is_content_type(har_resp_obj, type="text/plain") }
35
+
36
+#' @rdname get_content_type
37
+#' @export
38
+is_binary <- function(har_resp_obj) {  is_content_type(har_resp_obj, type="application/octet-stream") }
39
+
40
+#' @rdname get_content_type
41
+#' @export
42
+is_javascript <- function(har_resp_obj) {
43
+  is_content_type(har_resp_obj, type="text/javascript") |
44
+    is_content_type(har_resp_obj, type="text/x-javascript")
45
+}
46
+
47
+#' @rdname get_content_type
48
+#' @export
49
+is_html <- function(har_resp_obj) {  is_content_type(har_resp_obj, type="text/html") }
50
+
51
+#' @rdname get_content_type
52
+#' @export
53
+is_jpeg <- function(har_resp_obj) {  is_content_type(har_resp_obj, type="image/jpeg") }
54
+
55
+#' @rdname get_content_type
56
+#' @export
57
+is_png <- function(har_resp_obj) {  is_content_type(har_resp_obj, type="image/png") }
58
+
59
+#' @rdname get_content_type
60
+#' @export
61
+is_svg <- function(har_resp_obj) {  is_content_type(har_resp_obj, type="image/svg+xml") }
62
+
63
+#' @rdname get_content_type
64
+#' @export
65
+is_gif <- function(har_resp_obj) {  is_content_type(har_resp_obj, type="image/gif") }
66
+
67
+#' Retrieve size of content |  body | headers
68
+#'
69
+#' @param har_resp_obj HAR response object
70
+#' @export
71
+get_content_size <- function(har_resp_obj) {
72
+  csize <- har_resp_obj$response$content$size
73
+  if (is.null(csize)) return(NA_real_)
74
+  return(as.numeric(csize))
75
+}
76
+
77
+#' @rdname get_content_size
78
+#' @export
79
+get_body_size <- function(har_resp_obj) {
80
+  bsize <- har_resp_obj$response$bodySize
81
+  if (is.null(bsize)) return(NA_real_)
82
+  return(as.numeric(bsize))
83
+}
84
+
85
+#' @rdname get_content_size
86
+#' @export
87
+get_headers_size <- function(har_resp_obj) {
88
+  hsize <- har_resp_obj$response$headersSize
89
+  if (is.null(hsize)) return(NA_real_)
90
+  return(as.numeric(hsize))
91
+}
92
+

+ 19
- 0
README.Rmd View File

@@ -49,6 +49,25 @@ The following functions are implemented:
49 49
 - `start_splash`:	Start a Splash server Docker container
50 50
 - `stop_splash`:	Stop a running a Splash server Docker container
51 51
 
52
+Helpers:
53
+
54
+- `get_body_size`:	Retrieve size of content | body | headers
55
+- `get_content_sie`:	Retrieve size of content | body | headers
56
+- `get_content_type`	Retrieve or test content type of a HAR request object
57
+- `get_headers_size`	Retrieve size of content | body | headers
58
+- `is_binary`:	Retrieve or test content type of a HAR request object
59
+- `is_content_type`:	Retrieve or test content type of a HAR request object
60
+- `is_css`:	Retrieve or test content type of a HAR request object
61
+- `is_gif`:	Retrieve or test content type of a HAR request object
62
+- `is_html`:	Retrieve or test content type of a HAR request object
63
+- `is_javascript`:	Retrieve or test content type of a HAR request object
64
+- `is_jpeg`:	Retrieve or test content type of a HAR request object
65
+- `is_json`:	Retrieve or test content type of a HAR request object
66
+- `is_plain`:	Retrieve or test content type of a HAR request object
67
+- `is_png`:	Retrieve or test content type of a HAR request object
68
+- `is_svg`:	Retrieve or test content type of a HAR request object
69
+- `is_xml`:	Retrieve or test content type of a HAR request object
70
+
52 71
 Some functions from `HARtools` are imported/exported and `%>%` is imported/exported.
53 72
 
54 73
 ### TODO

+ 21
- 2
README.md View File

@@ -46,6 +46,25 @@ The following functions are implemented:
46 46
 -   `start_splash`: Start a Splash server Docker container
47 47
 -   `stop_splash`: Stop a running a Splash server Docker container
48 48
 
49
+Helpers:
50
+
51
+-   `get_body_size`:	Retrieve size of content | body | headers
52
+-   `get_content_sie`:	Retrieve size of content | body | headers
53
+-   `get_content_type`	Retrieve or test content type of a HAR request object
54
+-   `get_headers_size`	Retrieve size of content | body | headers
55
+-   `is_binary`:	Retrieve or test content type of a HAR request object
56
+-   `is_content_type`:	Retrieve or test content type of a HAR request object
57
+-   `is_css`:	Retrieve or test content type of a HAR request object
58
+-   `is_gif`:	Retrieve or test content type of a HAR request object
59
+-   `is_html`:	Retrieve or test content type of a HAR request object
60
+-   `is_javascript`:	Retrieve or test content type of a HAR request object
61
+-   `is_jpeg`:	Retrieve or test content type of a HAR request object
62
+-   `is_json`:	Retrieve or test content type of a HAR request object
63
+-   `is_plain`:	Retrieve or test content type of a HAR request object
64
+-   `is_png`:	Retrieve or test content type of a HAR request object
65
+-   `is_svg`:	Retrieve or test content type of a HAR request object
66
+-   `is_xml`:	Retrieve or test content type of a HAR request object
67
+
49 68
 Some functions from `HARtools` are imported/exported and `%>%` is imported/exported.
50 69
 
51 70
 ### TODO
@@ -85,7 +104,7 @@ library(tidyverse)
85 104
 packageVersion("splashr")
86 105
 ```
87 106
 
88
-    ## [1] '0.1.0'
107
+    ## [1] '0.2.0'
89 108
 
90 109
 ``` r
91 110
 splash("splash", 8050L) %>%
@@ -258,7 +277,7 @@ library(testthat)
258 277
 date()
259 278
 ```
260 279
 
261
-    ## [1] "Fri Feb 10 14:02:35 2017"
280
+    ## [1] "Tue Feb 14 09:02:35 2017"
262 281
 
263 282
 ``` r
264 283
 test_dir("tests/")

+ 20
- 0
man/get_content_size.Rd View File

@@ -0,0 +1,20 @@
1
+% Generated by roxygen2: do not edit by hand
2
+% Please edit documentation in R/helpers.r
3
+\name{get_content_size}
4
+\alias{get_content_size}
5
+\alias{get_body_size}
6
+\alias{get_headers_size}
7
+\title{Retrieve size of content |  body | headers}
8
+\usage{
9
+get_content_size(har_resp_obj)
10
+
11
+get_body_size(har_resp_obj)
12
+
13
+get_headers_size(har_resp_obj)
14
+}
15
+\arguments{
16
+\item{har_resp_obj}{HAR response object}
17
+}
18
+\description{
19
+Retrieve size of content |  body | headers
20
+}

+ 52
- 0
man/get_content_type.Rd View File

@@ -0,0 +1,52 @@
1
+% Generated by roxygen2: do not edit by hand
2
+% Please edit documentation in R/helpers.r
3
+\name{get_content_type}
4
+\alias{get_content_type}
5
+\alias{is_content_type}
6
+\alias{is_json}
7
+\alias{is_xml}
8
+\alias{is_css}
9
+\alias{is_plain}
10
+\alias{is_binary}
11
+\alias{is_javascript}
12
+\alias{is_html}
13
+\alias{is_jpeg}
14
+\alias{is_png}
15
+\alias{is_svg}
16
+\alias{is_gif}
17
+\title{Retrieve or test content type of a HAR request object}
18
+\usage{
19
+get_content_type(har_resp_obj)
20
+
21
+is_content_type(har_resp_obj, type = "application/json")
22
+
23
+is_json(har_resp_obj)
24
+
25
+is_xml(har_resp_obj)
26
+
27
+is_css(har_resp_obj)
28
+
29
+is_plain(har_resp_obj)
30
+
31
+is_binary(har_resp_obj)
32
+
33
+is_javascript(har_resp_obj)
34
+
35
+is_html(har_resp_obj)
36
+
37
+is_jpeg(har_resp_obj)
38
+
39
+is_png(har_resp_obj)
40
+
41
+is_svg(har_resp_obj)
42
+
43
+is_gif(har_resp_obj)
44
+}
45
+\arguments{
46
+\item{har_resp_obj}{HAR response object}
47
+
48
+\item{type}{content type to compare to (default: "\code{application/json}")}
49
+}
50
+\description{
51
+Retrieve or test content type of a HAR request object
52
+}

+ 15
- 1
man/install_splash.Rd View File

@@ -1,19 +1,28 @@
1 1
 % Generated by roxygen2: do not edit by hand
2
-% Please edit documentation in R/docker-splash.r
2
+% Please edit documentation in R/docker-splash.r, R/docker.r
3 3
 \name{install_splash}
4 4
 \alias{install_splash}
5
+\alias{install_splash}
5 6
 \title{Retrieve the Docker image for Splash}
6 7
 \usage{
7 8
 install_splash(host = harbor::localhost)
9
+
10
+install_splash(host = harbor::localhost)
8 11
 }
9 12
 \arguments{
10 13
 \item{host}{Docker host; defaults to \code{localhost}}
14
+
15
+\item{host}{Docker host; defauolts to \code{localhost}}
11 16
 }
12 17
 \value{
13 18
 \code{harbor} \code{host} object
19
+
20
+\code{harbor} \code{host} object
14 21
 }
15 22
 \description{
16 23
 Retrieve the Docker image for Splash
24
+
25
+Retrieve the Docker image for Splash
17 26
 }
18 27
 \examples{
19 28
 \dontrun{
@@ -21,4 +30,9 @@ install_splash()
21 30
 splash_container <- start_splash()
22 31
 stop_splash(splash_container)
23 32
 }
33
+\dontrun{
34
+install_splash()
35
+splash_container <- start_splash()
36
+stop_splash(splash_container)
37
+}
24 38
 }

+ 20
- 2
man/start_splash.Rd View File

@@ -1,10 +1,13 @@
1 1
 % Generated by roxygen2: do not edit by hand
2
-% Please edit documentation in R/docker-splash.r
2
+% Please edit documentation in R/docker-splash.r, R/docker.r
3 3
 \name{start_splash}
4 4
 \alias{start_splash}
5
+\alias{start_splash}
5 6
 \title{Start a Splash server Docker container}
6 7
 \usage{
7
-start_splash(host = harbor::localhost, add_tempdir = FALSE)
8
+start_splash(host = harbor::localhost)
9
+
10
+start_splash(host = harbor::localhost)
8 11
 }
9 12
 \arguments{
10 13
 \item{host}{Docker host; defauolts to `localhost`}
@@ -15,18 +18,28 @@ If `TRUE` then a local temporary directory (made with [tempdir]())
15 18
 will be added to the mount configuration for use with [render_file](). You will need to
16 19
     ensure the necessary system temp dirs are accessible as a mounts. For
17 20
     macOS this means adding `/private` to said Docker config.}
21
+
22
+\item{host}{Docker host; defauolts to `localhost`}
18 23
 }
19 24
 \value{
20 25
 `harbor` `container` object
26
+
27
+`harbor` `container` object
21 28
 }
22 29
 \description{
23 30
 If using this in an automation context, you should consider adding a
24 31
 `Sys.sleep(3)` (or higher) after starting the docker container.
32
+
33
+Start a Splash server Docker container
25 34
 }
26 35
 \note{
27 36
 you need Docker running on your system and have pulled the container with
28 37
       [install_spash] for this to work. You should save the resultant `host`
29 38
       object for use in [stop_splash].
39
+
40
+you need Docker running on your system and have pulled the container with
41
+      [install_spash] for this to work. You should save the resultant `host`
42
+      object for use in [stop_splash].
30 43
 }
31 44
 \examples{
32 45
 \dontrun{
@@ -34,4 +47,9 @@ install_splash()
34 47
 splash_container <- start_splash()
35 48
 stop_splash(splash_container)
36 49
 }
50
+\dontrun{
51
+install_splash()
52
+splash_container <- start_splash()
53
+stop_splash(splash_container)
54
+}
37 55
 }

+ 19
- 1
man/stop_splash.Rd View File

@@ -1,23 +1,36 @@
1 1
 % Generated by roxygen2: do not edit by hand
2
-% Please edit documentation in R/docker-splash.r
2
+% Please edit documentation in R/docker-splash.r, R/docker.r
3 3
 \name{stop_splash}
4 4
 \alias{stop_splash}
5
+\alias{stop_splash}
5 6
 \title{Stop a running a Splash server Docker container}
6 7
 \usage{
7 8
 stop_splash(splash_container)
9
+
10
+stop_splash(splash_container)
8 11
 }
9 12
 \arguments{
10 13
 \item{splash_container}{saved Splash container id from [start_splash]}
11 14
 
12 15
 \item{splash_container}{Docker `container` object created by [start_splash]}
16
+
17
+\item{splash_container}{saved Splash container id from [start_splash]}
18
+
19
+\item{splash_container}{Docker `container` object created by [start_splash]}
13 20
 }
14 21
 \description{
15 22
 Stop a running a Splash server Docker container
23
+
24
+Stop a running a Splash server Docker container
16 25
 }
17 26
 \note{
18 27
 you need Docker running on your system and have pulled the container with
19 28
       [install_spash] and started the Splash container with [start_splash] for this
20 29
       to work. You will need the `container` object from [start_splash] for this to work.
30
+
31
+you need Docker running on your system and have pulled the container with
32
+      [install_spash] and started the Splash container with [start_splash] for this
33
+      to work. You will need the `container` object from [start_splash] for this to work.
21 34
 }
22 35
 \examples{
23 36
 \dontrun{
@@ -25,4 +38,9 @@ install_splash()
25 38
 splash_container <- start_splash()
26 39
 stop_splash(splash_container)
27 40
 }
41
+\dontrun{
42
+install_splash()
43
+splash_container <- start_splash()
44
+stop_splash(splash_container)
45
+}
28 46
 }

Loading…
Cancel
Save