This repository has been archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
tctransit.coffee
68 lines (61 loc) · 2.09 KB
/
tctransit.coffee
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
# Description:
# Allows users to check MetroTransit times in the TwinCities
# metrotransit.herokuapp.com
#
# Dependencies:
# none
#
# Configuration:
# none
#
# Commands:
# hubot when is the next <route #> going <north/south/east/west> from <4 letter stop code OR street name>
#
# Author:
# pyro2927
module.exports = (robot) ->
robot.respond /when is the next (.*) going (.*) from (.*)/i, (msg) ->
route = msg.match[1]
direction = msg.match[2]
dirNum = 4
if direction.toLowerCase() == "east"
dirNum = 2
else if direction.toLowerCase() == "west"
dirNum = 3
else if direction.toLowerCase() == "south"
dirNum = 1
stop = msg.match[3]
if (stop.length != 4)
TransitAPI.search_stop_codes(route, dirNum, stop, msg)
else
TransitAPI.fetch_next_stop(route, dirNum, stop, msg)
class TransitAPI
constructor: ->
fetch_next_stop: (route, dirNum, stopCode, msg) =>
msg.http('http://metrotransit.herokuapp.com/nextTrip?route=' + route + '&direction=' + dirNum + '&stop=' + stopCode)
.get() (err, res, body) =>
stops = JSON.parse(body)
if stops.count <= 0
msg.send "No next stops"
return
time = stops[0].time
if time.match(/Min$/)
time = "in " + time
else if time.match(/:/)
time = "at " + time
msg.send "The next " + route + " at " + stops[0].stop_name + " is " + time
search_stop_codes: (route, dirNum, stopName, msg) =>
msg.http('http://metrotransit.herokuapp.com/stops?route=' + route + '&direction=' + dirNum)
.get() (err, res, body) =>
stops = JSON.parse(body)
# too bad, no stops found for this
if stops.count <= 0
msg.send "No stops available for the " + route + " going that direction"
return
# see if any of our stops match
for stop in stops
if stop.name.toLowerCase().indexOf(stopName.toLowerCase()) > -1
this.fetch_next_stop(route, dirNum, stop.key, msg)
return
msg.send "No stops found with name: " + stopName
TransitAPI = new TransitAPI()