Skip to content
Merged
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
45 changes: 25 additions & 20 deletions R/rt_search.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
tabularize_ticket <- function(text) {
# Helper function to help tabularize ticket text from search results
ticket_headers <- stringr::str_extract_all(text, "\\n[a-zA-Z0-9{}.]+:")[[1]] %>%
str_replace_all("\\n|:", "")
ticket_content <- stringr::str_split(text, "\\n[a-zA-Z0-9{}.]+:")[[1]][-1] %>%
trimws()

return(data.frame(ticket_headers, ticket_content, stringsAsFactors = FALSE))
}

#' Search RT
#'
#' Search RT for tickets that fit your query.
Expand All @@ -24,20 +34,20 @@
#' }

rt_search <- function(query, orderBy = NULL, format="l", rt_base = getOption("rt_base")) {
base_api <- paste(stringr::str_replace(rt_base, "\\/$", ""), # removes trailing slash from base URL just in case
"REST", "1.0", sep = "/")
base_api <- rt_url(rt_base, "search/ticket?")

url <- paste0(base_api, "/search/ticket?query=", query)
#based on httr::modify_url()
#possible TODO - turn this into its own function that can be used internally in the package
l <- Filter(Negate(is.null), #remove nulls, equivalent to purrr::compact
list(query = query,
orderBy = orderBy,
format = format))

if (!is.null(orderBy)) {
url <- paste0(url, "&orderBy=", orderBy)
}
params <- paste(paste0(names(l), "=", l), collapse = "&")

if (!is.null(format)) {
url <- paste0(url, "&format=", format)
}
url <- utils::URLencode(paste0(base_api, params))

req <- httr::GET(utils::URLencode(url))
req <- httr::GET(url)

if (stringr::str_detect(httr::content(req), "Bad request")) {
stop(httr::content(req), call. = FALSE)
Expand All @@ -47,19 +57,14 @@ rt_search <- function(query, orderBy = NULL, format="l", rt_base = getOption("rt
return(req)
}

not_empty <- function(column) {
!all(column == "" | is.na(column))
}

result <- tibble::tibble(content = stringr::str_split(httr::content(req), "\\n--\\n")[[1]]) %>%
dplyr::mutate(content = stringr::str_split(content, "\\n"),
line = 1:n()) %>%
mutate(line = 1:n(),
content = lapply(content, tabularize_ticket)) %>%
tidyr::unnest() %>%
dplyr::filter(content != "") %>%
tidyr::separate(content, c("colname", "value"), sep = ":", fill = "right", extra = "merge") %>%
tidyr::spread(colname, value) %>%
dplyr::select_if(not_empty)
tidyr::spread(ticket_headers, ticket_content) %>%
dplyr::select_if(function(column) {!all(column == "" | is.na(column))}) #not empty

return(result)
}