Class: Timezonedb::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/timezonedb/client.rb,
lib/timezonedb/client/error.rb,
lib/timezonedb/client/version.rb,
lib/timezonedb/client/response.rb

Defined Under Namespace

Classes: Error, Response

Constant Summary collapse

FREE_URL =
'http://api.timezonedb.com'
PREMIUM_URL =
'http://vip.timezonedb.com'
VERSION =
'0.2.0'

Instance Method Summary collapse

Constructor Details

#initialize(api_key, options = {}) ⇒ Client

Returns a new instance of Client.



28
29
30
31
32
# File 'lib/timezonedb/client.rb', line 28

def initialize(api_key, options = {})
  @api_key = api_key
  premium = options[:premium]
  @url = premium ? PREMIUM_URL : FREE_URL
end

Instance Method Details

#search_by_coords(latitude, longitude) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/timezonedb/client.rb', line 34

def search_by_coords(latitude, longitude)
  params = { key: @api_key, lat: latitude, lng: longitude, format: 'json' }

  response = RestClient.get @url, params: params
  response_hash = JSON.parse(response)

  if response_hash['status'] == 'OK'
    Response.new(response_hash)
  else
    fail Timezonedb::Client::Error.new(response_hash['message'])
  end
rescue RestClient::Exception => e
  raise Timezonedb::Client::Error.new(e.inspect)
end