Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Depends:
R (>= 3.1)
Imports:
httr
RoxygenNote: 7.1.2
RoxygenNote: 7.3.1
Suggests: knitr,
dplyr,
rmarkdown,
Expand Down
22 changes: 11 additions & 11 deletions R/bulk_postcode_lookup.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
#' @importFrom httr POST
#' @importFrom utils URLencode
#'
#' @param postcodes Accepts a list of postcodes. Accepts up to 100 postcodes.
#' @param ... One or more vectors containing postcodes to look up. Up to 100 postcodes are accepted.
#' For only one postcode use \code{\link{postcode_lookup}}.
#'
#' @return A list of length one.
#' @return A list
#' @seealso \code{\link{postcode_lookup}} for documentation.
#'
#' @export
#'
#' @examples
#' \donttest{
#' pc_list <- list(
#' postcodes = c("PR3 0SG", "M45 6GN", "EX165BL")) # spaces are ignored
#' bulk_postcode_lookup(pc_list)
#' # The function needs a list of length one. This won't work:
#' bulk_postcode_lookup(list("PR3 0SG", "M45 6GN", "EX165BL"))
#' ## Postcodes can be provided as individual arguments
#' bulk_postcode_lookup("PR30SG", "M456GN", "EX165BL")
#' ## Or as one or more vectors:
#' bulk_postcode_lookup(c("PR30SG", "M456GN"), "EX165BL")
#' }
#'
bulk_postcode_lookup <- function(postcodes) {
bulk_postcode_lookup <- function(...) {
dots <- unlist(c(...), recursive = TRUE)
postcodes <- list(postcodes = dots)
check_list_limit(postcodes)
postcodes <- lapply(postcodes, URLencode)
r <- POST("https://api.postcodes.io/postcodes",
Expand All @@ -35,8 +35,8 @@ check_list_limit <- function(x) {
if (!is.list(x))
stop("Please provide a list with postcodes.")
if (length(x) == 0)
stop("Please provide a list with more than one postcode")
stop("Please provide more than one postcode")
count <- sum(sapply(x, length))
if (count > 100)
stop("Please provide a list with less than 100 postcodes.")
stop("Please provide fewer than 100 postcodes.")
}
16 changes: 7 additions & 9 deletions man/bulk_postcode_lookup.Rd

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

8 changes: 8 additions & 0 deletions tests/testthat/test-bulk_postcode_lookup.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ test_that("bulk_postcode_lookup works as expected", {
expect_error(bulk_postcode_lookup(pc_list))
expect_that(lookup_results, is_a("list"))
})

test_that("bulk_postcode_lookup_accepts_multiple_vectors", {
# Don't run these tests on the CRAN build servers
skip_on_cran()

expect_no_error(bulk_postcode_lookup("PR30SG", "M456GN", "EX165BL"))
expect_no_error(bulk_postcode_lookup("PR30SG", c("M456GN", "EX165BL")))
})