Skip to content

Commit

Permalink
Add Google Premier geocoder
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Hoeksema authored and alexreisner committed Sep 13, 2011
1 parent d5dcaf5 commit 46ab278
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/geocoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def valid_lookups
# All street address lookups, default first.
#
def street_lookups
[:google, :yahoo, :bing, :geocoder_ca, :yandex]
[:google, :google_premier, :yahoo, :bing, :geocoder_ca, :yandex]
end

##
Expand Down
6 changes: 6 additions & 0 deletions lib/geocoder/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ def self.options_and_defaults
# 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],

Expand Down
43 changes: 43 additions & 0 deletions lib/geocoder/lookups/google_premier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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
6 changes: 6 additions & 0 deletions lib/geocoder/results/google_premier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'geocoder/results/google'

module Geocoder::Result
class GooglePremier < Google
end
end
18 changes: 18 additions & 0 deletions test/services_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ def test_google_precision
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
Expand Down
3 changes: 3 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ def fetch_raw_data(query, reverse = false)
end
end

class GooglePremier < Google
end

class Yahoo < Base
private #-----------------------------------------------------------------
def fetch_raw_data(query, reverse = false)
Expand Down

0 comments on commit 46ab278

Please sign in to comment.