Skip to content

Commit

Permalink
Added filter_third_party_resources as a parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
IronistM committed Nov 9, 2017
1 parent 376cb82 commit 6d19a44
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
15 changes: 9 additions & 6 deletions R/speed-insights-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
#' @param strategy Whether the URL should be evaluated in a mobile or desktop context. Accordingly, acceptable values
#' are either the string "mobile" or the string "desktop".
#' @param key A unique key obtainable from Google by registering for free as a Google developer.
#' @param filter_third_party_resources A boolean to indicate if third party resources should be filtered out before PageSpeed analysis. (Default: FALSE)
#' @examples
#' \dontrun{
#' speedfinder("https://www.cars.com","mobile",key)
#' }

#' @export
speedfinder <- function(url,strategy,key) {
pid <- RJSONIO::fromJSON(paste0("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=",url,"&strategy=",strategy,"&key=",key))
speedfinder <- function(url,strategy,key,filter_third_party_resources=FALSE) {
pid <- RJSONIO::fromJSON(paste0("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=",url,"&strategy=",strategy,"&key=",key,"&filter_third_party_resources=",filter_third_party_resources))
frame1 <- cbind(as.data.frame(pid[2]),as.data.frame(pid[3]),as.data.frame(pid[5]),as.data.frame(pid[6]))
rbind.data.frame(data.frame(), frame1,make.row.names=FALSE)
}
Expand All @@ -26,10 +27,11 @@ speedfinder <- function(url,strategy,key) {
#' @param strategy Whether the list of URLs should be evaluated in a mobile or desktop context. Accordingly, acceptable values
#' are either the string "mobile" or the string "desktop".
#' @param key A unique key obtainable from Google by registering for free as a Google developer.
#' @param filter_third_party_resources A boolean to indicate if third party resources should be filtered out before PageSpeed analysis. (Default: FALSE)

#' @export
speedfinder2 <- function(url,strategy,key) {
pid <- RJSONIO::fromJSON(paste0("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=",url,"&strategy=",strategy,"&key=",key))
speedfinder2 <- function(url,strategy,key,filter_third_party_resources=FALSE) {
pid <- RJSONIO::fromJSON(paste0("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=",url,"&strategy=",strategy,"&key=",key,"&filter_third_party_resources=",filter_third_party_resources))
frame1 <- cbind(as.data.frame(pid[2]),as.data.frame(pid[3]),as.data.frame(pid[5]),as.data.frame(pid[6]))
}

Expand All @@ -42,14 +44,15 @@ speedfinder2 <- function(url,strategy,key) {
#' @param strategy Whether the list of URLs should be evaluated in a mobile or desktop context. Accordingly, acceptable values
#' are either the string "mobile" or the string "desktop".
#' @param key A unique key obtainable from Google by registering for free as a Google developer.
#' @param filter_third_party_resources A boolean to indicate if third party resources should be filtered out before PageSpeed analysis. (Default: FALSE)
#' @examples
#' \dontrun{
#' speedlist(listofURLs,"mobile",key)
#' }

#' @export
speedlist <- function(pagelist,strategy,key) {
list1 <- lapply(pagelist,speedfinder2,strategy,key)
speedlist <- function(pagelist,strategy,key,filter_third_party_resources=FALSE) {
list1 <- lapply(pagelist,speedfinder2,strategy,key,filter_third_party_resources)
suppressWarnings(do.call(gtools::smartbind,list1))
}

Expand Down
4 changes: 3 additions & 1 deletion man/speedfinder.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/speedfinder2.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/speedlist.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions tests/testthat/testthat.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ context("gpagespeed functions")

test_check("gpagespeed")

gspeed_key = 'YOUR_API_KEY'

test_that("speedfinder function", {
expect_equal(class(speedfinder("https://www.cars.com","mobile",key)),
expect_equal(class(speedfinder("https://www.cars.com","mobile",key=gspeed_key,filter_third_party_resources = TRUE)),
class(data.frame()))
expect_equal(nrow(speedfinder("https://www.cars.com","mobile",key)), 1)
expect_equal(nrow(speedfinder("https://www.cars.com","mobile",key=gspeed_key)), 1)
})

test_that("speedlist function", {
expect_equal(class(speedlist(c("https://www.cars.com","https://www.yahoo.com","https://www.techmeme.com"),
"mobile",key)),
"mobile",key=gspeed_key)),
class(data.frame()))
expect_equal(nrow(speedlist(c("https://www.cars.com","https://www.yahoo.com","https://www.techmeme.com"),
"mobile",key)), 3)
"mobile",key=gspeed_key)), 3)
})

0 comments on commit 6d19a44

Please sign in to comment.