-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvans.rb
210 lines (166 loc) · 5.38 KB
/
vans.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
require 'rubygems'
require 'json'
require 'sinatra/base'
require 'typhoeus'
class VandyVans < Sinatra::Base
API_KEY = ENV['API_KEY']
# Stops
BRANSCOMB_QUAD = '263473'
CARMICHAEL_TOWERS = '263470'
HANK_INGRAM = '644903'
KISSAM = '1198824'
HIGHLAND_QUAD = '263444'
# Other Stops
VUPD = '264041'
BOOK_STORE = '332298'
TERRACE_PLACE = '644873'
WESLEY_PLACE = '1198825'
NORTH_HOUSE = '263463'
BLAIR = '264091'
MCGUGIN_CENTER = '264101'
MRB_3 = '644874'
set :logging, true
before do
content_type :json
end
get '/arrivalTimes' do
cache_control :private, max_age: 15
stop_ID = params[:stopID]
predictions_response = [ ]
if stop_ID
hydra = Typhoeus::Hydra.hydra
routes = self.class.routes_for_stop(stop_ID)
routes.each do |route|
logger.info 'Request for Route ' + route.to_s + ' and Stop ' + stop_ID
request = Typhoeus::Request.new('http://api.syncromatics.com/Route/' + route.to_s + '/Stop/' + stop_ID + '/Arrivals?api_key=' + API_KEY)
request.on_complete do |response|
if response.success?
result = JSON.parse(response.body)
predictions = result['Predictions']
logger.info 'Got result: ' + result.to_s
predictions.each do |prediction|
predictions_response << {
'stopID' => prediction['StopId'],
'routeID' => prediction['RouteId'],
'minutes' => prediction['Minutes'],
}
end
elsif response.timed_out?
logger.info 'Request Timed Out'
elsif response.code == 0
# Could not get an HTTP response; something's wrong.
loger.info response.return_message
else
logger.info 'HTTP request failed: ' + response.code.to_s
end
end
hydra.queue request
end
hydra.run
logger.info 'Sorting Predictions'
predictions_response.sort! do |a, b|
a['minutes'] <=> b['minutes']
end
end
predictions_response.to_json
end
get '/vans' do
cache_control :private, max_age: 3
route_ID = params[:routeID]
vans_response = [ ]
if route_ID
request = Typhoeus::Request.new('http://api.syncromatics.com/Route/' + route_ID + '/Vehicles?api_key=' + API_KEY)
request.on_complete do |response|
if response.success?
vans = JSON.parse(response.body)
vans.each do |van|
vans_response << { 'vanID' => van['ID'], 'latitude' => van['Latitude'], 'longitude' => van['Longitude'], 'percentageFull' => van['APCPercentage'] }
end
elsif response.timed_out?
logger.info 'Request Timed Out'
elsif response.code == 0
# Could not get an HTTP response; something's wrong.
logger.info response.return_message
else
logger.info 'HTTP request failed: ' + response.code.to_s
end
end
request.run
else
logger.info 'No route ID'
end
vans_response.to_json
end
get '/waypoints' do
cache_control :private, max_age: 60
route_ID = params[:routeID]
waypoints_response = [ ]
if route_ID
request = Typhoeus::Request.new('http://vandyvans.com/Route/' + route_ID + '/Waypoints?api_key=' + API_KEY)
request.on_complete do |response|
if response.success?
waypoints_outer_array = JSON.parse(response.body)
waypoints = waypoints_outer_array[0]
waypoints.each do |waypoint|
waypoints_response << { 'latitude' => waypoint['Latitude'], 'longitude' => waypoint['Longitude'] }
end
elsif response.timed_out?
logger.info 'Request Timed Out'
elsif response.code == 0
# Could not get an HTTP response; something's wrong.
logger.info response.return_message
else
logger.info 'HTTP request failed: ' + response.code.to_s
end
end
request.run
else
logger.info 'No route ID'
end
waypoints_response.to_json
end
get '/stops' do
cache_control :private, max_age: 60
stops_response = {
stops: [
{ BRANSCOMB_QUAD => 'Branscomb Quad' },
{ CARMICHAEL_TOWERS => 'Carmichael Towers' },
{ HANK_INGRAM => 'Hank Ingram' },
{ KISSAM => 'College Halls at Kissam' },
{ HIGHLAND_QUAD => 'Highland Quad' },
],
other_stops: [
{ VUPD => 'Vanderbilt Police Department' },
{ BOOK_STORE => 'Vanderbilt Book Store' },
{ TERRACE_PLACE => '21st near Terrace Place' },
{ WESLEY_PLACE => 'Wesley Place Garage' },
{ NORTH_HOUSE => 'North House' },
{ BLAIR => 'Blair School of Music' },
{ MCGUGIN_CENTER => 'McGugin Center' },
{ MRB_3 => 'MRB 3' },
],
}
stops_response.to_json
end
def self.routes_for_stop(stop_ID)
black = 1857
gold = 1856
red = 1858
routes = [ ]
case stop_ID
when BRANSCOMB_QUAD, CARMICHAEL_TOWERS, HIGHLAND_QUAD
routes = [ black, gold, red ]
when KISSAM, TERRACE_PLACE
routes = [ black, gold ]
when VUPD
routes = [ red, gold ]
when BOOK_STORE, NORTH_HOUSE, BLAIR, MCGUGIN_CENTER, MRB_3
routes = [ gold ]
when WESLEY_PLACE
routes = [ black ]
when HANK_INGRAM
routes = [ black, red ]
end
routes
end
end