Class: TZDetect::GoogleParser

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

Constant Summary collapse

GOOGLE_GEOCODE_TIMEZONE_API =
"https://maps.googleapis.com/maps/api/timezone/json"

Instance Attribute Summary

Attributes included from Parser

#latitude, #longitude

Instance Method Summary collapse

Methods included from Parser

included, #initialize, #timezone

Instance Method Details

#paramsObject



30
31
32
33
# File 'lib/tzdetect/parsers/google.rb', line 30

def params
    timestamp = Time.now.to_i
    {:location =>"#{@latitude},#{@longitude}", :timestamp=>"#{timestamp}", :sensor=>"false"}.merge(Configuration.google_params) 
end

#timezone!Object



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

def timezone! 
  begin
    url =URI(GOOGLE_GEOCODE_TIMEZONE_API)
    url.query = URI.encode_www_form(self.params)
    Net::HTTP.start(url.host, url.port, :use_ssl => true) do |http| 
      request = Net::HTTP::Get.new(url.request_uri)
      response = http.request request # Net::HTTPResponse object
      json_result =JSON.parse(response.read_body)
      if json_result["status"] == "OK"
        json_result["timeZoneId"]
      else
        raise "No result"
      end
    end
  rescue Exception => e
    raise TZDetect::Error::Parser, e.message
  end
end