require 'json'
require 'open-uri'

module GoogleTimezone
  class Base
    @allowed_params = [:language, :sensor, :timestamp, :client, :signature]

    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, value| !@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_error(result.result) unless result.success?
      result
    end

    private
    def hash_to_query(hash)
      require 'cgi' unless defined?(CGI) && defined?(CGI.escape)
      hash.collect{ |p|
        p[1].nil? ? nil : p.map{ |i| CGI.escape i.to_s } * '='
      }.compact.sort * '&'
    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) ? pop : {}
    end

    def get_result(params)
      open(url(params)) { |r| JSON.parse(r.read) }
    end
  end
end
require 'json'
require 'open-uri'

module GoogleTimezone
  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, value| !@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_error(result.result) unless result.success?
      result
    end

    private
    def hash_to_query(hash)
      require 'cgi' unless defined?(CGI) && defined?(CGI.escape)
      hash.collect{ |p|
        p[1].nil? ? nil : p.map{ |i| CGI.escape i.to_s } * '='
      }.compact.sort * '&'
    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) ? pop : {}
    end

    def get_result(params)
      open(url(params)) { |r| JSON.parse(r.read) }
    end
  end
end
Add key as allowed param
Add key as allowed param

Google's timezone API documentation indicates that all apps should use
an API key for requests.
https://developers.google.com/maps/documentation/timezone/#api_key

The "key" parameter was not in the list of allowed_params and would be
rejected. This change simply adds :key to the list of allowed_params.
