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

specify questions, embedded, & metadata, or drop w/NA, closing #223 #226

Closed
wants to merge 4 commits into from
Closed
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: 17 additions & 2 deletions R/assertions.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,23 @@ assert_timeZone_string <- function(time_zone) {

# Check if include_questions are string(s)
assert_includedQuestionIds_string <- function(include_questions) {
assertthat::assert_that(mode(include_questions) == "character",
msg = "'include_questions' must be a character vector."
assertthat::assert_that(mode(include_questions) == "character" |
(length(include_questions) == 1 && is.na(include_questions[1])),
msg = "if present, 'include_questions' must be a character vector or NA."
)
}
# Check if include_embedded are string(s)
assert_includedembeddedDataIds_string <- function(include_embedded) {
assertthat::assert_that(mode(include_embedded) == "character" |
(length(include_embedded) == 1 && is.na(include_embedded[1])),
msg = "if present, 'include_embedded' must be a character vector or NA."
)
}
# Check if include_metadata are string(s)
assert_includedsurveyMetadataIds_string <- function(include_metadata) {
assertthat::assert_that(mode(include_metadata) == "character" |
(length(include_metadata) == 1 && is.na(include_metadata[1])),
msg = "if present, 'include_metadata' must be a character vector or NA."
)
}

Expand Down
6 changes: 6 additions & 0 deletions R/fetch_survey.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ fetch_survey <- function(surveyID,
include_display_order = TRUE,
limit = NULL,
include_questions = NULL,
include_embedded = NULL,
include_metadata = NULL,
save_dir = NULL,
force_request = FALSE,
verbose = TRUE,
Expand Down Expand Up @@ -136,6 +138,8 @@ fetch_survey <- function(surveyID,
start_date = start_date,
end_date = end_date,
include_questions = include_questions,
include_embedded = include_embedded,
include_metadata = include_metadata,
save_dir = save_dir,
unanswer_recode = unanswer_recode,
unanswer_recode_multi = unanswer_recode_multi,
Expand Down Expand Up @@ -177,6 +181,8 @@ fetch_survey <- function(surveyID,
limit = limit,
time_zone = time_zone,
include_questions = include_questions,
include_embedded = include_embedded,
include_metadata = include_metadata,
breakout_sets = breakout_sets
)

Expand Down
39 changes: 35 additions & 4 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,24 @@ check_params <- function(...) {
assert_limit_abovezero(args$limit)
}
}
# Check if includedQuestionIds is a string
# Check if include_questions is a string
if ("include_questions" %in% names(args)) {
if (!is.null(args$include_questions)) {
assert_includedQuestionIds_string(args$include_questions)
}
}
# Check if include_embedded is a string
if ("include_questions" %in% names(args)) {
if (!is.null(args$include_embedded)) {
assert_includedembeddedDataIds_string(args$include_embedded)
}
}
# Check if include_metadata is a string
if ("include_questions" %in% names(args)) {
if (!is.null(args$include_metadata)) {
assert_includedsurveyMetadataIds_string(args$include_metadata)
}
}
}

#' Generate URL for specific API query by type and (if appropriate) ID
Expand Down Expand Up @@ -272,6 +284,8 @@ create_raw_payload <- function(label = TRUE,
unanswer_recode_multi = NULL,
include_display_order = TRUE,
include_questions = NULL,
include_embedded = NULL,
include_metadata = NULL,
breakout_sets = NULL) {

params <- as.list(environment())
Expand All @@ -286,19 +300,34 @@ create_raw_payload <- function(label = TRUE,
unanswer_recode_multi = "multiselectSeenUnansweredRecode",
include_display_order = "includeDisplayOrder",
include_questions = "questionIds",
include_embedded = "embeddedDataIds",
include_metadata = "surveyMetadataIds",
breakout_sets = "breakoutSets")



# Fix a few parameters:
if(!is.null(params$start_date)){
params$start_date <- paste0(start_date, "T00:00:00Z")
}
if(!is.null(params$end_date)){
params$end_date <- paste0(end_date, "T00:00:00Z")
}
# If these are NA, the user wants none of this content,
# so exclude it via making it into an empty string vector:
if(length(params$include_questions) == 1 &&
is.na(params$include_questions[1])){
params$include_questions <- character()
}
if(length(params$include_embedded) == 1 &&
is.na(params$include_embedded[1])){
params$include_embedded <- character()
}
if(length(params$include_metadata) == 1 &&
is.na(params$include_metadata[1])){
params$include_metadata <- character()
}

# Adjust names to fit API names:

names(params) <- names_crosswalk[names(params)]

# Add in format param:
Expand All @@ -310,8 +339,10 @@ create_raw_payload <- function(label = TRUE,
}
)

# But "questionIds" needs to be boxed
# But the variable inclusion items needs to be boxed:
params_ub["questionIds"] <- params["questionIds"]
params_ub["embeddedDataIds"] <- params["embeddedDataIds"]
params_ub["surveyMetadataIds"] <- params["surveyMetadataIds"]

# Drop any NULL elements:
params_ub <- purrr::discard(params_ub, ~ is.null(.x))
Expand Down