Skip to content

Commit 1513ca2

Browse files
committed
format with air
1 parent 69ce52f commit 1513ca2

32 files changed

+623
-359
lines changed

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ readme:
4545
${RSCRIPT} -e "rmarkdown::render('details.md')"
4646
${RSCRIPT} -e "knitr::knit('README.Rmd')"
4747

48-
lint_package:
49-
${RSCRIPT} -e "lintr::lint_package()"
48+
lint_check:
49+
air format --check .
5050

51-
style_package:
52-
${RSCRIPT} -e "styler::style_pkg()"
51+
lint_fix:
52+
air format .
5353

5454
# use: `make style_file FILE=stuff`
5555
# ("R/" is prepended); accepts 1 file only

R/RequestPattern.R

Lines changed: 74 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,15 @@ RequestPattern <- R6::R6Class(
114114
#' @param basic_auth (list) vector of length 2 (username, password),
115115
#' optional
116116
#' @return A new `RequestPattern` object
117-
initialize = function(method, uri = NULL, uri_regex = NULL,
118-
query = NULL, body = NULL, headers = NULL,
119-
basic_auth = NULL) {
117+
initialize = function(
118+
method,
119+
uri = NULL,
120+
uri_regex = NULL,
121+
query = NULL,
122+
body = NULL,
123+
headers = NULL,
124+
basic_auth = NULL
125+
) {
120126
if (is.null(uri) && is.null(uri_regex)) {
121127
abort("one of uri or uri_regex is required")
122128
}
@@ -148,31 +154,31 @@ RequestPattern <- R6::R6Class(
148154
if (!is.null(c_type)) c_type <- strsplit(c_type, ";")[[1]][1]
149155
self$method_pattern$matches(request_signature$method) &&
150156
self$uri_pattern$matches(request_signature$uri) &&
151-
(
152-
is.null(self$body_pattern) ||
153-
self$body_pattern$matches(request_signature$body, c_type %||% "")
154-
) &&
155-
(
156-
is.null(self$headers_pattern) ||
157-
self$headers_pattern$matches(request_signature$headers)
158-
)
157+
(is.null(self$body_pattern) ||
158+
self$body_pattern$matches(request_signature$body, c_type %||% "")) &&
159+
(is.null(self$headers_pattern) ||
160+
self$headers_pattern$matches(request_signature$headers))
159161
},
160162

161163
#' @description Print pattern for easy human consumption
162164
#' @return a string
163165
to_s = function() {
164-
gsub("^\\s+|\\s+$", "", paste(
165-
toupper(self$method_pattern$to_s()),
166-
self$uri_pattern$to_s(),
167-
if (!is.null(self$body_pattern)) {
168-
if (!is.null(self$body_pattern$pattern)) {
169-
paste0(" with body ", self$body_pattern$to_s())
166+
gsub(
167+
"^\\s+|\\s+$",
168+
"",
169+
paste(
170+
toupper(self$method_pattern$to_s()),
171+
self$uri_pattern$to_s(),
172+
if (!is.null(self$body_pattern)) {
173+
if (!is.null(self$body_pattern$pattern)) {
174+
paste0(" with body ", self$body_pattern$to_s())
175+
}
176+
},
177+
if (!is.null(self$headers_pattern)) {
178+
paste0(" with headers ", self$headers_pattern$to_s())
170179
}
171-
},
172-
if (!is.null(self$headers_pattern)) {
173-
paste0(" with headers ", self$headers_pattern$to_s())
174-
}
175-
))
180+
)
181+
)
176182
}
177183
),
178184
private = list(
@@ -306,7 +312,8 @@ HeadersPattern <- R6::R6Class(
306312
headers <- private$normalize_headers(headers)
307313
out <- c()
308314
for (i in seq_along(self$pattern)) {
309-
out[i] <- names(self$pattern)[i] %in% names(headers) &&
315+
out[i] <- names(self$pattern)[i] %in%
316+
names(headers) &&
310317
self$pattern[[i]] == headers[[names(self$pattern)[i]]]
311318
}
312319
all(out)
@@ -454,15 +461,16 @@ BodyPattern <- R6::R6Class(
454461
)
455462
} else {
456463
# FIXME: add partial approach later
457-
(private$empty_string(self$pattern) && private$empty_string(body)) || {
458-
if (xor(is_na(self$pattern), is_na(body))) {
459-
return(FALSE)
460-
}
461-
if (xor(is_null(self$pattern), is_null(body))) {
462-
return(FALSE)
464+
(private$empty_string(self$pattern) && private$empty_string(body)) ||
465+
{
466+
if (xor(is_na(self$pattern), is_na(body))) {
467+
return(FALSE)
468+
}
469+
if (xor(is_null(self$pattern), is_null(body))) {
470+
return(FALSE)
471+
}
472+
all(self$pattern == body)
463473
}
464-
all(self$pattern == body)
465-
}
466474
}
467475
},
468476

@@ -489,14 +497,14 @@ BodyPattern <- R6::R6Class(
489497
body_char <- rapply(body, as.character, how = "replace")
490498

491499
if (self$partial) {
492-
names_values_check <- switch(self$partial_type,
500+
names_values_check <- switch(
501+
self$partial_type,
493502
# unname() here not needed for R < 4.5, but is needed for R 4.5
494503
# because intersect changes to output unnamed lists
495-
include =
496-
identical(
497-
unname(intersect(pattern_char, body_char)),
498-
unname(pattern_char)
499-
),
504+
include = identical(
505+
unname(intersect(pattern_char, body_char)),
506+
unname(pattern_char)
507+
),
500508
exclude = length(intersect(pattern_char, body_char)) == 0
501509
)
502510
if (!names_values_check) {
@@ -551,25 +559,30 @@ BodyPattern <- R6::R6Class(
551559
)
552560

553561
BODY_FORMATS <- list(
554-
"text/xml" = "xml",
555-
"application/xml" = "xml",
556-
"application/json" = "json",
557-
"text/json" = "json",
558-
"application/javascript" = "json",
559-
"text/javascript" = "json",
562+
"text/xml" = "xml",
563+
"application/xml" = "xml",
564+
"application/json" = "json",
565+
"text/json" = "json",
566+
"application/javascript" = "json",
567+
"text/javascript" = "json",
560568
"application/x-amz-json-1.1" = "json", # AWS
561-
"text/html" = "html",
562-
"application/x-yaml" = "yaml",
563-
"text/yaml" = "yaml",
564-
"text/plain" = "plain"
569+
"text/html" = "html",
570+
"application/x-yaml" = "yaml",
571+
"text/yaml" = "yaml",
572+
"text/plain" = "plain"
565573
)
566574

567575
# remove_reserved & promote_attr from
568576
# https://www.garrickadenbuie.com/blog/recursive-xml-workout/
569577
remove_reserved <- function(this_attr) {
570578
reserved_attr <- c(
571-
"class", "comment", "dim", "dimnames",
572-
"names", "row.names", "tsp"
579+
"class",
580+
"comment",
581+
"dim",
582+
"dimnames",
583+
"names",
584+
"row.names",
585+
"tsp"
573586
)
574587
if (!any(reserved_attr %in% names(this_attr))) {
575588
return(this_attr)
@@ -753,7 +766,8 @@ UriPattern <- R6::R6Class(
753766
bools[i] <- qp %in% uri_qp
754767
}
755768
}
756-
out <- switch(self$partial_type,
769+
out <- switch(
770+
self$partial_type,
757771
include = any(bools),
758772
exclude = !any(bools)
759773
)
@@ -790,10 +804,14 @@ UriPattern <- R6::R6Class(
790804
inherits(query_params, "list") ||
791805
inherits(query_params, "character")
792806
) {
793-
pars <- paste0(unname(Map(
794-
function(x, y) paste(x, esc(y), sep = "="),
795-
names(query_params), query_params
796-
)), collapse = "&")
807+
pars <- paste0(
808+
unname(Map(
809+
function(x, y) paste(x, esc(y), sep = "="),
810+
names(query_params),
811+
query_params
812+
)),
813+
collapse = "&"
814+
)
797815
self$pattern <- paste0(self$pattern, "?", pars)
798816
}
799817
}
@@ -856,7 +874,8 @@ parse_a_url <- function(url) {
856874
if (!is.na(tmp$parameter)) {
857875
tmp$parameter <- unlist(
858876
lapply(
859-
strsplit(tmp$parameter, "&")[[1]], function(x) {
877+
strsplit(tmp$parameter, "&")[[1]],
878+
function(x) {
860879
z <- strsplit(x, split = "=")[[1]]
861880
as.list(stats::setNames(z[2], z[1]))
862881
}

R/RequestSignature.R

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -125,33 +125,43 @@ RequestSignature <- R6::R6Class(
125125
#' @description Request signature to a string
126126
#' @return a character string representation of the request signature
127127
to_s = function() {
128-
gsub("^\\s+|\\s+$", "", paste(
129-
paste0(toupper(self$method), ": "),
130-
self$uri,
131-
if (!is.null(self$body) && length(self$body)) {
132-
paste0(" with body ", to_string(self$body))
133-
},
134-
if (!is.null(self$headers) && length(self$headers)) {
135-
paste0(
136-
" with headers ",
137-
sprintf(
138-
"{%s}",
139-
paste(names(self$headers),
140-
unlist(unname(self$headers)),
141-
sep = ": ",
142-
collapse = ", "
128+
gsub(
129+
"^\\s+|\\s+$",
130+
"",
131+
paste(
132+
paste0(toupper(self$method), ": "),
133+
self$uri,
134+
if (!is.null(self$body) && length(self$body)) {
135+
paste0(" with body ", to_string(self$body))
136+
},
137+
if (!is.null(self$headers) && length(self$headers)) {
138+
paste0(
139+
" with headers ",
140+
sprintf(
141+
"{%s}",
142+
paste(
143+
names(self$headers),
144+
unlist(unname(self$headers)),
145+
sep = ": ",
146+
collapse = ", "
147+
)
143148
)
144149
)
145-
)
146-
}
147-
))
150+
}
151+
)
152+
)
148153
}
149154
),
150155
private = list(
151156
assign_options = function(options) {
152157
op_vars <- c(
153-
"body", "headers", "proxies", "auth",
154-
"disk", "fields", "output"
158+
"body",
159+
"headers",
160+
"proxies",
161+
"auth",
162+
"disk",
163+
"fields",
164+
"output"
155165
)
156166
for (i in seq_along(op_vars)) {
157167
if (op_vars[i] %in% names(options)) {
@@ -167,7 +177,8 @@ RequestSignature <- R6::R6Class(
167177
cat_foo <- function(x) {
168178
cat_line(paste0(
169179
" ",
170-
paste0(paste(names(x) %||% "<unnamed>", x, sep = ": "),
180+
paste0(
181+
paste(names(x) %||% "<unnamed>", x, sep = ": "),
171182
collapse = "\n "
172183
)
173184
))

R/Response.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ Response <- R6::R6Class(
7676
for (i in seq_along(self$request_headers)) {
7777
cat_line(paste0(
7878
" ",
79-
paste(names(self$request_headers)[i], self$request_headers[[i]],
79+
paste(
80+
names(self$request_headers)[i],
81+
self$request_headers[[i]],
8082
sep = ": "
8183
)
8284
))
@@ -85,7 +87,9 @@ Response <- R6::R6Class(
8587
for (i in seq_along(self$response_headers)) {
8688
cat_line(paste0(
8789
" ",
88-
paste(names(self$response_headers)[i], self$response_headers[[i]],
90+
paste(
91+
names(self$response_headers)[i],
92+
self$response_headers[[i]],
8993
sep = ": "
9094
)
9195
))

R/StubRegistry.R

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,22 @@ StubRegistry <- R6::R6Class(
5858
#' @return logical, 1 or more
5959
request_stub_for = function(request_signature, count = TRUE) {
6060
stubs <- self$request_stubs
61-
mtchs <- vapply(stubs, function(z) {
62-
tmp <- RequestPattern$new(
63-
method = z$method, uri = z$uri,
64-
uri_regex = z$uri_regex, query = z$query,
65-
body = z$body, headers = z$request_headers,
66-
basic_auth = z$basic_auth
67-
)
68-
tmp$matches(request_signature)
69-
}, logical(1))
61+
mtchs <- vapply(
62+
stubs,
63+
function(z) {
64+
tmp <- RequestPattern$new(
65+
method = z$method,
66+
uri = z$uri,
67+
uri_regex = z$uri_regex,
68+
query = z$query,
69+
body = z$body,
70+
headers = z$request_headers,
71+
basic_auth = z$basic_auth
72+
)
73+
tmp$matches(request_signature)
74+
},
75+
logical(1)
76+
)
7077
if (count) {
7178
for (i in seq_along(stubs)) {
7279
if (mtchs[i]) stubs[[i]]$counter$put(request_signature)
@@ -147,17 +154,21 @@ make_query <- function(x) {
147154
if (is.null(x)) {
148155
return("")
149156
}
150-
txt <- paste(names(x), subs(unname(unlist(x)), 20),
157+
txt <- paste(
158+
names(x),
159+
subs(unname(unlist(x)), 20),
151160
sep = "=",
152161
collapse = ", "
153162
)
154163
if (attr(x, "partial_match") %||% FALSE) {
155164
txt <- sprintf(
156165
"%s(%s)",
157-
switch(attr(x, "partial_type"),
166+
switch(
167+
attr(x, "partial_type"),
158168
include = "including",
159169
exclude = "excluding"
160-
), txt
170+
),
171+
txt
161172
)
162173
}
163174
paste0(" with query params ", txt)

0 commit comments

Comments
 (0)