Class: Timezone::Lookup::Google
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
Basic
|
|
Instance Chain:
self,
Basic
|
|
Inherits: |
Timezone::Lookup::Basic
|
Defined in: | lib/timezone/lookup/google.rb |
Constant Summary
-
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.
'ZERO_RESULTS'
Class Method Summary
Instance Attribute Summary
Instance Method Summary
- #lookup(lat, long)
- #authorize(url) private
- #url(lat, long) private
Basic
- Inherited
Constructor Details
.new(config) ⇒ Google
Instance Attribute Details
#use_google_enterprise? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'lib/timezone/lookup/google.rb', line 54
def use_google_enterprise? !config.client_id.nil? end
Instance Method Details
#authorize(url) (private)
[ GitHub ]# File 'lib/timezone/lookup/google.rb', line 58
def (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
#lookup(lat, long)
[ GitHub ]# 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 /^2\d\d$/.match?(response.code) 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. ) end
#url(lat, long) (private)
[ GitHub ]# File 'lib/timezone/lookup/google.rb', line 73
def url(lat, long) query = URI.encode_www_form( 'location' => "#{lat},#{long}", 'timestamp' => Time.now.to_i ) ("/maps/api/timezone/json?#{query}") end