Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Returns stimated crossing times in the US border

commit bbe9de7e45edd86b1caebe4cfa643c15144ccdfc 1 parent de4993b
@javierg javierg authored
Showing with 44 additions and 0 deletions.
  1. +44 −0 src/scripts/crossing.coffee
View
44 src/scripts/crossing.coffee
@@ -0,0 +1,44 @@
+# Gets border waiting times to US.
+#
+# crossing <port_name>
+
+parser = require('xml2json');
+
+String.prototype.minimize = ->
+ return this.toLowerCase().replace( /\ /g, '' )
+
+delay_time = ( port_object ) ->
+ return "N/A" if port_object["operational_status"].match /N\/A|closed/gi
+ return port_object["delay_minutes"]
+
+build_name = ( port_object ) ->
+ name = port_object['port_name']
+ name += " #{port_object['crossing_name']}" unless port_object['crossing_name'] instanceof Object
+ return name
+
+safe_element = ( port_object, element ) ->
+ return "N/A" if port_object[ element ] instanceof Object
+ return port_object[ element ]
+
+module.exports = (robot) ->
+ robot.respond /crossing (.*)$/i, (msg) ->
+ port_name = msg.match[1]
+ message = ""
+
+ msg.http('http://apps.cbp.gov/bwt/bwt.xml')
+ .get() (err, res, body) ->
+ json_border_data = parser.toJson( body )
+ border = JSON.parse( json_border_data )["border_wait_time"]["port"]
+ for port in border
+ if port.port_name.minimize() is port_name.minimize()
+ message += "#{ build_name( port )}\n" +
+ "Updated at #{ safe_element( port, 'date' ) } #{ safe_element( port['passenger_vehicle_lanes']['standard_lanes'], 'update_time' ) }\n" +
+ "Port is now #{port['port_status']}\n" +
+ "Normal Lanes: #{ delay_time( port['passenger_vehicle_lanes']['standard_lanes'] )}\n" +
+ "SENTRI: #{delay_time( port['passenger_vehicle_lanes']['NEXUS_SENTRI_lanes'] )}\n" +
+ "Ready Lanes: #{delay_time( port['passenger_vehicle_lanes']['ready_lanes'] )}\n" +
+ "Pedestrian: #{ delay_time( port['pedestrian_lanes']['standard_lanes'] )}\n"
+
+ message = "I don't know where #{port_name} is" if message.minimize() is ""
+
+ msg.send message
Please sign in to comment.
Something went wrong with that request. Please try again.