Skip to content

Instantly share code, notes, and snippets.

@jamesgecko
Created May 23, 2012 20:31
Show Gist options
  • Select an option

  • Save jamesgecko/2777617 to your computer and use it in GitHub Desktop.

Select an option

Save jamesgecko/2777617 to your computer and use it in GitHub Desktop.
Load address info
# Public: Split an address string into structured data
# If you want structured data, don't let the user enter it unstructured!
# This function won't work for everything
#
# Returns a Hash containing :address, :name, :street, :zip, :state, and :city
def split_address(address_string)
result = {}
address = address_string.split("\n")
result[:name] = address[0]
result[:street] = address[1]
# The next line is probably either a PO Box or the city/state/zip line
if address[2].downcase == 'po'
result[:street] << ' ' + address[2]
address.delete_at 2
end
city_state_zip = address[2].strip.split(" ")
result[:zip] = city_state_zip.pop
result[:state] = city_state_zip.pop
result[:city] = city_state_zip
result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment