Class: TZDetect::GoogleGeocode

Inherits:
Object
  • Object
show all
Includes:
Geocoder
Defined in:
lib/tzdetect/geocoders/google.rb

Constant Summary collapse

GOOGLE_GEOCODE_API =
"https://maps.google.com/maps/api/geocode/json"

Instance Attribute Summary

Attributes included from Geocoder

#city, #country, #latitude, #longitude, #region, #timezone

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Geocoder

#fetched_data, included, #initialize, #postion

Class Method Details

.fetch_by_address!(address) ⇒ Object

Find position by addresss



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/tzdetect/geocoders/google.rb', line 23

def self.fetch_by_address! address
  begin 
    url =URI(GOOGLE_GEOCODE_API)
    url.query = URI.encode_www_form(self.params(address))
    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"
        return  json_result["results"][0]["geometry"]["location"]
      else 
        raise "no results"
      end
    end
  rescue Exception => e
    raise TZDetect::Error::GeoCoder, e.message
  end
end

.params(address) ⇒ Object



43
44
45
# File 'lib/tzdetect/geocoders/google.rb', line 43

def self.params address
  {:address=> address, :sensor=>"false"}.merge(Configuration.google_params) 
end

Instance Method Details

#position!Object

Returns position



12
13
14
15
16
17
18
19
# File 'lib/tzdetect/geocoders/google.rb', line 12

def position! 
    other = @region.nil? ? @country : "#{@region},#{@country}"
    address = "#{@city},#{other}"
    position = self.class.fetch_by_address!(address)
    @latitude = position["lat"]
    @longitude = position["lng"]
    return fetched_data
end