A tiny Ruby library to quickly standardize a postal address, either through MelissaData or Google Maps.
If you are using Rails, put this in your environment.rb:
rake gems:install to install the gem.
Otherwise, just run
gem install address_standardization
Right now this library supports two services: MelissaData and Google Maps.
addr = ::.( :street => "1 Infinite Loop", :city => "Cupertino", :state => "CA" )
This submits the address to MelissaData. If the address can't be found, you'll get back
nil. But if the address can be found (as in this case), you'll get an instance of
AddressStandardization::Address. If you store the instance, you can refer to the individual fields like so:
addr.street #=> "1 INFINITE LOOP" addr.city #=> "CUPERTINO" addr.state #=> "CA" addr.zip #=> "95014-2083" addr.country #=> "USA"
And standardizing a Canadian address:
addr = ::.( :street => "103 Metig St", :city => "Sault Ste Marie", :province => "ON", :country => "Canada" ) addr.street #=> "103 METIG ST RR 4" addr.city #=> "SAULT STE MARIE" addr.province #=> "ON" addr.postalcode #=> "P6A 5K9" addr.country #=> "CANADA"
Note that when standardizing a Canadian address, the
:country must be "Canada" (or "CANADA", or anything like that). Otherwise it will be treated as a US address.
Also note that I'm referring to the address's province as
province, but you can also use
state if you like. Same goes for the postal code -- you can also refer to it as
Using Google Maps to validate an address is just as easy:
addr = ::.( :street => "1600 Amphitheatre Parkway", :city => "Mountain View", :state => "CA" ) addr.street #=> "1600 AMPHITHEATRE PKWY" addr.city #=> "MOUNTAIN VIEW" addr.state #=> "CA" addr.zip #=> "94043" addr.country #=> "USA"
And, again, a Canadian address:
addr = ::.( :street => "1770 Stenson Blvd.", :city => "Peterborough", :province => "ON" ) addr.street #=> "1770 STENSON BLVD" addr.city #=> "PETERBOROUGH" addr.province #=> "ON" addr.postalcode #=> "K9K" addr.country #=> "CANADA"
Sharp eyes will notice that the Google Maps API doesn't return the full postal code for Canadian addresses. If you know why this is please let me know (my email address is below).
If you find any bugs with this plugin, feel free to:
- file a bug report in the Issues area on Github
- fork the project on Github and send me a pull request
- email me (*firstname* dot lastname at gmail dot com)
(c) 2008-2010 Elliot Winkler. Released under the MIT license.