From 031c2a028ba8fc5bdc944014ead5244249da03ee Mon Sep 17 00:00:00 2001 From: Rafael Macedo Date: Fri, 7 Oct 2011 13:50:44 -0300 Subject: [PATCH] UspsStandardizer gem v0.1.0 released --- README.rdoc | 26 + Rakefile | 4 +- lib/usps_standardizer.rb | 11 +- lib/usps_standardizer/version.rb | 2 +- lib/usps_standardizer/zip_lookup.rb | 76 ++ spec/fixtures/content.html | 906 ++++++++++++++++++++++ spec/usps_standardizer/zip_lookup_spec.rb | 19 + spec/usps_standardizer_spec.rb | 13 +- usps_standardizer.gemspec | 8 +- 9 files changed, 1055 insertions(+), 10 deletions(-) create mode 100644 lib/usps_standardizer/zip_lookup.rb create mode 100644 spec/fixtures/content.html create mode 100644 spec/usps_standardizer/zip_lookup_spec.rb diff --git a/README.rdoc b/README.rdoc index e621f00..fb220f0 100644 --- a/README.rdoc +++ b/README.rdoc @@ -1,2 +1,28 @@ = USPS Address Standardizer +== Installing + + $ require 'usps_standardizer' + $ gem install usps_standardizer + +== Usage + + $ result = USPSStandardizer.lookup_for(:address => '6216 eddington drive', :state => 'oh', :city => 'middletown') + +Or you can pass your own Mechanize object to fetch the information + + $ require 'usps_standardizer' + $ require 'mechanize' $ + $ browser = Mechanize.new { |agent| agent.user_agent_alias = 'Mac Safari' } + $ result = USPSStandardizer.lookup_for({:address => '6216 eddington drive', :state => 'oh', :city => 'middletown'}, browser) + +== License + +(The MIT License) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/Rakefile b/Rakefile index 3ddfcc2..bbf9b81 100644 --- a/Rakefile +++ b/Rakefile @@ -1,10 +1,10 @@ -require "bundler/gem_tasks" +require "bundler" Bundler::GemHelper.install_tasks require "rspec/core/rake_task" RSpec::Core::RakeTask.new -require "rdoc/task" +require 'rdoc/task' RDoc::Task.new do |t| t.rdoc_dir = "docs" t.title = "USPS Address Standardizer" diff --git a/lib/usps_standardizer.rb b/lib/usps_standardizer.rb index 142bef0..fd6949b 100644 --- a/lib/usps_standardizer.rb +++ b/lib/usps_standardizer.rb @@ -1,4 +1,13 @@ -module UspsStandardizer +# -*- encoding : utf-8 -*- +require 'mechanize' +module USPSStandardizer autoload :Version, "usps_standardizer/version" + autoload :ZipLookup, "usps_standardizer/zip_lookup" + + def self.lookup_for(options, mechanize = Mechanize.new) + z = USPSStandardizer::ZipLookup.new(options, mechanize) + z.std_address + end + end diff --git a/lib/usps_standardizer/version.rb b/lib/usps_standardizer/version.rb index 4b62d3d..a2a22bd 100644 --- a/lib/usps_standardizer/version.rb +++ b/lib/usps_standardizer/version.rb @@ -1,4 +1,4 @@ -module UspsStandardizer +module USPSStandardizer module Version MAJOR = 0 MINOR = 1 diff --git a/lib/usps_standardizer/zip_lookup.rb b/lib/usps_standardizer/zip_lookup.rb new file mode 100644 index 0000000..d99d876 --- /dev/null +++ b/lib/usps_standardizer/zip_lookup.rb @@ -0,0 +1,76 @@ +# -*- encoding : utf-8 -*- +require 'sanitize' +require 'mechanize' + +module USPSStandardizer + + class ZipLookup + + attr_accessor :address, :state, :city, :zipcode + + def initialize(options = {}, mechanize = Mechanize.new) + @address, @state, @city, @zipcode, @county = '', '', '', '', '' + @mechanize = mechanize + options.each do |name, value| + send("#{name}=", value) + end + end + + def std_address + return [] unless (content = get_std_address_content) + + content.gsub!(/\t|\n|\r/, '') + content.squeeze!(" ").strip! + + raw_matches = content.scan(%r{(.*?)>Mailing Industry Information}mi) + + raw_matches.inject([]) do |results, raw_match| + if raw_match[0] =~ /mailingIndustryPopup2\(([^\)]*)/i + @county = $1.split(',')[1].gsub(/'/, '') + end + + @address, city_state_zipcode = Sanitize.clean(raw_match[0], + :remove_contents => true, + :elements => %w[br] + ).strip.split('
') + if city_state_zipcode.sub_nonascii(' ').squeeze!(' ') =~ /^(.*)\s+(\w\w)\s(\d{5})(-\d{4})?/i + @city, @state, @zipcode = $1, $2, $3 + end + + results << {:address => @address, :city => @city, :state => @state, :county => @county} + results + end + end + + private + + def get_std_address_content + search_form = @mechanize.get('http://zip4.usps.com/zip4/').forms.first + + search_form.address2 = @address + search_form.city = @city + search_form.state = @state + + @mechanize.submit(search_form, search_form.buttons.first) + + return false unless @mechanize.page.search('p.mainRed').empty? + @mechanize.page.body + end + end +end + +String.class_eval do + def sub_nonascii(replacement) + chars = self.split("") + self.slice!(0..self.size) + chars.each do |char| + if char.ord < 33 || char.ord > 127 + self.concat(replacement) + else + self.concat(char) + end + end + self.to_s + end +end + diff --git a/spec/fixtures/content.html b/spec/fixtures/content.html new file mode 100644 index 0000000..2a85180 --- /dev/null +++ b/spec/fixtures/content.html @@ -0,0 +1,906 @@ + + + + + + + + + + + + + + + + + + + +USPS - ZIP Code Lookup - Find a ZIP + 4 Code By Address Results + + + + + + + + + + + + +
+ +
+ + + + + Skip Navigation + +
+ + USPS Home + +   |   + + FAQs + +
+
+ + + + + +
+

Find a ZIP + 4® Code By Address Results

+
+
+

You Gave Us

+ + +6216 eddington drive
+ +middletown oh  

+ + Lookup Another ZIP Code™
+
+ + + +
+
+
+ + + + + + + + + + + + + + + + + +
+

Full Address in Standard Format What is Standard Format?

 
+ + 6216 EDDINGTON ST
+ + LIBERTY TOWNSHIP OH  45044-9761 +
+
  + Mailing Industry Information +
+
+ +
+ +
+ + + + + + + + + + + + + + + + + + + +
Related Links
+
+ Calculate Postage
+ Calculate postage for your letter or package online!
+ Rate Calculator
 
+ Print Shipping Labels
+Print shipping labels from your desktop and pay online.
+ Click-N-Ship®
+ Other Postage
+
+
Residential and
+ Business Lookup

+
+ Find an address with WhitePages
+ People Search and
+ Business Search.
+
A service of WhitePages
+
+
+
+ + + + + + + + + + + + + + + + + + + + + +
+ + + diff --git a/spec/usps_standardizer/zip_lookup_spec.rb b/spec/usps_standardizer/zip_lookup_spec.rb new file mode 100644 index 0000000..9f5c786 --- /dev/null +++ b/spec/usps_standardizer/zip_lookup_spec.rb @@ -0,0 +1,19 @@ +require "spec_helper" + +describe USPSStandardizer::ZipLookup do + let(:content) { File.read(File.dirname(__FILE__) + "/../fixtures/content.html") } + + describe "std_address" do + it "return [] if invalid address" do + subject.stub(:get_std_address_content).and_return(false) + subject.std_address.should be_empty + end + it "return a standardized address" do + subject.stub(:get_std_address_content).and_return(content) + a = subject.std_address + a.should be_instance_of(Array) + a.should_not be_empty + end + end +end + diff --git a/spec/usps_standardizer_spec.rb b/spec/usps_standardizer_spec.rb index 6134404..87e9812 100644 --- a/spec/usps_standardizer_spec.rb +++ b/spec/usps_standardizer_spec.rb @@ -1,8 +1,17 @@ require "spec_helper" -describe UspsStandardizer do +describe USPSStandardizer do + it "has a version" do - UspsStandardizer::Version::STRING.should match(/^\d+\.\d+\.\d+$/) + USPSStandardizer::Version::STRING.should match(/^\d+\.\d+\.\d+$/) + end + + it "standards an address on usps website" do + result = USPSStandardizer.lookup_for(:address => '6216 eddington drive', :state => 'oh', :city => 'middletown') + result[0][:address].should == '6216 EDDINGTON ST' + result[0][:state].should == 'OH' + result[0][:city].should == 'LIBERTY TOWNSHIP' end + end diff --git a/usps_standardizer.gemspec b/usps_standardizer.gemspec index e41f9e9..c265b8e 100644 --- a/usps_standardizer.gemspec +++ b/usps_standardizer.gemspec @@ -4,11 +4,11 @@ require "usps_standardizer/version" Gem::Specification.new do |s| s.name = "usps_standardizer" - s.version = UspsStandardizer::Version::STRING + s.version = USPSStandardizer::Version::STRING s.authors = ["Rafael Macedo"] s.email = ["macedo.rafaelfernandes@gmail.com"] s.homepage = "http://github.com/rafaelmacedo/usps_standardizer" - s.summary = %q{TODO: Write a gem summary} + s.summary = "Ruby class to standardize U.S. postal addresses by referencing the U.S. Postal Service's web site" s.description = s.summary @@ -17,8 +17,8 @@ Gem::Specification.new do |s| s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.require_paths = ["lib"] - # specify any dependencies here; for example: + s.add_dependency "mechanize", "~> 2.0.1" + s.add_dependency "sanitize", "~> 2.0.3" s.add_development_dependency "rspec", "~> 2.6.0" - # s.add_runtime_dependency "rest-client" end