Skip to content

Commit

Permalink
Merge pull request #316 from bcgov/issue-315
Browse files Browse the repository at this point in the history
Change download_audience default to NULL and quote facet values for bcdc_search() (#315)
  • Loading branch information
stephhazlitt authored Mar 11, 2023
2 parents d5f2434 + 448c7d5 commit 52dce66
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 17 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ Imports:
purrr (>= 0.3),
readr (>= 2.1),
readxl (>= 1.4.0),
jsonlite (>= 1.8.4),
rlang (>= 1.0),
sf (>= 1.0),
tidyselect (>= 1.1.0),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# bcdata (development version)

* Add `jsonlite::read_json()` as a file read method, so users can now download & read `json` resources in B.C. Data Catalogue records
* Change the `download_audience` default from `Public` to `NULL` in `bcdc_search()` (#315)
* Fix bug where some/all facet values in `bcdc_search()` need to be quoted to generate a valid API query (#315)

# bcdata 0.4.0

Expand Down
42 changes: 28 additions & 14 deletions R/bcdc_search.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
#' @examples
#' \donttest{
#' try(
#' bcdc_search_facets("download_audience")
#' )
#'
#' try(
#' bcdc_search_facets("res_format")
#' )
#' }
Expand Down Expand Up @@ -129,7 +133,7 @@ bcdc_list <- function() {
#' @param ... search terms
#' @param license_id the type of license (see `bcdc_search_facets("license_id")`).
#' @param download_audience download audience
#' (see `bcdc_search_facets("download_audience")`). Default `"Public"`
#' (see `bcdc_search_facets("download_audience")`). Default `NULL` (all audiences).
#' @param res_format format of resource (see `bcdc_search_facets("res_format")`)
#' @param sector sector of government from which the data comes
#' (see `bcdc_search_facets("sector")`)
Expand All @@ -151,8 +155,8 @@ bcdc_list <- function() {
#' )
#' }
bcdc_search <- function(..., license_id = NULL,
download_audience = "Public",
res_format=NULL,
download_audience = NULL,
res_format = NULL,
sector = NULL,
organization = NULL,
n = 100) {
Expand All @@ -168,17 +172,27 @@ bcdc_search <- function(..., license_id = NULL,
organization = organization
))

lapply(names(facets), function(x) {
facet_vals <- bcdc_search_facets(x)
if (!facets[x] %in% facet_vals$name) {
stop(facets[x], " is not a valid value for ", x,
call. = FALSE)
}
})

query <- paste0(
terms, ifelse(nzchar(terms), "+", ""),
paste(names(facets), facets, sep = ":", collapse = "+"))
# build query by collating the terms and any user supplied facets
# if there are no supplied facets (e.g., is_empty(facets) returns TRUE) just use terms)
query <- if (is_empty(facets)) {
paste0(terms)
} else {
#check that the facet values are valid
lapply(names(facets), function(x) {
facet_vals <- bcdc_search_facets(x)
if (!facets[x] %in% facet_vals$name) {
stop(facets[x], " is not a valid value for ", x,
call. = FALSE)
}
})

paste0(terms, "+", paste(
names(facets),
paste0("\"", facets, "\""),
sep = ":",
collapse = "+"
))
}

query <- gsub("\\s+", "%20", query)

Expand Down
4 changes: 4 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -448,3 +448,7 @@ names_to_lazy_tbl <- function(x) {
frame <- as.data.frame(stats::setNames(rep(list(logical()), length(x)), x))
dbplyr::tbl_lazy(frame)
}

is_empty <- function(x) {
length(x) == 0
}
4 changes: 2 additions & 2 deletions man/bcdc_search.Rd

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

4 changes: 4 additions & 0 deletions man/bcdc_search_facets.Rd

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

0 comments on commit 52dce66

Please sign in to comment.