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
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