Skip to content
This repository was archived by the owner on Mar 25, 2019. It is now read-only.

Commit ada7b52

Browse files
committed
Merge branch 'function-otp-connect'
2 parents 9e9c617 + 8a5e3c1 commit ada7b52

File tree

6 files changed

+196
-0
lines changed

6 files changed

+196
-0
lines changed

DESCRIPTION

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ Description: An interface for Open Trip Planner <http://www.opentripplanner.org/
1313
License: GPL-3
1414
Encoding: UTF-8
1515
LazyData: true
16+
Imports:
17+
checkmate,
18+
httr
1619
RoxygenNote: 6.0.1
20+
Suggests: testthat

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(hello)
4+
export(otp_connect)

R/otp-connect.R

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#' Set up and confirm a connection to an OTP instance.
2+
#'
3+
#' Defines the parameters required to connect to a router on an OTP instance and,
4+
#' if required, confirms that the instance and router are queryable.
5+
#'
6+
#' @param hostname A string, e.g. "ec2-34-217-73-26.us-west-2.compute.amazonaws.com".
7+
#' Optional, default is "localhost".
8+
#' @param router A string, e.g. "UK2018". Optional, default is "default".
9+
#' @param port A positive integer. Optional, default is 8080.
10+
#' @param ssl Logical, indicates whether to use https. Optional, default is FALSE.
11+
#' @param check Logical. If TRUE connection object is only returned if OTP
12+
#' instance and router are confirmed reachable. Optional, default is TRUE.
13+
#' @return Returns an S3 object of class otpconnect. If \code{check} is TRUE
14+
#' and the router is not reachable the object is not returned.
15+
#' @examples
16+
#' otpcon <- otp_connect()
17+
#' otpcon <- otp_connect(router = "UK2018",
18+
#' ssl = TRUE)
19+
#' otpcon <- otp_connect(hostname = "ec2.us-west-2.compute.amazonaws.com",
20+
#' router = "UK2018",
21+
#' port = 8888,
22+
#' ssl = TRUE)
23+
24+
#' @export
25+
otp_connect <- function(hostname = "localhost",
26+
router = "default",
27+
port = 8080,
28+
ssl = FALSE,
29+
check = TRUE)
30+
{
31+
# argument checks
32+
33+
coll <- checkmate::makeAssertCollection()
34+
checkmate::assert_string(hostname, add = coll)
35+
checkmate::assert_string(router, add = coll)
36+
checkmate::assert_int(port, lower = 1, add = coll)
37+
checkmate::assert_logical(ssl, add = coll)
38+
checkmate::assert_logical(check, add = coll)
39+
checkmate::reportAssertions(coll)
40+
41+
otpcon <- list(
42+
hostname = hostname,
43+
router = router,
44+
port = port,
45+
ssl = ssl
46+
)
47+
48+
# Set the name for the class
49+
class(otpcon) <- append(class(otpcon), "otpconnect")
50+
51+
52+
# If check then confirm router is queryable
53+
54+
if (isTRUE(check)) {
55+
if (check_router(otpcon) == 200) {
56+
message("Router ", make_url(otpcon), " exists")
57+
return(otpcon)
58+
} else {
59+
stop("Router ", make_url(otpcon), " does not exist")
60+
}
61+
} else {
62+
return(otpcon)
63+
}
64+
}
65+
66+
# otpconnect class method to generate baseurl
67+
68+
make_url <- function(x)
69+
{
70+
UseMethod("make_url", x)
71+
}
72+
73+
make_url.default <- function(x)
74+
{
75+
warning(
76+
"make_url does not know how to handle objects of class ",
77+
class(x),
78+
", and can only be used on the class otpconnect"
79+
)
80+
return(NULL)
81+
}
82+
83+
make_url.otpconnect <- function(x)
84+
{
85+
url <- paste0(
86+
ifelse(isTRUE(x$ssl), 'https://', 'http://'),
87+
x$hostname,
88+
':',
89+
x$port,
90+
'/otp/routers/',
91+
x$router
92+
)
93+
return(url)
94+
}
95+
96+
# otpconnect method to check if router exists
97+
98+
check_router <- function(x)
99+
{
100+
UseMethod("check_router", x)
101+
}
102+
103+
check_router.default <- function(x)
104+
{
105+
warning(
106+
"check_router does not know how to handle objects of class ",
107+
class(x),
108+
", and can only be used on the class otpconnect"
109+
)
110+
return(NULL)
111+
}
112+
113+
check_router.otpconnect <- function(x)
114+
{
115+
check <- httr::GET(make_url(x))
116+
return(check$status_code)
117+
}

man/otp_connect.Rd

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
library(testthat)
2+
library(opentripplanner)
3+
4+
test_check("opentripplanner")

tests/testthat/test-otp_connect.R

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
3+
context("Test the otp_connect function")
4+
5+
test_that("default object is created and make_url method works", {
6+
otpcon <- otp_connect(check = FALSE)
7+
expect_is(otpcon, "otpconnect")
8+
expect_match(make_url(otpcon), "http://localhost:8080/otp/routers/default")
9+
})
10+
11+
# the following tests require an OTP instance at http://localhost:8080/otp with "default" router
12+
13+
test_that("object returned when check is TRUE and router exists", {
14+
otpcon <- otp_connect()
15+
expect_is(otpcon, "otpconnect")
16+
})
17+
18+
test_that("correct message when check is TRUE and router exists", {
19+
expect_message(otp_connect(), "Router http://localhost:8080/otp/routers/default exists")
20+
})
21+
22+
23+
test_that("object is not returned when check is TRUE and router does not exist", {
24+
#otpcon <- otp_connect(router = "test")
25+
expect_false(exists("otpcon"))
26+
})
27+
28+
test_that("correct error when check is TRUE and router does not exist", {
29+
expect_error(otp_connect(router = "test"), "Router http://localhost:8080/otp/routers/test does not exist")
30+
})
31+

0 commit comments

Comments
 (0)