Turned routines to preprocess user inputs into S3 methods.

Still needs to be tested.
This commit is contained in:
pegeler 2018-06-22 22:24:34 -04:00
commit 1053329a62
5 changed files with 70 additions and 50 deletions

53
R/R/process_user_input.r Normal file
View file

@ -0,0 +1,53 @@
process_user_input <- function (x) {
UseMethod("process_user_input", x)
}
process_user_input.default <- function(x, ...) {
stop(
deparse(substitute(x)),
" must be a 'data.frame',",
" a 'response',",
" or a 'character' vector containing JSON.",
call. = FALSE
)
}
process_user_input.data.frame <- function(x, ...) {
x
}
process_user_input.character <- function(x, ...) {
if (!requireNamespace("jsonlite", quietly = TRUE)) {
stop(
"The package 'jsonlite' is needed to convert ",
deparse(substitute(x)),
" into a data frame.",
"\n Either install 'jsonlite' or pass ",
deparse(substitute(x)),
" as a 'data.frame'.",
call. = FALSE
)
}
jsonlite::fromJSON(x)
}
process_user_input.response <- function(x, ...) {
if (!requireNamespace("httr", quietly = TRUE)) {
stop(
"The package 'httr' is needed to convert ",
deparse(substitute(x)),
" into a data frame.",
"\n Either install 'httr' or pass ",
deparse(substitute(x)),
" as a 'data.frame'.",
call. = FALSE
)
}
httr::content(x, as = "text")
}