R/utils.R

Defines functions is_congress_id is_congress_number is_chamber as_congress_number ppc_response_text ppc_response_parsed

#' Paste operator
#'
#' See \code{tfse::\link[tfse]{\%P\%}} for details.
#'
#' @name %P%
#' @rdname P
#' @keywords internal
#' @export
#' @importFrom tfse %P%
#' @usage lhs \%P\% rhs
NULL


`%||%` <- function (a, b) if (is.null(a)) b else a

is_congress_id <- function(x) length(x) == 1 && is.character(x) && grepl("^[A-Z]\\d+$", x)

## validation function for congress number
is_congress_number <- function(x) {
  is.atomic(x) && length(x) == 1 && grepl("^1\\d{2}(th|st|rd|nd)?$", x)
}

## validation function for chamber
is_chamber <- function(x) {
  is.character(x) && tolower(x[1]) %in% c("h", "s", "house", "senate", "b", "both", "sen", "rep")
}


as_congress_number <- function(x) {
  sub("(?<=\\d)[[:alpha:]]+$", "", x, perl = TRUE)
}

ppc_response_text <- function(x) {
  if ("content" %in% names(x)) {
    x <- x[["content"]]
  }
  paste(rawToChar(x, multiple = TRUE), collapse = "")
}

ppc_response_parsed <- function(x) {
  jsonlite::fromJSON(ppc_response_text(x))
}
mkearney/ppc documentation built on Dec. 18, 2019, 5:40 a.m.