adlib_build_query <- function()

in R/adlib.R [121:202]


adlib_build_query <- function(ad_reached_countries,
                              ad_active_status = c("ALL", "ACTIVE", "INACTIVE"),
                              ad_delivery_date_max = NULL,
                              ad_delivery_date_min = NULL,
                              ad_type = c(
                                "POLITICAL_AND_ISSUE_ADS", "HOUSING_ADS",
                                "NEWS_ADS", "UNCATEGORIZED_ADS", "ALL"
                              ),
                              bylines = NULL,
                              delivery_by_region = NULL,
                              potential_reach_max = NULL,
                              potential_reach_min = NULL,
                              publisher_platform = "FACEBOOK",
                              search_page_ids = NULL,
                              search_terms = NULL,
                              limit = 1000,
                              fields = "ad_data") {
  ad_active_status <- match.arg(ad_active_status)
  ad_type <- match.arg(ad_type)

  if (length(search_page_ids) > 10) {
    stop("Can only search 10 page IDs at a time.")
  }

  if (is.null(search_page_ids) & is.null(search_terms)) {
    stop("At least one of search_page_ids or search_terms must be supplied.")
  }

  if (!is.null(potential_reach_max) && !(potential_reach_max %in% POTENTIAL_REACH_MAX_VALUES)) {
    if (potential_reach_max < 1000) {
      stop("potential_reach_max must be at least 1000.")
    } else if (potential_reach_max > 1000000) {
      stop("potential_reach_max can be at most 1,000,000.
           Leave potential_reach_max as NULL to accept any potential reach.")
    } else {
      potential_reach_max <- POTENTIAL_REACH_MAX_VALUES[which(sort(c(POTENTIAL_REACH_MAX_VALUES, potential_reach_max)) == potential_reach_max) - 1]
      warning(glue::glue("potential_reach_max must be one of {paste(as.character(POTENTIAL_REACH_MAX_VALUES), collapse = ', ')}.\n Rounding down to {potential_reach_max}."))
    }
  }

  if (!is.null(potential_reach_min) && !(potential_reach_min %in% POTENTIAL_REACH_MIN_VALUES)) {
    if (potential_reach_min < 100) {
      stop("potential_reach_min must be at least 100.")
    } else if (potential_reach_min > 1000000) {
      stop("potential_reach_min can be at most 1,000,000.")
    } else {
      potential_reach_min <- POTENTIAL_REACH_MIN_VALUES[which(sort(c(POTENTIAL_REACH_MIN_VALUES, potential_reach_min)) == potential_reach_min)]
      warning(glue::glue("potential_reach_min must be one of {paste(as.character(POTENTIAL_REACH_MIN_VALUES), collapse = ', ')}.\n Rounding up to {potential_reach_min}."))
    }
  }

  if (!is.null(potential_reach_max) && !is.null(potential_reach_min) && potential_reach_max <= potential_reach_min) {
    stop("potential_reach_min must be less than potential_reach_max") # the API won't let them be equal
  }

  ad_reached_countries <- format_array(ad_reached_countries)
  if (!is.null(bylines)) bylines <- format_array(bylines)
  if (!is.null(search_page_ids)) {
    search_page_ids <- format_array(search_page_ids)
  }
  if (!is.null(delivery_by_region)) delivery_by_region <- format_array(delivery_by_region)
  publisher_platform <- format_array(publisher_platform)
  fields <- adlib_fields(fields)


  return(list(
    ad_active_status = ad_active_status,
    ad_reached_countries = ad_reached_countries,
    ad_delivery_date_max = ad_delivery_date_max,
    ad_delivery_date_min = ad_delivery_date_min,
    ad_type = ad_type,
    bylines = bylines,
    delivery_by_region = delivery_by_region,
    potential_reach_max = as.integer(potential_reach_max),
    potential_reach_min = as.integer(potential_reach_min),
    publisher_platform = publisher_platform,
    search_page_ids = search_page_ids,
    search_terms = search_terms,
    fields = fields,
    limit = limit
  ))
}