Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for full and abbreviated states. #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/indirizzo/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Address
attr_accessor :street
attr_accessor :city
attr_accessor :state
attr_accessor :full_state
attr_accessor :zip, :plus4
attr_accessor :country
attr_accessor :options
Expand All @@ -40,7 +41,7 @@ def clean (value)
end

def assign_text_to_address(text)
@text, @city, @street, @number, @prenum, @sufnum, @full_state, @state, @zip, @plus4, @country = AddressHashExtractor.extract(text, @options)
@text, @city, @street, @number, @prenum, @sufnum, @full_state, @abbr_state, @state, @zip, @plus4, @country = AddressHashExtractor.extract(text, @options)
end

def expand_numbers (string)
Expand Down
4 changes: 3 additions & 1 deletion lib/indirizzo/address_hash_extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def extract
handle_hash
end

return @text, @city, @street, @number, @prenum, @sufnum, @full_state, @state, @zip, @plus4, @country
return @text, @city, @street, @number, @prenum, @sufnum, @full_state, @abbr_state, @state, @zip, @plus4, @country
end

private
Expand Down Expand Up @@ -69,6 +69,8 @@ def handle_state
@state = State[@state] if @state.length > 2
elsif !address_hash[:state].nil?
@state = address_hash[:state]
@abbr_state = State.select{|k,v| k == @state}.values.first || @state
@full_state = State.select{|k,v| k == @state || v == @state}.keys.first
elsif !address_hash[:country].nil?
@state = address_hash[:country]
end
Expand Down
16 changes: 14 additions & 2 deletions test/test_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ def test_new_raises_exception_with_no_text
end
end

def test_text_parsing_city
addr = Address.new("Lake Forest")
assert_equal ["lake forest"], addr.city
end

def test_new
addr = Address.new("1600 Pennsylvania Av., Washington DC")
assert_equal "1600 Pennsylvania Av, Washington DC", addr.text
Expand Down Expand Up @@ -43,7 +48,7 @@ def test_skip_parse
addresses = [
{:street => "1233 Main St", :city => "Springfield", :region => "VA", :postal_code => "12345", :final_number => "1233", :parsed_street => "main st"},
{:street => "somewhere Ln", :city => "Somewhere", :region => "WI", :postal_code => "22222", :number => "402", :parsed_street => "somewhere ln", :final_number => "402"},
{:street => "somewhere Ln", :city => "Somewhere", :state => "WI", :postal_code => "22222", :number => "402", :parsed_street => "somewhere ln", :final_number => "402"},
{:street => "somewhere Ln", :city => "Somewhere", :state => "WI", :abbr_state => "WI", :full_state => "Wisconsin", :postal_code => "22222", :number => "402", :parsed_street => "somewhere ln", :final_number => "402"},
]
for preparsed_address in addresses
address_for_geocode = Address.new preparsed_address
Expand All @@ -52,6 +57,8 @@ def test_skip_parse
assert_equal preparsed_address[:city],address_for_geocode.city[0]
assert_equal preparsed_address[:region],address_for_geocode.state if preparsed_address[:region]
assert_equal preparsed_address[:state],address_for_geocode.state if preparsed_address[:state]
assert_equal preparsed_address[:full_state],address_for_geocode.full_state if preparsed_address[:state] || preparsed_address[:region]
assert_equal preparsed_address[:abbr_state],address_for_geocode.state if preparsed_address[:state]
assert_equal preparsed_address[:postal_code],address_for_geocode.zip
end
end
Expand All @@ -73,7 +80,7 @@ def test_address_hash
{:address => "Arlington, VA", :place_check => ["arlington"]}
]
for preparsed_address in addresses
address_for_geocode = Address.new preparsed_address
address_for_geocode = Address.new preparsed_address
assert_equal preparsed_address[:place_check],address_for_geocode.city
end
end
Expand Down Expand Up @@ -153,30 +160,35 @@ def check_city(fixture)
:street => "Pennsylvania Ave",
:city => "Washington",
:state => "DC",
:full_state => "washington dc",
:zip => "20050"},

{:text => "1600 Pennsylvania, Washington DC",
:number => "1600",
:street => "Pennsylvania",
:city => "Washington",
:full_state => "washington dc",
:state => "DC"},

{:text => "1600 Pennsylvania Washington DC",
:number => "1600",
:city => "Washington",
:street => "Pennsylvania",
:full_state => "washington dc",
:state => "DC"},

{:text => "1600 Pennsylvania Washington",
:pending => true,
:number => "1600",
:street => "Pennsylvania",
:city => "Washington",
:full_state => "washington dc",
:state => "DC"},

{:text => "1600 Pennsylvania 20050",
:number => "1600",
:state => "PA",
:full_state => "pennsylvania",
:zip => "20050"},

{:text => "1600 Pennsylvania Av, Washington DC 20050-9999",
Expand Down