Class: GeocoderUs
- Inherits:
-
RCRest
- Object
- RCRest
- GeocoderUs
- Defined in:
- lib/geocoder_us.rb
Overview
A library for lookup up coordinates with geocoder.us’ API.
Defined Under Namespace
Classes: AddressError, Error, Location, LoginError
Instance Method Summary collapse
- #check_error(xml) ⇒ Object
-
#initialize(user = nil, password = nil) ⇒ GeocoderUs
constructor
Creates a new GeocoderUs object optionally using
username
andpassword
. -
#locate(address) ⇒ Object
Locates
address
and returns the address’ latitude and longitude or raises an AddressError. - #parse_response(xml) ⇒ Object
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:
35 36 37 38 39 40 41 42 43 |
# File 'lib/geocoder_us.rb', line 35 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
63 64 65 |
# File 'lib/geocoder_us.rb', line 63 def check_error(xml) 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.
49 50 51 |
# File 'lib/geocoder_us.rb', line 49 def locate(address) get :address => address end |
#parse_response(xml) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/geocoder_us.rb', line 53 def parse_response(xml) location = Location.new location.address = 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 |