Permalink
Browse files

UspsStandardizer gem v0.1.0 released

  • Loading branch information...
1 parent 1827a0b commit 031c2a028ba8fc5bdc944014ead5244249da03ee @macedo committed Oct 7, 2011
View
@@ -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.
+
View
@@ -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"
View
@@ -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
@@ -1,4 +1,4 @@
-module UspsStandardizer
+module USPSStandardizer
module Version
MAJOR = 0
MINOR = 1
@@ -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{<td headers="\w+" height="34" valign="top" class="main" style="background:url\(images/table_gray\.gif\); padding:5px 10px;">(.*?)>Mailing Industry Information</a>}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('<br>')
+ 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
+
Oops, something went wrong.

0 comments on commit 031c2a0

Please sign in to comment.