Class: TimestampMaker::TimeZoneLookupers::GeoName

Inherits:
Object
  • Object
show all
Defined in:
lib/timestamp_maker/time_zone_lookupers/geo_name.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username:) ⇒ GeoName

Returns a new instance of GeoName.



14
15
16
# File 'lib/timestamp_maker/time_zone_lookupers/geo_name.rb', line 14

def initialize(username:)
  @username = username
end

Instance Attribute Details

#usernameObject

Returns the value of attribute username.



12
13
14
# File 'lib/timestamp_maker/time_zone_lookupers/geo_name.rb', line 12

def username
  @username
end

Instance Method Details

#lookup(latitude:, longitude:) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/timestamp_maker/time_zone_lookupers/geo_name.rb', line 18

def lookup(latitude:, longitude:)
  query = URI.encode_www_form(
    [['lat', latitude], ['lng', longitude], ['username', username]]
  )
  response = Net::HTTP.get_response(URI.parse("#{ENDPOINT}?#{query}"))
  raise "Got HTTP status code: #{response.code}" unless response.is_a?(Net::HTTPSuccess)

  parsed = JSON.parse(response.body)
  parsed['timezoneId']
end