From 8a13590a54feba4f87ea798d86d0ea04884f00b8 Mon Sep 17 00:00:00 2001 From: David Lovell Date: Tue, 5 Mar 2024 11:57:39 +0000 Subject: [PATCH 1/3] adding support for dots --- R/bulk_postcode_lookup.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/bulk_postcode_lookup.R b/R/bulk_postcode_lookup.R index d327005..032ea0b 100644 --- a/R/bulk_postcode_lookup.R +++ b/R/bulk_postcode_lookup.R @@ -22,7 +22,9 @@ #' bulk_postcode_lookup(list("PR3 0SG", "M45 6GN", "EX165BL")) #' } #' -bulk_postcode_lookup <- function(postcodes) { +bulk_postcode_lookup <- function(...) { + dots <- unlist(c(...), recursive = TRUE) + postcodes <- list(postcodes = ...) check_list_limit(postcodes) postcodes <- lapply(postcodes, URLencode) r <- POST("https://api.postcodes.io/postcodes", From 54f348625189de1b56fa80cf6de25563802127d1 Mon Sep 17 00:00:00 2001 From: David Lovell Date: Tue, 5 Mar 2024 11:58:45 +0000 Subject: [PATCH 2/3] updating documentation, bulk lookup does not return a list of length one --- R/bulk_postcode_lookup.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/bulk_postcode_lookup.R b/R/bulk_postcode_lookup.R index 032ea0b..9c80a9f 100644 --- a/R/bulk_postcode_lookup.R +++ b/R/bulk_postcode_lookup.R @@ -8,7 +8,7 @@ #' @param postcodes Accepts a list of postcodes. Accepts up to 100 postcodes. #' 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 @@ -24,7 +24,7 @@ #' bulk_postcode_lookup <- function(...) { dots <- unlist(c(...), recursive = TRUE) - postcodes <- list(postcodes = ...) + postcodes <- list(postcodes = dots) check_list_limit(postcodes) postcodes <- lapply(postcodes, URLencode) r <- POST("https://api.postcodes.io/postcodes", From 8a5781ec6a1d24d31715a530fb736f5b82893718 Mon Sep 17 00:00:00 2001 From: David Lovell Date: Tue, 5 Mar 2024 12:26:30 +0000 Subject: [PATCH 3/3] updating documentation and error messages --- DESCRIPTION | 2 +- R/bulk_postcode_lookup.R | 16 +++++++--------- man/bulk_postcode_lookup.Rd | 16 +++++++--------- tests/testthat/test-bulk_postcode_lookup.R | 8 ++++++++ 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c3aa375..e64d564 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -23,7 +23,7 @@ Depends: R (>= 3.1) Imports: httr -RoxygenNote: 7.1.2 +RoxygenNote: 7.3.1 Suggests: knitr, dplyr, rmarkdown, diff --git a/R/bulk_postcode_lookup.R b/R/bulk_postcode_lookup.R index 9c80a9f..b1e073d 100644 --- a/R/bulk_postcode_lookup.R +++ b/R/bulk_postcode_lookup.R @@ -5,7 +5,7 @@ #' @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 @@ -15,13 +15,11 @@ #' #' @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(...) { dots <- unlist(c(...), recursive = TRUE) postcodes <- list(postcodes = dots) @@ -37,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.") } diff --git a/man/bulk_postcode_lookup.Rd b/man/bulk_postcode_lookup.Rd index b45e682..4e1e1e6 100644 --- a/man/bulk_postcode_lookup.Rd +++ b/man/bulk_postcode_lookup.Rd @@ -4,27 +4,25 @@ \alias{bulk_postcode_lookup} \title{Bulk postcode lookup} \usage{ -bulk_postcode_lookup(postcodes) +bulk_postcode_lookup(...) } \arguments{ -\item{postcodes}{Accepts a list of postcodes. Accepts up to 100 postcodes. +\item{...}{One or more vectors containing postcodes to look up. Up to 100 postcodes are accepted. For only one postcode use \code{\link{postcode_lookup}}.} } \value{ -A list of length one. +A list } \description{ Returns a list of matching postcodes and respective available data. } \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") } - } \seealso{ \code{\link{postcode_lookup}} for documentation. diff --git a/tests/testthat/test-bulk_postcode_lookup.R b/tests/testthat/test-bulk_postcode_lookup.R index ad51084..2043807 100644 --- a/tests/testthat/test-bulk_postcode_lookup.R +++ b/tests/testthat/test-bulk_postcode_lookup.R @@ -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"))) +})