The rich text element allows you to create and format headings, paragraphs, blockquotes, images, and video all in one place instead of having to add and format them individually. Just double-click and easily create content.

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

You will need to sign up for a API key in order to use Lob's Address Verifcation API. It's free for domestic address verification.

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

1 2 3 4 5 6 7 8 185 Berry St Unit 1510 San Francisco CA 185 Berry St Ste 1510 SF CA 185 Berry St SF CA 185 NW Berry St , SF CA 1600 Pennsylvania Ave , Washington , DC , 20500 5904 Richmond Hwy Ste 340 Alexandria VA 22303 - 1864 5905 Richmond Hwy Ste 340 Alexandria VA

‍



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.

‍