Class: Eztz::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/eztz/client.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: Eztz.api_key) ⇒ Client

Returns a new instance of Client.



14
15
16
17
# File 'lib/eztz/client.rb', line 14

def initialize(api_key: Eztz.api_key)
  @api_key = api_key
  @uri = URI("https://maps.googleapis.com/maps/api/timezone/json")
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



12
13
14
# File 'lib/eztz/client.rb', line 12

def api_key
  @api_key
end

Instance Method Details

#timezone(location:, timestamp: Time.now.utc.to_i, language: "en") ⇒ Eztz::TimeZoneResponse

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

Parameters:

  • 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]).

  • timestamp (Integer) (defaults to: Time.now.utc.to_i)

    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.

  • language (String) (defaults to: "en")

    The language in which to return results Defaults to ‘en’. A list of supported languages can be found at developers.google.com/maps/faq#languagesupport

Returns:

Raises:

  • (ArgumentError)

    if location is not provided

  • (ApiError)

    if the API returns an error response.



36
37
38
39
40
41
# File 'lib/eztz/client.rb', line 36

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