Lob's website experience is not optimized for Internet Explorer.
Please choose another browser.

Arrow Up to go to top of page
Hero Image for Lob Deep Dives Blog PostAddress Parsing & Validation in Ruby
Engineering
July 13, 2015

Address Parsing & Validation in Ruby

Share this post

In this post I am going to show you how to take a text file full of raw mailing address data, parse each one into its component parts, and then run them through our address validation service. At the end of the tutorial you will have an output.csv file that contains valid mailing addreses split into address, city, state, zip, and country fields.

Just want the code? Check out our GitHub Repository

Pre Reqs

Install Dependancies

gem install StreetAddress
gem install lob

We will be using the StreetAddress Gem to parse the address strings, and then use the Lob Gem to verify and cleanse the addresses.

input.txt


This is the input file we will be feeding to our address parsing script. The script assumes it will be named input.txt.

As a result, you should make sure that this file is a plain text file with one address per a line.

Address Parsing Script

Create a blank file called verify.rb and lets get started.

require 'csv'
require 'lob'
require 'street_address'

# Initialize Lob object
Lob.api_key = 'test_0dc8d51e0acffcb1880e0f19c79b2f5b0cc'
@lob = Lob.load

First thing we need to do is include the necessary gems and then initialize the Lob ruby library. You can find your API key here.

# Open our output.csv for writing
output = File.open(File.expand_path('../output.csv', __FILE__), 'w')

# Add the CSV column headers
output.puts ['address_line1', 'address_city', 'address_state', 'address_zip', 'address_country'].join(',')

Next we want to create the output.csv file that will contain our parsed and validated addresses.

# Itterate through our input file.
File.open(File.expand_path('../input.txt', __FILE__)).each_line do |line|
parsed_address = StreetAddress::US.parse(line)

verified_address = @lob.addresses.verify(
address_line1: parsed_address.to_s(:line1),
address_city: parsed_address.city,
address_state: parsed_address.state,
address_country: 'US'
)['address']

output.puts [
verified_address['address_line1'],
verified_address['address_city'],
verified_address['address_state'],
verified_address['address_zip'],
verified_address['address_country']
].join(',')

end

output.close

Finally, we open up the input.txt file and itterate through each line in the file.

For each address line, we first parse the address using the StreetAddress gem. We then take the component parts of the address and pass it to the Lob Address Verification Service.

Once the address has been parsed and verified we then write it to our output.csv file.

Testing

Open up your terminal and run:

ruby verify.rb

If all goes as expected, you should now see an output.csv file in our directory. If you are running into any issues, leave a comment and we will get back to you.

Continue Reading