Created
May 23, 2012 20:31
-
-
Save jamesgecko/2777617 to your computer and use it in GitHub Desktop.
Load address info
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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