Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 49 lines (40 sloc) 1.271 kb
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
# Description:
# Get directions between two locations
#
# Dependencies:
# None
#
# Configuration:
# None
#
# Commands:
# hubot get directions "<origin>" "<destination>" -- Shows directions between these locations
#
# Author:
# sleekslush

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
Something went wrong with that request. Please try again.