Skip to content
This repository has been archived by the owner on Oct 10, 2022. It is now read-only.

Commit

Permalink
Added pastes
Browse files Browse the repository at this point in the history
  • Loading branch information
Steph Locke committed Mar 24, 2016
1 parent 017b12f commit f1763f5
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: HIBPwned
Title: Bindings for the HaveIBeenPwned.com data breach API
Version: 0.0.0.9006
Version: 0.0.0.9007
Authors@R: c(person("Steph", "Locke"
, email = "stephanie.g.locke@gmail.com"
, role = c("aut", "cre"))
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export(account_breaches)
export(breached_site)
export(breached_sites)
export(data_classes)
export(pastes)
importFrom(httr,add_headers)
importFrom(httr,content)
importFrom(jsonlite,fromJSON)
Expand Down
27 changes: 27 additions & 0 deletions R/pastes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#' Search for cases where an email address has been included in
#' a paste
#'
#' @param emails A character vector of email addresses
#' @param ... Optional passthrough to HIBP_headers()
#'
#' @return List of data.frames containing results
#' @export
#'
#' @examples
#' pastes(c("steff.sullivan@gmail.com","stephanie.g.locke@gmail.com"))
pastes<-function(
emails
,...){

if(length(emails)==0|!inherits(emails,"character")) stop("Problematic emails")

encoded<-urltools::url_encode(emails)
URLS<-paste0("https://haveibeenpwned.com/api/pasteaccount/"
,encoded)

res<-lapply(URLS, GETcontent, HIBP_headers(...))
names(res)<-emails

return(res)

}
25 changes: 25 additions & 0 deletions man/pastes.Rd

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

48 changes: 48 additions & 0 deletions tests/testthat/test-pastes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
context("pastes")

test_that("pastes works for a single account",{

acct<-"steff.sullivan@gmail.com"

# Simplest usage scenario
res<-pastes(acct)

expect_is(res, "list")
expect_named(res, acct)
expect_equal(length(res),1)
expect_is(res[[acct]],"data.frame")
expect_gte(ncol(res[[acct]]),1)
expect_gte(nrow(res[[acct]]),1)

# With header passthrough
expect_error(pastes(acct,agent="blah"),NA)
})

test_that("pastes works for multiple accounts",{

acct<-c("steff.sullivan@gmail.com","stephanie.g.locke@gmail.com")

# Simplest usage scenario
res<-pastes(acct)

expect_is(res, "list")
expect_named(res, acct)
expect_equal(length(res),length(acct))
for(i in 1:length(acct)){
a<-acct[i]
expect_is(res[[a]],"data.frame")
expect_gte(ncol(res[[a]]),1)
expect_gte(nrow(res[[a]]),1)
}

# With header passthrough
expect_error(pastes(acct,agent="blah"),NA)
})

test_that("handles incorrect values",{
errors<-"Problematic emails"

expect_error(pastes(TRUE),errors)
expect_error(pastes(as.character(c())),errors)

})

0 comments on commit f1763f5

Please sign in to comment.