Class: Graticule::Geocoder::GeocoderUs

Inherits:
Rest
  • Object
show all
Defined in:
lib/graticule/geocoder/geocoder_us.rb

Overview

A library for lookup up coordinates with geocoder.us’ API.

geocoder.us/help/

Instance Method Summary collapse

Constructor Details

#initialize(user = nil, password = nil) ⇒ GeocoderUs

Creates a new GeocoderUs object optionally using username and password.

You can sign up for a geocoder.us account here:

geocoder.us/user/signup



15
16
17
18
19
20
21
22
23
# File 'lib/graticule/geocoder/geocoder_us.rb', line 15

def initialize(user = nil, password = nil)
  if user and password then
    @url = URI.parse 'http://geocoder.us/member/service/rest/geocode'
    @url.user = user
    @url.password = password
  else
    @url = URI.parse 'http://rpc.geocoder.us/service/rest/geocode'
  end
end

Instance Method Details

#check_error(xml) ⇒ Object

:nodoc:

Raises:



41
42
43
# File 'lib/graticule/geocoder/geocoder_us.rb', line 41

def check_error(xml) #:nodoc:
  raise AddressError, xml.text if xml.text == 'couldn\'t find this address! sorry'
end

#locate(address) ⇒ Object

Locates address and returns the address’ latitude and longitude or raises an AddressError.



27
28
29
# File 'lib/graticule/geocoder/geocoder_us.rb', line 27

def locate(address)
  get :address => address.is_a?(String) ? address : location_from_params(address).to_s(:country => false)
end

#parse_response(xml) ⇒ Object

:nodoc:



31
32
33
34
35
36
37
38
39
# File 'lib/graticule/geocoder/geocoder_us.rb', line 31

def parse_response(xml) #:nodoc:
  location = Location.new
  location.street = xml.elements['rdf:RDF/geo:Point/dc:description'].text

  location.latitude = xml.elements['rdf:RDF/geo:Point/geo:lat'].text.to_f
  location.longitude = xml.elements['rdf:RDF/geo:Point/geo:long'].text.to_f

  return location
end