Sha256: 7469c2e6c033ba53f3a60de279f64d2e0194300b51381813ae9ec181edf54826

Contents?: true

Size: 822 Bytes

Versions: 3

Compression:

Stored size: 822 Bytes

Contents

require 'timezone/lookup/basic'
require 'timezone/error'
require 'json'
require 'uri'

module Timezone
  module Lookup
    class Google < ::Timezone::Lookup::Basic
      def lookup(lat,lng)
        response = client.get(url(lat,lng))

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

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

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

      private

      def url(lat,lng)
          query = URI.encode_www_form(
            'location' => "#{lat},#{lng}",
            'timestamp' => Time.now.to_i,
            'key' => config.google_api_key)

          "/maps/api/timezone/json?#{query}"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
timezone-0.3.9 lib/timezone/lookup/google.rb
timezone-0.3.8 lib/timezone/lookup/google.rb
timezone-0.3.7 lib/timezone/lookup/google.rb