Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
amoeba committed Mar 3, 2017
0 parents commit a4364aa
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
^.*\.Rproj$
^\.Rproj\.user$
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
15 changes: 15 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Package: rt
Type: Package
Title: An R Package for the RT REST API
Version: 0.1.0
Author: Bryce Mecum <[email protected]>
Maintainer: Bryce Mecum <[email protected]>
Description: This package implements a number of the functions provided by the
RT REST API.
License: MIT
Encoding: UTF-8
LazyData: true
Depends: httr,
stringr
Suggests: testthat
RoxygenNote: 6.0.1
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exportPattern("^[[:alpha:]]+")
11 changes: 11 additions & 0 deletions R/rt_login.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
rt_login <- function(base, user, pass) {
req <- httr::POST(base, body = list('user' = user, 'pass' = pass))
if (req$status_code == 200) {
message("Successfully logged in.")
} else {
stop(req)
}

invisible(TRUE)
}

56 changes: 56 additions & 0 deletions R/rt_search.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
rt_search <- function(base, query, orderBy=NULL, format="l") {
url <- paste0(base, "/search/ticket?query=", query)

if (!is.null(orderBy)) {
url <- paste0(url, "&orderBy=", orderBy)
}

if (!is.null(format)) {
url <- paste0(url, "&format=", format)
}

req <- httr::GET(url)

if (req$status_code != 200) {
stop(req, call. = FALSE)
}


if (format != "l") {
return(req)
}

result_split <- lapply(stringr::str_split(httr::content(req), "\\n--\\n"), stringr::str_split, "\\n")[[1]]

parse_rt_result <- function(x) {
lines_f <- Filter(function(x) { stringr::str_detect(x, ": ")}, x)
parts <- stringr::str_split(lines_f, ": ")
x <- do.call(list, lapply(parts, function(p) paste0(p[-1], collapse = ": ")))
names(x) <- lapply(parts, function(p) p[[1]])

x
}

y <- lapply(result_split, parse_rt_result)

result <- data.frame()

for (z in y) {
zdf <- as.data.frame(z, stringsAsFactors = FALSE)

if (nrow(result) > 0 ) {
for (name in setdiff(names(result), names(zdf))) {
zdf[,name] <- NA
}

for (name in setdiff(names(zdf), names(result))) {
result[,name] <- NA
}
}

result <- rbind(result, zdf)
}

result
}

19 changes: 19 additions & 0 deletions R/rt_ticket_history.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
rt_ticket_history <- function(base, ticket) {
if (missing(ticket)) {
stop("'ticket' must be specified.", call. = FALSE)
}

url <- paste0(base, "/ticket/", as.character(ticket), "/history")

req <- httr::GET(url)
history <- stringr::str_split(httr::content(req), "\\n")[[1]]
history <- Filter(function(x) { stringr::str_detect(x, ":")}, history)
transactions <- stringr::str_split(history, ": ")
ids <- lapply(transactions, function(x) x[1])
bodies <- lapply(transactions, function(x) x[2])

transactions_df <- data.frame(id = unlist(ids), body = unlist(bodies), stringsAsFactors = FALSE)
transactions_df <- transactions_df[order(transactions_df$id),]

transactions_df
}
9 changes: 9 additions & 0 deletions R/rt_ticket_show.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
rt_ticket_show <- function(base, ticket) {
if (missing(ticket)) {
stop("The argument 'ticket' must be specified.", call. = FALSE)
}

url <- paste0(base, "/ticket/", as.character(ticket), "/show")

httr::GET(url)
}
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# rt

An R package for the [RT REST API](https://rt-wiki.bestpractical.com/wiki/REST).

## Installation

TBD

## Development

TBD

## Support / Issues / Feedback

TBD
12 changes: 12 additions & 0 deletions man/hello.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
\name{hello}
\alias{hello}
\title{Hello, World!}
\usage{
hello()
}
\description{
Prints 'Hello, world!'.
}
\examples{
hello()
}
20 changes: 20 additions & 0 deletions rt.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: knitr
LaTeX: pdfLaTeX

StripTrailingWhitespace: Yes

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace,vignette
4 changes: 4 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
library(testthat)
library(rt)

test_check("rt")

0 comments on commit a4364aa

Please sign in to comment.