You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
718 B
31 lines
718 B
require 'json'
|
|
require 'open-uri'
|
|
require 'uri'
|
|
|
|
require_relative 'alfred'
|
|
|
|
Geocoder = Struct.new(:api_key) do
|
|
def self.geocode(location)
|
|
geocoder.geocode(location)
|
|
end
|
|
|
|
def self.geocoder
|
|
return @geocoder if defined?(@geocoder)
|
|
|
|
@geocoder = self.new(Alfred::Config['GOOGLE_API_KEY'])
|
|
end
|
|
|
|
def geocode(location)
|
|
url = 'https://maps.googleapis.com/maps/api/geocode/json'
|
|
query = URI.encode_www_form(address: location, api_key: api_key)
|
|
response = JSON.load(open("#{url}?#{query}"))
|
|
result = response['results'][0]
|
|
|
|
name = result['formatted_address']
|
|
location = result['geometry']['location']
|
|
lat, long = location.values_at('lat', 'lng')
|
|
|
|
[name, lat, long]
|
|
end
|
|
end
|