Sha256: dae20fa4dd46f6469faad463a85518175853c08af62429d1880705c5098d39d3

Contents?: true

Size: 1.03 KB

Versions: 5

Compression:

Stored size: 1.03 KB

Contents

module Commands
  API_URL = 'https://maps.googleapis.com/maps/api/geocode/json?latlng='.freeze

  # Lookup based on location data from user's device
  def lookup_location
    if message_contains_location?
      handle_user_location
    else
      say("Please try your request again and use 'Send location' button")
    end
    stop_thread
  end

  def handle_user_location
    coords = message.attachments.first['payload']['coordinates']
    lat = coords['lat']
    long = coords['long']
    message.typing_on
    parsed = get_parsed_response(API_URL, "#{lat},#{long}")
    address = extract_full_address(parsed)
    say "Coordinates of your location: Latitude #{lat}, Longitude #{long}. " \
        "Looks like you're at #{address}"
    message.typing_off
  end

  # Talk to API
  def get_parsed_response(url, query)
    response = HTTParty.get(url + query)
    parsed = JSON.parse(response.body)
    parsed['status'] != 'ZERO_RESULTS' ? parsed : nil
  end

  def extract_full_address(parsed)
    parsed['results'].first['formatted_address']
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubotnik-0.2.3 templates/location.rb
rubotnik-0.2.2 templates/location.rb
rubotnik-0.2.1 templates/location.rb
rubotnik-0.2.0 templates/location.rb
rubotnik-0.1.1 templates/location.rb