class Eztz::Client

The Google Time Zone API client. If you need to make requests with multiple api keys, then you'll probably want to use this class directly. Otherwise, it will be easier to use {::Eztz#timezone Eztz.timezone}

Attributes

api_key[R]
uri[R]

Public Class Methods

new(api_key: Eztz.api_key) click to toggle source
# File lib/eztz/client.rb, line 13
def initialize(api_key: Eztz.api_key)
  @api_key = api_key
  @uri = URI('https://maps.googleapis.com/maps/api/timezone/json')
end

Public Instance Methods

timezone(location:, timestamp: Time.now.utc.to_i, language: 'en') click to toggle source

Gets timezone information for a location on earth, as well as that location's time offset from UTC.

@param location [String, Array] a comma-separated lat,lng tuple

(eg. "-33.86,151.20"), representing the location to look up. Can also be
an array (eg. [-33.86, 151.20]).

@param timestamp [Integer] specifies the desired time as seconds

since midnight, January 1, 1970 UTC. The Google Maps Time Zone API uses
the timestamp to determine whether or not Daylight Savings should be
applied. Times before 1970 can be expressed as negative values.
Defaults to the current time.

@param language [String] The language in which to return results

Defaults to 'en'. A list of supported languages can be found at
https://developers.google.com/maps/faq#languagesupport

@raise [ArgumentError] if location is not provided @raise [ApiError] if the API returns an error response. @return [Eztz::TimeZoneResponse] the resulting timezone data.

# File lib/eztz/client.rb, line 35
def timezone(location:, timestamp: Time.now.utc.to_i, language: 'en')
  uri.query = query_params(location, timestamp, language)
  res = Net::HTTP.get_response(uri)
  raise ApiError, res.body unless res.is_a?(Net::HTTPSuccess)
  TimeZoneResponse.new(timestamp, JSON.parse(res.body))
end

Private Instance Methods

query_params(location, timestamp, language) click to toggle source
# File lib/eztz/client.rb, line 46
def query_params(location, timestamp, language)
  URI.encode_www_form(
    location: (location.respond_to?(:join) ? location.join(',') : location),
    timestamp: timestamp,
    language: language,
    key: api_key
  )
end