Sha256: 2b04583c2fa2c98f84aad6446867bf89e4fbcb84c657a7f12afaec8b58b26b93

Contents?: true

Size: 1.2 KB

Versions: 7

Compression:

Stored size: 1.2 KB

Contents

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_key[1],
        :channel => Geocoder::Configuration.api_key[2]
      }.reject{ |key, value| value.nil? }
      path = "/maps/api/geocode/json?#{hash_to_query(params)}"
      "#{protocol}://maps.googleapis.com#{path}&signature=#{sign(path)}"
    end

    def sign(string)
      raw_private_key = url_safe_base64_decode(Geocoder::Configuration.api_key[0])
      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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
geocoder-1.1.3 lib/geocoder/lookups/google_premier.rb
geocoder-1.1.2 lib/geocoder/lookups/google_premier.rb
geocoder-1.1.1 lib/geocoder/lookups/google_premier.rb
geocoder-1.1.0 lib/geocoder/lookups/google_premier.rb
geocoder-1.0.5 lib/geocoder/lookups/google_premier.rb
geocoder-1.0.4 lib/geocoder/lookups/google_premier.rb
geocoder-1.0.3 lib/geocoder/lookups/google_premier.rb