Class: GoogleGeocoding::Geocoder

Inherits:
Object
  • Object
show all
Defined in:
lib/google_geocoding/geocoder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Geocoder

Creates a new geocoder instance.

See Also:

  • Geocoder#geocode


9
10
11
12
13
14
15
# File 'lib/google_geocoding/geocoder.rb', line 9

def initialize(options = {})
  @options = options
  @sess = Patron::Session.new
  @sess.timeout = options[:timeout] || 10
  @sess.base_url = "http://maps.google.com/maps/geo"
  @sess.headers['User-Agent'] = options[:user_agent]
end

Class Method Details

.query(address, options = {}) ⇒ Object



3
4
5
# File 'lib/google_geocoding/geocoder.rb', line 3

def self.query(address, options = {})
  self.new(options).query(address)
end

Instance Method Details

#query(address) ⇒ Response

Performs a geocoding request against google for the given address.

Parameters:

  • (String, #to_s)

Returns:



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/google_geocoding/geocoder.rb', line 21

def query(address)
  params = "?q=#{URI.encode(address.to_s)}"
  params << "&key=#{@options[:api_key]}" if @options[:api_key]
  resp = @sess.get(params)
  
  if (200...300).include?(resp.status)
    Response.new(resp.body)
  else
    raise Errors::HttpError.new(address, resp)
  end
end