R/gi_available_templates.R

Defines functions gi_available_templates

Documented in gi_available_templates

#' Fetch available templates from gitignore.io
#'
#' This return list of all templates supported by gitignore.io.
#'
#' @details The returned list is returned as lower case characters.
#'
#' @return A character with all templates supported by gitignore.io.
#' @export
#'
#' @examplesIf curl::has_internet()
#' gi_available_templates()
gi_available_templates <-
  function() {
    url <- glue::glue("{backend_url()}/api/list?format=json")
    res <- curl::curl_fetch_memory(url)

    if (res[["status_code"]] != 200L) {
      cli::cli_abort("http request failed with status code: {res$status_code}")
    }

    json_text <- rawToChar(res[["content"]])

    if (!jsonlite::validate(json_text)) {
      cli::cli_abort(
        "Invalid json file returned in gi_available_templates() function."
      )
    }

    r <- jsonlite::fromJSON(json_text)

    templates <- tolower(sort(purrr::map_chr(r, "name")))
    unname(templates)
  }
ropensci/gitignore documentation built on Nov. 2, 2024, 9:53 p.m.