Class: Timezone::Lookup::Geonames

Inherits:
Basic
  • Object
show all
Defined in:
lib/timezone/lookup/geonames.rb

Instance Attribute Summary

Attributes inherited from Basic

#config

Instance Method Summary collapse

Methods inherited from Basic

#client

Constructor Details

#initialize(config) ⇒ Geonames

Returns a new instance of Geonames.



9
10
11
12
13
14
15
# File 'lib/timezone/lookup/geonames.rb', line 9

def initialize(config)
  if config.username.nil?
    raise(::Timezone::Error::InvalidConfig, 'missing username')
  end

  super
end

Instance Method Details

#lookup(lat, lng) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/timezone/lookup/geonames.rb', line 17

def lookup(lat, lng)
  response = client.get(url(lat, lng))

  return unless response.code =~ /^2\d\d$/

  data = JSON.parse(response.body)

  if data['status'] && data['status']['value'] == 18
    raise(Timezone::Error::GeoNames, 'api limit reached')
  end

  data['timezoneId']
rescue => e
  raise(Timezone::Error::GeoNames, e.message)
end