Sha256: 8cc94cdd78a703fb25d5c11e41bed211690ae1daf6e91d12a7d71e8f4b47a69a

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

require 'json'
require 'open-uri'

module GoogleTimezone

  class Error < StandardError; end

  class Base
    ALLOWED_PARAMS = [:language, :sensor, :timestamp, :client, :signature, :key]

    def initialize(*args)
      @lat, @lon = if args.first.is_a? Array
                     args.first
                   else
                     args[0..1]
                   end

      @options = extract_options!(args)
      @options.reject! { |key, _| !ALLOWED_PARAMS.include?(key) }
    end

    def fetch
      location = [@lat, @lon].join(',')
      params = { location: location, sensor: false, timestamp: Time.now.to_i }.merge(@options)
      result = get_result(params)
      Result.new(result)
    end

    def fetch!
      result = fetch
      raise(GoogleTimezone::Error.new(result.result)) unless result.success?
      result
    end

    private

    def hash_to_query(hash)
      hash.collect { |key, val| "#{key}=#{val}" }.join('&')
    end

    def url(params)
      "https://maps.googleapis.com/maps/api/timezone/json?#{hash_to_query(params)}"
    end

    def extract_options!(args)
      args.last.is_a?(::Hash) ? args.pop : {}
    end

    def get_result(params)
      open(url(params)) { |r| JSON.parse(r.read) }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
google_timezone-0.0.4 lib/google_timezone/base.rb