Class: Lita::Handlers::Directions

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/directions.rb

Instance Method Summary collapse

Instance Method Details

#get_directions(response) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/lita/handlers/directions.rb', line 11

def get_directions(response)
  if Lita.config.handlers.directions.google_api_key.nil?
    response.reply("Please get an API key from https://console.developers.google.com, and follow the instructions on https://github.com/cashman04/lita-directions to configure.")
    return
  end
  from = response.matches[0][0]
  to = response.matches[0][1]
  json_ip_url = URI.escape("https://maps.googleapis.com/maps/api/directions/json?origin=#{from}&destination=#{to}&key=#{Lita.config.handlers.directions.google_api_key}")
  results = JSON.parse(RestClient.get(json_ip_url))
  if Lita.config.robot.log_level == :debug
    puts "from: #{from}"
    puts "to: #{to}"
    puts results
  end
  if results['routes'].any?
    response.reply("It is a #{results['routes'][0]['legs'][0]['distance']['text'].gsub('mi', 'mile')} drive from #{results['routes'][0]['legs'][0]['start_address'].gsub(', USA', '').gsub(/\d{5}/, '')} to #{results['routes'][0]['legs'][0]['end_address'].gsub(', USA', '').gsub(/\d{5}/, '')}. The approximate drive time is #{results['routes'][0]['legs'][0]['duration']['text'].gsub('mins', 'minutes')}.")
    response.reply(URI.encode("https://www.google.com/maps/dir/#{from}/#{to}/"))
  else
    response.reply(["Umm, you might want to double-check that.", "I think you did it wrong.", "There was an error, I think it was your fault.", "I'm pretty sure you messed up.", "Are you sure you read the directions?"].sample)
  end
end