Skip to content

Commit

Permalink
lint: all files
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnCoene committed Oct 9, 2024
1 parent 3f0bc59 commit a6a91b0
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 116 deletions.
216 changes: 108 additions & 108 deletions R/ambiorix.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ Ambiorix <- R6::R6Class(
not_found = NULL,
error = NULL,
on_stop = NULL,
#' @details Define the webserver.
#'
#' @param host A string defining the host.
#' @param port Integer defining the port, defaults to `ambiorix.port` option: uses a random port if `NULL`.
#' @param log Whether to generate a log of events.
#' @details Define the webserver.
#'
#' @param host A string defining the host.
#' @param port Integer defining the port, defaults to `ambiorix.port` option: uses a random port if `NULL`.
#' @param log Whether to generate a log of events.
initialize = function(
host = getOption("ambiorix.host", "0.0.0.0"),
port = getOption("ambiorix.port", NULL),
Expand All @@ -68,91 +68,91 @@ Ambiorix <- R6::R6Class(
.globals$cache_tmpls <- TRUE
invisible(self)
},
#' @details Specifies the port to listen on.
#' @param port Port number.
#'
#' @examples
#' app <- Ambiorix$new()
#'
#' app$listen(3000L)
#'
#' app$get("/", function(req, res){
#' res$send("Using {ambiorix}!")
#' })
#'
#' if(interactive())
#' app$start()
#' @details Specifies the port to listen on.
#' @param port Port number.
#'
#' @examples
#' app <- Ambiorix$new()
#'
#' app$listen(3000L)
#'
#' app$get("/", function(req, res){
#' res$send("Using {ambiorix}!")
#' })
#'
#' if(interactive())
#' app$start()
listen = function(port){
assert_that(not_missing(port))
private$.port <- as.integer(port)
invisible(self)
},
#' @details Sets the 404 page.
#' @param handler Function that accepts the request and returns an object
#' describing an httpuv response, e.g.: [response()].
#'
#' @examples
#' app <- Ambiorix$new()
#'
#' app$set_404(function(req, res){
#' res$send("Nothing found here")
#' })
#'
#' app$get("/", function(req, res){
#' res$send("Using {ambiorix}!")
#' })
#'
#' if(interactive())
#' app$start()
#' @details Sets the 404 page.
#' @param handler Function that accepts the request and returns an object
#' describing an httpuv response, e.g.: [response()].
#'
#' @examples
#' app <- Ambiorix$new()
#'
#' app$set_404(function(req, res){
#' res$send("Nothing found here")
#' })
#'
#' app$get("/", function(req, res){
#' res$send("Using {ambiorix}!")
#' })
#'
#' if(interactive())
#' app$start()
set_404 = function(handler){
assert_that(not_missing(handler))
assert_that(is_handler(handler))
self$not_found <- handler
invisible(self)
},
#' @details Sets the error handler.
#' @param handler Function that accepts a request, response and an error object.
#'
#' @examples
#' # my custom error handler:
#' error_handler <- \(req, res, error) {
#' if (!is.null(error)) {
#' error_msg <- conditionMessage(error)
#' cli::cli_alert_danger("Error: {error_msg}")
#' }
#' response <- list(
#' code = 500L,
#' msg = "Uhhmmm... Looks like there's an error from our side :("
#' )
#' res$
#' set_status(500L)$
#' json(response)
#' }
#'
#' # handler for GET at /whoami:
#' whoami <- \(req, res) {
#' # simulate error (object 'Pikachu' is not defined)
#' print(Pikachu)
#' }
#'
#' app <- Ambiorix$
#' new()$
#' set_error(error_handler)$
#' get("/whoami", whoami)
#'
#' if (interactive()) {
#' app$start(open = FALSE)
#' }
#' @details Sets the error handler.
#' @param handler Function that accepts a request, response and an error object.
#'
#' @examples
#' # my custom error handler:
#' error_handler <- \(req, res, error) {
#' if (!is.null(error)) {
#' error_msg <- conditionMessage(error)
#' cli::cli_alert_danger("Error: {error_msg}")
#' }
#' response <- list(
#' code = 500L,
#' msg = "Uhhmmm... Looks like there's an error from our side :("
#' )
#' res$
#' set_status(500L)$
#' json(response)
#' }
#'
#' # handler for GET at /whoami:
#' whoami <- \(req, res) {
#' # simulate error (object 'Pikachu' is not defined)
#' print(Pikachu)
#' }
#'
#' app <- Ambiorix$
#' new()$
#' set_error(error_handler)$
#' get("/whoami", whoami)
#'
#' if (interactive()) {
#' app$start(open = FALSE)
#' }
set_error = function(handler) {
assert_that(not_missing(handler))
assert_that(is_error_handler(handler))
self$error <- handler
invisible(self)
},
#' @details Static directories
#'
#' @param path Local path to directory of assets.
#' @param uri URL path where the directory will be available.
#' @details Static directories
#'
#' @param path Local path to directory of assets.
#' @param uri URL path where the directory will be available.
static = function(path, uri = "www"){
assert_that(not_missing(uri))
assert_that(not_missing(path))
Expand All @@ -162,21 +162,21 @@ Ambiorix <- R6::R6Class(
private$.static <- append(private$.static, lst)
invisible(self)
},
#' @details Start
#' Start the webserver.
#' @param host A string defining the host.
#' @param port Integer defining the port, defaults to `ambiorix.port` option: uses a random port if `NULL`.
#' @param open Whether to open the app the browser.
#'
#' @examples
#' app <- Ambiorix$new()
#'
#' app$get("/", function(req, res){
#' res$send("Using {ambiorix}!")
#' })
#'
#' if(interactive())
#' app$start(port = 3000L)
#' @details Start
#' Start the webserver.
#' @param host A string defining the host.
#' @param port Integer defining the port, defaults to `ambiorix.port` option: uses a random port if `NULL`.
#' @param open Whether to open the app the browser.
#'
#' @examples
#' app <- Ambiorix$new()
#'
#' app$get("/", function(req, res){
#' res$send("Using {ambiorix}!")
#' })
#'
#' if(interactive())
#' app$start(port = 3000L)
start = function(
port = NULL,
host = NULL,
Expand Down Expand Up @@ -262,30 +262,30 @@ Ambiorix <- R6::R6Class(

invisible(self)
},
#' @details Define Serialiser
#' @param handler Function to use to serialise.
#' This function should accept two arguments: the object to serialise and `...`.
#'
#' @examples
#' app <- Ambiorix$new()
#'
#' app$serialiser(function(data, ...){
#' jsonlite::toJSON(x, ..., pretty = TRUE)
#' })
#'
#' app$get("/", function(req, res){
#' res$send("Using {ambiorix}!")
#' })
#'
#' if(interactive())
#' app$start()
#' @details Define Serialiser
#' @param handler Function to use to serialise.
#' This function should accept two arguments: the object to serialise and `...`.
#'
#' @examples
#' app <- Ambiorix$new()
#'
#' app$serialiser(function(data, ...){
#' jsonlite::toJSON(x, ..., pretty = TRUE)
#' })
#'
#' app$get("/", function(req, res){
#' res$send("Using {ambiorix}!")
#' })
#'
#' if(interactive())
#' app$start()
serialiser = function(handler){
assert_that(is_function(handler))
options(AMBIORIX_SERIALISER = handler)
invisible(self)
},
#' @details Stop
#' Stop the webserver.
#' @details Stop
#' Stop the webserver.
stop = function(){

if(!private$.is_running){
Expand All @@ -304,7 +304,7 @@ Ambiorix <- R6::R6Class(

invisible(self)
},
#' @details Print
#' @details Print
print = function(){
cli::cli_rule("Ambiorix", right = "web server")
cli::cli_li("routes: {.val {private$n_routes()}}")
Expand Down
4 changes: 2 additions & 2 deletions R/log.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ new_log <- function(
file = file,
sep = sep
)$
date()$
time()
date()$
time()
}

#' Customise logs
Expand Down
5 changes: 2 additions & 3 deletions R/request.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
#' @field rook.input Rook inputs.
#' @field rook.url_scheme Rook url scheme.
#' @field rook.version Rook version.
#' @field SCRIPT_NAME The initial portion of the request URL's "path" that corresponds to the application object, so that the application knows its virtual "location".
#' @field SERVER_NAME Server name.
#' @field SCRIPT_NAME The initial portion of the request URL's "path" that corresponds to the application object, so that the application knows its virtual "location". #' @field SERVER_NAME Server name.
#' @field SERVER_PORT Server port
#' @field CONTENT_LENGTH Size of the message body.
#' @field CONTENT_TYPE Type of content of the request.
Expand Down Expand Up @@ -332,4 +331,4 @@ mockRequest <- function(
)

Request$new(req)
}
}
6 changes: 3 additions & 3 deletions R/router.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ Router <- R6::R6Class(
inherit = Routing,
public = list(
error = NULL,
#' @details Define the base route.
#' @param path The base path of the router.
#' @details Define the base route.
#' @param path The base path of the router.
initialize = function(path){
assert_that(not_missing(path))
super$initialize(path)
},
#' @details Print
#' @details Print
print = function(){
cli::cli_rule("Ambiorix", right = "router")
cli::cli_li("routes: {.val {super$n_routes()}}")
Expand Down

0 comments on commit a6a91b0

Please sign in to comment.