class Timezone::Lookup::Google

@!visibility private

Constants

NO_TIMEZONE_INFORMATION

Indicates that no time zone data could be found for the specified <lat, lng>. This can occur if the query is incomplete or ambiguous.

Public Class Methods

new(config) click to toggle source
Calls superclass method Timezone::Lookup::Basic::new
# File lib/timezone/lookup/google.rb, line 19
def initialize(config)
  if config.api_key.nil?
    raise(::Timezone::Error::InvalidConfig, 'missing api key')
  end

  config.protocol ||= 'https'
  config.url ||= 'maps.googleapis.com'

  super
end

Public Instance Methods

lookup(lat, long) click to toggle source
# File lib/timezone/lookup/google.rb, line 30
def lookup(lat, long)
  response = client.get(url(lat, long))

  if response.code == '403'
    raise(Timezone::Error::Google, '403 Forbidden')
  end

  return unless response.code =~ /^2\d\d$/
  data = JSON.parse(response.body)

  return if data['status'] == NO_TIMEZONE_INFORMATION

  if data['status'] != 'OK'
    raise(Timezone::Error::Google, data['errorMessage'])
  end

  data['timeZoneId']
rescue StandardError => e
  raise(Timezone::Error::Google, e.message)
end

Private Instance Methods

authorize(url) click to toggle source
# File lib/timezone/lookup/google.rb, line 57
def authorize(url)
  if use_google_enterprise?
    url += "&client=#{CGI.escape(config.client_id)}"

    sha1 = OpenSSL::Digest.new('sha1')
    binary_key = Base64.decode64(config.api_key.tr('-_', '+/'))
    binary_signature = OpenSSL::HMAC.digest(sha1, binary_key, url)
    signature = Base64.encode64(binary_signature).tr('+/', '-_').strip

    url + "&signature=#{signature}"
  else
    url + "&key=#{config.api_key}"
  end
end
url(lat, long) click to toggle source
# File lib/timezone/lookup/google.rb, line 72
def url(lat, long)
  query = URI.encode_www_form(
    'location' => "#{lat},#{long}",
    'timestamp' => Time.now.to_i
  )

  authorize("/maps/api/timezone/json?#{query}")
end
use_google_enterprise?() click to toggle source
# File lib/timezone/lookup/google.rb, line 53
def use_google_enterprise?
  !config.client_id.nil?
end