-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
68 lines (54 loc) · 1.2 KB
/
app.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
require 'sinatra'
require 'net/http'
require 'json'
#TODO: show trolley direction
get '/' do
@data = parse_data(trolley_data)
@trolley_coords = trolley_coords(@data)
@service_alert = service_alert?
@alert_message = alert_message
@advisory_message = advisory_message
@detour_reason = detour_reason
@alert_last_updated = alert_last_updated
erb :index
end
def trolley_data
send_request('http://www3.septa.org/beta/TransitView/34')
end
def parse_data(data)
JSON.parse(data)
end
def trolley_coords(data)
data["bus"].map {|bus| [bus["lat"], bus['lng']]}
end
def send_request(url)
url = URI.parse(url.to_s)
req = Net::HTTP::Get.new(url)
res = Net::HTTP.start(url.host, url.port) {|http| http.request(req)}
res.body
end
def alerts
alerts = send_request(
'http://www3.septa.org/hackathon/Alerts/get_alert_data.php?req1=trolley_route_34'
)
JSON.parse(alerts)
end
def service_alert?
if alerts.first['current_message'].empty?
false
else
true
end
end
def alert_message
alerts.first['current_message']
end
def advisory_message
alerts.first['advisory_message']
end
def detour_reason
alerts.first['detour_reason']
end
def alert_last_updated
alerts.first['last_updated']
end