Sha256: 2fbbd049185af0b972cfb0258c4b2cd51ccfc95719eecc3629fd2b7c17801ed3
Contents?: true
Size: 1.82 KB
Versions: 11
Compression:
Stored size: 1.82 KB
Contents
require 'timezone/lookup/basic' require 'timezone/error' require 'json' require 'uri' require 'base64' require 'openssl' require 'cgi' module Timezone module Lookup # @!visibility private class Google < ::Timezone::Lookup::Basic def initialize(config) if config.api_key.nil? raise(::Timezone::Error::InvalidConfig, 'missing api key'.freeze) end config.protocol ||= 'https'.freeze config.url ||= 'maps.googleapis.com'.freeze super end def lookup(lat, long) response = client.get(url(lat, long)) if response.code == '403'.freeze raise(Timezone::Error::Google, '403 Forbidden'.freeze) end return unless response.code =~ /^2\d\d$/ data = JSON.parse(response.body) if data['status'.freeze] != 'OK'.freeze raise(Timezone::Error::Google, data['errorMessage']) end data['timeZoneId'.freeze] rescue => e raise(Timezone::Error::Google, e.message) end private def use_google_enterprise? !config.client_id.nil? end 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 def url(lat, long) query = URI.encode_www_form( 'location' => "#{lat},#{long}", 'timestamp' => Time.now.to_i ) authorize("/maps/api/timezone/json?#{query}") end end end end
Version data entries
11 entries across 11 versions & 1 rubygems