R/write_constants.R

Defines functions write_constants

Documented in write_constants

#' Constants to zzz
#'
#' @export
#' @param template_path (character) path to your yaml template file. by default,
#' we use a demo template yaml file
#' @param outfile (character) Path to write your functions to
#' @param base_url (character) optional. if not given, use base url from spec.
#' you can give it here and it will override anything in the spec
#' @return returns silently, after writing things to zzz.R
write_constants <- function(template_path = NULL, outfile = "zzz.R",
  base_url = NULL) {
  
  spec <- load_spec(template_path)
  if ("raml" %in% names(spec)) {
    stop("not ready yet")
  } else if (any(c("swagger", "openapi") %in% names(spec))) {
    if ("servers" %in% names(spec)) {
      bu <- unlist(spec$servers, FALSE)$url
    } else {
      bu <- sprintf("%s://%s%s", spec$schemes, spec$host, spec$basePath)
    }
  } else {
    bu <- spec$baseurl
  }
  url <- base_url %||% bu
  url <- sub("/$", "", url)
  cat(sprintf("\n\nbase_url <- function() \"%s\"", url),
      file = outfile, append = TRUE)
}
ropenscilabs/apipkgen documentation built on Sept. 3, 2020, 5:30 a.m.