From 2ff7b0d36b253c8a551971f0561b59f7bb3b6435 Mon Sep 17 00:00:00 2001 From: Irene <25118334+isteves@users.noreply.github.com> Date: Wed, 30 May 2018 12:09:24 -0700 Subject: [PATCH 1/6] rewrote URL build --- R/rt_search.R | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/R/rt_search.R b/R/rt_search.R index b7c9bec..b817ad4 100644 --- a/R/rt_search.R +++ b/R/rt_search.R @@ -26,15 +26,16 @@ rt_search <- function(query, orderBy = NULL, format="l", rt_base = getOption("rt base_api <- paste(stringr::str_replace(rt_base, "\\/$", ""), # removes trailing slash from base URL just in case "REST", "1.0", sep = "/") - url <- paste0(base_api, "/search/ticket?query=", query) + #based on httr::modify_url() + l <- plyr::compact(list(query = query, + orderBy = orderBy, + format = format)) + + params <- paste(paste0(names(l), "=", l), collapse = "&") + + url <- paste0(base_api, "/search/ticket?", params) - if (!is.null(orderBy)) { - url <- paste0(url, "&orderBy=", orderBy) - } - if (!is.null(format)) { - url <- paste0(url, "&format=", format) - } req <- httr::GET(URLencode(url)) From 235912317fc0e48cd8445eb6694c7c7a3a1dc203 Mon Sep 17 00:00:00 2001 From: Irene <25118334+isteves@users.noreply.github.com> Date: Wed, 30 May 2018 14:43:28 -0700 Subject: [PATCH 2/6] Reworked search result processing to fix issue #11 --- R/rt_search.R | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/R/rt_search.R b/R/rt_search.R index d1ea159..6ad16a5 100644 --- a/R/rt_search.R +++ b/R/rt_search.R @@ -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. @@ -16,6 +26,7 @@ #' @importFrom tibble tibble #' @import stringr #' @importFrom utils URLencode +#' @importFrom plyr compact #' #' @examples #' \dontrun{ @@ -28,6 +39,7 @@ rt_search <- function(query, orderBy = NULL, format="l", rt_base = getOption("rt "REST", "1.0", sep = "/") #based on httr::modify_url() + #possible TODO - turn this into its own function that can be used internally in the package l <- plyr::compact(list(query = query, orderBy = orderBy, format = format)) @@ -36,8 +48,6 @@ rt_search <- function(query, orderBy = NULL, format="l", rt_base = getOption("rt url <- paste0(base_api, "/search/ticket?", params) - - req <- httr::GET(utils::URLencode(url)) if (stringr::str_detect(httr::content(req), "Bad request")) { @@ -48,19 +58,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) } + From e45ee579dbbc1223bc4d85097a429121f11b5cc4 Mon Sep 17 00:00:00 2001 From: Irene <25118334+isteves@users.noreply.github.com> Date: Wed, 30 May 2018 14:44:56 -0700 Subject: [PATCH 3/6] updated description/namespace --- DESCRIPTION | 1 + NAMESPACE | 1 + 2 files changed, 2 insertions(+) diff --git a/DESCRIPTION b/DESCRIPTION index c50f54b..273f0de 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -15,6 +15,7 @@ LazyData: true Depends: httr Imports: dplyr, getPass, + plyr, stringr, tibble, tidyr diff --git a/NAMESPACE b/NAMESPACE index 280e759..ebd5b08 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -12,6 +12,7 @@ import(httr) import(stringr) importFrom(getPass,getPass) importFrom(httr,GET) +importFrom(plyr,compact) importFrom(tibble,tibble) importFrom(tidyr,separate) importFrom(tidyr,spread) From 27e81b9b91e82181b257ee706b75a4b23604f9c9 Mon Sep 17 00:00:00 2001 From: Irene <25118334+isteves@users.noreply.github.com> Date: Wed, 30 May 2018 15:40:08 -0700 Subject: [PATCH 4/6] reworked with rt_url --- R/rt_search.R | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/R/rt_search.R b/R/rt_search.R index 6ad16a5..ec566ca 100644 --- a/R/rt_search.R +++ b/R/rt_search.R @@ -35,8 +35,7 @@ tabularize_ticket <- function(text) { #' } 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?") #based on httr::modify_url() #possible TODO - turn this into its own function that can be used internally in the package @@ -46,9 +45,9 @@ rt_search <- function(query, orderBy = NULL, format="l", rt_base = getOption("rt params <- paste(paste0(names(l), "=", l), collapse = "&") - url <- paste0(base_api, "/search/ticket?", params) + 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) From fa4729597e3a90403b32c9459a0bc88a748bf5e5 Mon Sep 17 00:00:00 2001 From: Irene <25118334+isteves@users.noreply.github.com> Date: Fri, 1 Jun 2018 12:10:58 -0700 Subject: [PATCH 5/6] removed plyr dependency --- NAMESPACE | 1 - R/rt_search.R | 8 ++++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index ebd5b08..280e759 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -12,7 +12,6 @@ import(httr) import(stringr) importFrom(getPass,getPass) importFrom(httr,GET) -importFrom(plyr,compact) importFrom(tibble,tibble) importFrom(tidyr,separate) importFrom(tidyr,spread) diff --git a/R/rt_search.R b/R/rt_search.R index ec566ca..084ef5c 100644 --- a/R/rt_search.R +++ b/R/rt_search.R @@ -26,7 +26,6 @@ tabularize_ticket <- function(text) { #' @importFrom tibble tibble #' @import stringr #' @importFrom utils URLencode -#' @importFrom plyr compact #' #' @examples #' \dontrun{ @@ -39,9 +38,10 @@ rt_search <- function(query, orderBy = NULL, format="l", rt_base = getOption("rt #based on httr::modify_url() #possible TODO - turn this into its own function that can be used internally in the package - l <- plyr::compact(list(query = query, - orderBy = orderBy, - format = format)) + l <- Filter(Negate(is.null), #remove nulls, equivalent to purrr::compact + list(query = query, + orderBy = orderBy, + format = format)) params <- paste(paste0(names(l), "=", l), collapse = "&") From 45ef010cc0f875329bafff1e91660e8fa314a990 Mon Sep 17 00:00:00 2001 From: Irene <25118334+isteves@users.noreply.github.com> Date: Fri, 1 Jun 2018 12:17:53 -0700 Subject: [PATCH 6/6] remove plyr from description --- DESCRIPTION | 1 - 1 file changed, 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 273f0de..c50f54b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -15,7 +15,6 @@ LazyData: true Depends: httr Imports: dplyr, getPass, - plyr, stringr, tibble, tidyr