Skip to content

Instantly share code, notes, and snippets.

@randrescastaneda
Created June 7, 2023 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randrescastaneda/ee2ca99e5b311f13c947088bfe5e1edc to your computer and use it in GitHub Desktop.
Save randrescastaneda/ee2ca99e5b311f13c947088bfe5e1edc to your computer and use it in GitHub Desktop.
Parse PIP API endpoints documentation to text file
library(data.table)
url <- "https://raw.githubusercontent.com/PIP-Technical-Team/pipapi/master/inst/plumber/v1/endpoints.R"
lns <- readLines(con = url) |>
{\(.) .[grep("\\@(get|param)", .)]}()
dt <- data.table(lines = lns)
dt[grep("\\@get", lines),
endpoint := gsub("(.+)/([^/]+)$", "\\2", lines)
][,
# fill in NAs
endpoint := vctrs::vec_fill_missing(endpoint)
][
# get parameters
grep("\\@param", lines),
param := gsub("(.+param )(.+):(.*)", "\\2", lines)
][
# get type
grep("\\@param", lines),
type := gsub("(.+):\\[(.*)\\](.+)", "\\2", lines)]
# get onl y params rows
dl <- dt[!grepl("\\@get", lines)
][,
lines := NULL] |>
split(by = "endpoint", keep.by = FALSE)
in_yml <- ymlthis::as_yml(dl)
in_json <- jsonlite::toJSON(dl, pretty = TRUE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment