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



13
14
15
16
# 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

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



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

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.

Raises:

  • (ArgumentError)

    if location is not provided

  • (ApiError)

    if the API returns an error response.



35
36
37
38
39
40
# 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