Skip to content
Snippets Groups Projects
Commit 46ab278a authored by Steve Hoeksema's avatar Steve Hoeksema Committed by Alex Reisner
Browse files

Add Google Premier geocoder

parent d5dcaf58
No related branches found
No related tags found
No related merge requests found
......@@ -56,7 +56,7 @@ module Geocoder
# All street address lookups, default first.
#
def street_lookups
[:google, :yahoo, :bing, :geocoder_ca, :yandex]
[:google, :google_premier, :yahoo, :bing, :geocoder_ca, :yandex]
end
##
......
......@@ -24,6 +24,12 @@ module Geocoder
# API key for geocoding service
[:api_key, nil],
# API client for geocoding service
[:api_client, nil],
# API channel for geocoding service
[:api_channel, nil],
# cache object (must respond to #[], #[]=, and #keys)
[:cache, nil],
......
require 'openssl'
require 'base64'
require 'geocoder/lookups/google'
require 'geocoder/results/google_premier'
module Geocoder::Lookup
class GooglePremier < Google
private # ---------------------------------------------------------------
def query_url(query, reverse = false)
params = {
(reverse ? :latlng : :address) => query,
:sensor => 'false',
:language => Geocoder::Configuration.language,
:client => Geocoder::Configuration.api_client,
:channel => Geocoder::Configuration.api_channel
}.reject{ |key, value| value.nil? }
path = "/maps/api/geocode/json?#{hash_to_query(params)}"
signature = sign(path)
"#{protocol}://maps.googleapis.com#{path}&signature=#{signature}"
end
def sign(string)
raw_private_key = url_safe_base64_decode(Geocoder::Configuration.api_key)
digest = OpenSSL::Digest::Digest.new('sha1')
raw_signature = OpenSSL::HMAC.digest(digest, raw_private_key, string)
url_safe_base64_encode(raw_signature)
end
def url_safe_base64_decode(base64_string)
Base64.decode64(base64_string.tr('-_', '+/'))
end
def url_safe_base64_encode(raw)
Base64.encode64(raw).tr('+/', '-_').strip
end
end
end
require 'geocoder/results/google'
module Geocoder::Result
class GooglePremier < Google
end
end
\ No newline at end of file
......@@ -33,6 +33,24 @@ class ServicesTest < Test::Unit::TestCase
end
# --- Google Premier ---
def test_google_premier_result_components
Geocoder::Configuration.lookup = :google_premier
result = Geocoder.search("Madison Square Garden, New York, NY").first
assert_equal "Manhattan",
result.address_components_of_type(:sublocality).first['long_name']
end
def test_google_premier_query_url
Geocoder::Configuration.api_key = "deadbeef"
Geocoder::Configuration.api_client = "gme-test"
Geocoder::Configuration.api_channel = "test-dev"
assert_equal "http://maps.googleapis.com/maps/api/geocode/json?address=Madison+Square+Garden%2C+New+York%2C+NY&channel=test-dev&client=gme-test&language=en&sensor=false&signature=doJvJqX7YJzgV9rJ0DnVkTGZqTg=",
Geocoder::Lookup::GooglePremier.new.send(:query_url, "Madison Square Garden, New York, NY", false)
end
# --- Yahoo ---
def test_yahoo_result_components
......
......@@ -73,6 +73,9 @@ module Geocoder
end
end
class GooglePremier < Google
end
class Yahoo < Base
private #-----------------------------------------------------------------
def fetch_raw_data(query, reverse = false)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment