Class: GeocoderUs
- Inherits:
-
Object
- Object
- GeocoderUs
- Defined in:
- lib/geocoder_us.rb
Overview
A library for lookup up coordinates with geocoder.us’ API.
Defined Under Namespace
Classes: AddressError, Error, LoginError
Instance Method Summary collapse
-
#initialize(user = nil, password = nil) ⇒ GeocoderUs
constructor
Creates a new GeocoderUs object optionally using
usernameandpassword. -
#locate(address) ⇒ Object
Locates
addressand returns the address’ latitude and longitude or raises an AddressError.
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:
33 34 35 36 37 38 39 40 41 |
# File 'lib/geocoder_us.rb', line 33 def initialize(user = nil, password = nil) if user and password then @url = URI.parse 'http://geocoder.us/member/service/csv' @url.user = user @url.password = password else @url = URI.parse 'http://rpc.geocoder.us/service/csv' end end |
Instance Method Details
#locate(address) ⇒ Object
Locates address and returns the address’ latitude and longitude or raises an AddressError.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/geocoder_us.rb', line 47 def locate(address) url = @url.dup url.query = "address=#{URI.escape address}" url.open do |f| response = f.read raise AddressError, $' if response =~ /^2: / lat, lon, = response.split ',', 3 lat = lat.to_f lon = lon.to_f return lat, lon end rescue OpenURI::HTTPError => e raise LoginError if e. =~ /^401 / end |