Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial path #19

Merged
merged 6 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
Package: rapid
Title: R 'API' Documents
Title: R 'API' Definitions
Version: 0.0.0.9000
Authors@R:
Authors@R: c(
person("Jon", "Harmon", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-4781-4346"))
comment = c(ORCID = "0000-0003-4781-4346")),
person("The Linux Foundation", role = "cph",
comment = "OpenAPI Specification")
)
Description: Convert an 'API' document ('APID'), such as one that follows
the 'OpenAPI Specification', to an R object.
the 'OpenAPI Specification', to an R 'API' definition object (a
"rapid"). The rapid object follows the 'OpenAPI Specification' to
make it easy to convert to and from 'API' documents.
License: MIT + file LICENSE
URL: https://jonthegeek.github.io/rapid/,
https://github.com/jonthegeek/rapid
BugReports: https://github.com/jonthegeek/rapid/issues
Imports:
rlang (>= 1.1.0),
stbl
Suggests:
testthat (>= 3.0.0)
Remotes:
jonthegeek/stbl
Config/testthat/edition: 3
Config/testthat/parallel: true
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
8 changes: 8 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
# Generated by roxygen2: do not edit by hand

S3method(.add_class,default)
S3method(.add_class,list)
export(api_contact)
export(api_license)
importFrom(rlang,check_dots_empty)
importFrom(rlang,check_dots_used)
importFrom(stbl,stabilize_chr_scalar)
importFrom(stbl,to_chr_scalar)
54 changes: 54 additions & 0 deletions R/info-contact.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#' Contact information for the API
#'
#' Validate the contact information for an API.
#'
#' @param name The identifying name of the contact person/organization.
#' @param url The URL pointing to the contact information. This *must* be in the
#' form of a URL.
#' @param email The email address of the contact person/organization. This
#' *must* be in the form of an email address.
#'
#' @return A `rapid_contact` object: a list with fields `name`, `url`, and
#' `email`.
#' @export
#'
#' @examples
#' api_contact(
#' "API Support",
#' "https://www.example.com/support",
#' "[email protected]"
#' )
api_contact <- function(name = character(),
url = character(),
email = character()) {
name <- to_chr_scalar(name)
url <- stabilize_chr_scalar(
url,
regex = "http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+"
)
email <- stabilize_chr_scalar(
email,
regex = "^[^@]+@[^@]+$"
)

return(
.new_rapid_contact(
name = name,
url = url,
email = email
)
)
}

.new_rapid_contact <- function(name, url, email) {
return(
.add_class(
list(
name = name,
url = url,
email = email
),
new_class = "rapid_contact"
)
)
}
57 changes: 57 additions & 0 deletions R/info-license.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#' License information for the API
#'
#' Validate the license information for an API.
#'
#' @inheritParams rlang::args_dots_empty
#' @param name The license name used for the API.
#' @param identifier An
#' [SPDX](https://spdx.org/spdx-specification-21-web-version#h.jxpfx0ykyb60)
#' license expression for the API. The `identifier` field is mutually
#' exclusive of the `url` field.
#' @param url A URL to the license used for the API. This *must* be in the form
#' of a URL. The `url` field is mutually exclusive of the `identifier` field.
#'
#' @return A `rapid_license` object: a list with fields `name`, identifier, and
#' `url`.
#' @export
#'
#' @examples
#' api_license(
#' "Apache 2.0",
#' identifier = "Apache-2.0"
#' )
#' api_license(
#' "Apache 2.0",
#' url = "https://opensource.org/license/apache-2-0/"
#' )
api_license <- function(name, ..., identifier = NULL, url = NULL) {
name <- to_chr_scalar(name)
rlang::check_dots_empty()
rlang::check_exclusive(identifier, url)
identifier <- to_chr_scalar(identifier)
url <- stabilize_chr_scalar(
url,
regex = "http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+"
)

return(
.new_rapid_license(
name = name,
identifier = identifier,
url = url
)
)
}

.new_rapid_license <- function(name, identifier = NULL, url = NULL) {
return(
.add_class(
list(
name = name,
identifier = identifier,
url = url
),
new_class = "rapid_license"
)
)
}
53 changes: 53 additions & 0 deletions R/info.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# rapid_info <- function(title = character(),
# version = character(),
# ...,
# contact = NULL,
# description = character(),
# license = NULL,
# summary = character(), # New in 3.1
# terms_of_service = NULL) {
# # TODO: Um. These things aren't vectors. They're lists with special
# # properties. I don't think the things in vctrs make sense. new_rcrd is close
# # but these will NOT have the same lengths within their fields in almost all
# # cases. Think through what the implications are.
# check_dots_empty()
# title <- to_chr_scalar(title)
# version <- to_chr_scalar(version)
# description <- to_chr_scalar(description)
# summary <- to_chr_scalar(summary)
# terms_of_service <- to_chr_scalar(terms_of_service)
#
# .new_info(
# title = title,
# version = version,
# contact = contact,
# description = description,
# license = license,
# summary = summary,
# terms_of_service = terms_of_service
# )
# }
#
# .new_info <- function(title,
# version,
# summary,
# description,
# terms_of_service,
# contact,
# license,
# ...) {
# vctrs::new_vctr(
# list(
# title = title,
# version = version,
# summary = summary,
# description = description,
# terms_of_service = terms_of_service,
# contact = contact,
# license = license,
# ...
# ),
# # Attributes?
# class = "rapid_info"
# )
# }
13 changes: 13 additions & 0 deletions R/oas.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# blah <- function(x) {
# yaml::read_yaml(
# x,
# # Don't let read_yaml try to guess formats. Sometimes something is wrong and
# # it breaks the whole thing. The document is telling us formats, so we can
# # apply them once we have them.
# handlers = list(
# int = as.character,
# float = as.character,
# bool = as.character
# )
# )
# }
15 changes: 15 additions & 0 deletions R/objects.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.add_class <- function(x, new_class, ...) {
check_dots_used()
UseMethod(".add_class")
}

#' @export
.add_class.list <- function(x, new_class, ...) {
class(x) <- unique(c(new_class, class(x)))
return(x)
}

#' @export
.add_class.default <- function(x, ...) {
stop("I haven't implemented this for anything but list.") # nocov
}
4 changes: 4 additions & 0 deletions R/rapid-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@
"_PACKAGE"

## usethis namespace: start
#' @importFrom rlang check_dots_empty
#' @importFrom rlang check_dots_used
#' @importFrom stbl stabilize_chr_scalar
#' @importFrom stbl to_chr_scalar
## usethis namespace: end
NULL
23 changes: 23 additions & 0 deletions R/rapid.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#' rapid <- function(info = rapid_info()) {
#' .new_rapid(info = info)
#' }
#'
#' .new_rapid <- function(info) {
#' vctrs::new_vctr(
#' list(
#' info = info
#' ),
#' class = "rapid",
#' inherit_base_type = TRUE
#' )
#' }
#'
#'
#'
#' #' @export
#' vec_ptype_abbr.rapid <- function(x, ...) {
#' "rapid"
#' }
#'
#' # for compatibility with the S4 system
#' methods::setOldClass("rapid", "vctrs_vctr")
Loading