-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbus.rb
More file actions
40 lines (33 loc) · 847 Bytes
/
bus.rb
File metadata and controls
40 lines (33 loc) · 847 Bytes
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
require 'rubygems'
require 'sinatra'
require 'rest-client'
require 'json'
require 'time'
AGENCY_ID = 132
STOP_ID = 4068514
get '/' do
minutes = eta
if minutes.nil?
@info = "No Bus :("
elsif minutes < 2
@info = "Hurry! E.T.A #{minutes.to_s} minutes"
elsif minutes > 15
@info = "E.T.A #{minutes.to_s} minutes ... just walk!"
else
@info = "E.T.A #{minutes.to_s} minutes"
end
erb :index
end
def eta
website = RestClient.get("http://api.transloc.com/1.1/arrival-estimates.json",
{:params=> {:agencies=>AGENCY_ID, :stops=> STOP_ID}})
data = JSON.load(website)["data"]
if data.empty?
time = nil
else
arrival = data.first['arrivals'].first["arrival_at"]
seconds = Time.parse(arrival)-Time.now()
time = (seconds/60).floor;
end
return time
end