Repository URL to install this package:
|
Version:
0.1.1 ▾
|
require 'http'
module Geocoder
class Client
API_BASE_URL = 'http://www.mapquestapi.com/geocoding/v1/address'
def initialize(key: ENV['QUESTAPI_KEY'])
@key = key
@default_options = { maxResults: 1, outFormat: 'json' }
end
def lat_long(location:, options: {})
response = HTTP.get(request_url(location, options.merge(default_options)))
parse(response)
end
private
attr_reader :key, :default_options
def request_url(location, user_options)
query_part = user_options.map { |key, value| "#{key}=#{value}" }.join('&')
"#{API_BASE_URL}?key=#{key}&location=#{URI.escape(location)}&#{query_part}"
end
def parse(response)
json_body = JSON.parse(response.body.to_s)
lat_long = json_body['results']&.first&.dig('locations')&.first&.dig('latLng')
[lat_long['lat'], lat_long['lng']] if lat_long
end
end
end