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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
require 'geocoder/lookups/base'
require "geocoder/results/pc_miler"
require 'cgi' unless defined?(CGI) && defined?(CGI.escape)
module Geocoder::Lookup
class PcMiler < Base
# https://developer.trimblemaps.com/restful-apis/location/single-search/single-search-api/#test-the-api-now
def valid_region_codes
# AF: Africa
# AS: Asia
# EU: Europe
# ME: Middle East
# MX: Mexico
# NA: North America
# OC: Oceania
# SA: South America
%w[AF AS EU ME MX NA OC SA]
end
def name
"PCMiler"
end
private # ---------------------------------------------------------------
def base_query_url(query)
region_code = region(query)
if !valid_region_codes.include?(region_code)
raise "region_code '#{region_code}' is invalid. use one of #{valid_region_codes}." \
"https://developer.trimblemaps.com/restful-apis/location/single-search/single-search-api/#test-the-api-now"
end
"#{protocol}://singlesearch.alk.com/#{region_code}/api/search?"
end
def results(query)
return [] unless data = fetch_data(query)
if data['Locations']
add_metadata_to_locations!(data)
data['Locations']
else
[]
end
end
def add_metadata_to_locations!(data)
confidence = data['QueryConfidence']
data['Locations'].each do |location|
location['QueryConfidence'] = confidence
end
end
def query_url_params(query)
if query.reverse_geocode?
lat,lon = query.coordinates
formatted_query = "#{CGI.escape(lat)},#{CGI.escape(lon)}"
else
formatted_query = query.text.to_s
end
{
authToken: configuration.api_key,
query: formatted_query,
# to add additional metadata to response such as QueryConfidence
include: 'Meta'
}.merge(super(query))
end
def region(query)
query.options[:region] || query.options['region'] || configuration[:region] || "NA"
end
def check_response_for_errors!(response)
if response.code.to_i == 403
raise_error(Geocoder::RequestDenied) ||
Geocoder.log(:warn, "Geocoding API error: 403 API key does not exist")
else
super(response)
end
end
def supported_protocols
[:https]
end
end
end
|