Skip to content

Instantly share code, notes, and snippets.

@libryder
Created June 27, 2012 23:06
Show Gist options
  • Select an option

  • Save libryder/3007474 to your computer and use it in GitHub Desktop.

Select an option

Save libryder/3007474 to your computer and use it in GitHub Desktop.
Simple USPS API for verifying addresses
class USPSAddress
EXEMPT_ADDRESS_PARTS = ["NW", "NE", "SW", "SE"]
def initialize
@usps = USPS.new(USPS_USER_ID)
end
def parse_address(address)
location = Location.new(address)
response = @usps.verify_address(location)
if (response[/error/])
response
else
format_address(response)
end
end
def format_address(full_address)
formatted_address = ''
address = "#{full_address[:address2]} #{full_address[:address1]}".split(' ').each { |str|
if EXEMPT_ADDRESS_PARTS.include?(str)
formatted_address << "#{str} "
else
formatted_address << "#{str.titleize} "
end
}
{
:address => formatted_address.strip,
:city => full_address[:city].titleize,
:state => full_address[:state],
:zip => full_address[:zip5]
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment