Class: TZDetect::GeonameParser

Inherits:
Object
  • Object
show all
Includes:
Parser
Defined in:
lib/tzdetect/parsers/geoname.rb

Constant Summary collapse

GEONAME_GEOCODE_TIMEZONE_API =
"http://api.geonames.org"

Instance Attribute Summary

Attributes included from Parser

#latitude, #longitude

Instance Method Summary collapse

Methods included from Parser

included, #initialize, #timezone

Instance Method Details

#geoname_paramsObject



25
26
27
28
29
# File 'lib/tzdetect/parsers/geoname.rb', line 25

def geoname_params
  username = Configuration.username
  raise "No geoname useraname" if username.nil?
  "/timezoneJSON?lat=#{@latitude}&lng=#{@longitude}&username=#{username}"
end

#timezone!Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/tzdetect/parsers/geoname.rb', line 10

def timezone! 
  begin

    url =URI("#{GEONAME_GEOCODE_TIMEZONE_API}#{geoname_params}")
    Net::HTTP.start(url.host, url.port, :use_ssl => false) do |http| 
      request = Net::HTTP::Get.new(url.request_uri)
      response = http.request request # Net::HTTPResponse object
      json_result =JSON.parse(response.read_body)["timezoneId"]
      raise "No result" if json_result.nil?
      return json_result
    end
  rescue Exception => e
    raise TZDetect::Error::Parser, e.message
  end
end