forked from travisjeffery/hal
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Travis Jeffery
committed
May 30, 2012
1 parent
50aeabf
commit bca1a9e
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Get directions between two locations | ||
# | ||
# hubot get directions "<origin>" "<destination>" -- Shows directions between these locations. | ||
|
||
parse_directions = (body) -> | ||
directions = JSON.parse body | ||
first_route = directions.routes[0] | ||
|
||
if not first_route | ||
return "Sorry, boss. Couldn't find directions" | ||
|
||
final_directions = [] | ||
|
||
for leg in first_route.legs | ||
do (leg) -> | ||
final_directions.push "From: " + leg.start_address | ||
final_directions.push "To: " + leg.end_address | ||
|
||
for step in leg.steps | ||
do (step) -> | ||
instructions = step.html_instructions.replace /<[^>]+>/g, '' | ||
final_directions.push instructions + " (#{step.distance.text})" | ||
|
||
return final_directions.join("\n") | ||
|
||
module.exports = (robot) -> | ||
robot.respond /(get )?directions "((?:[^\\"]+|\\.)*)" "((?:[^\\"]+|\\.)*)"$/i, (msg) -> | ||
[origin, destination] = msg.match[2..3] | ||
|
||
msg | ||
.http("http://maps.googleapis.com/maps/api/directions/json") | ||
.query | ||
origin: origin, | ||
destination: destination, | ||
sensor: false | ||
.get() (err, res, body) -> | ||
msg.send parse_directions body |