Browse files

Change Address so all attributes are upcased upon write

  • Loading branch information...
1 parent 6276116 commit 18b736ad46433fd5bdca96eea106d7694fb96198 @mcmire committed Dec 19, 2011
View
20 lib/address_standardization/address.rb
@@ -14,15 +14,17 @@ class Address
}
ATTRIBUTE_DEFS.each do |duck|
- if Array === duck
- name, aliases = duck[0], duck[1..-1]
- attr_accessor name
- aliases.each do |a|
- alias_method a, name
- alias_method "#{a}=", "#{name}="
- end
- else
- attr_accessor duck
+ duck = [duck] unless Array === duck
+ name, aliases = duck[0], duck[1..-1]
+
+ attr_reader name
+ define_method(:"#{name}=") do |value|
+ instance_variable_set("@#{name}", value.to_s.upcase)
+ end
+
+ aliases.each do |a|
+ alias_method a, name
+ alias_method "#{a}=", "#{name}="
end
end
View
10 lib/address_standardization/melissa_data.rb
@@ -62,11 +62,11 @@ def get_live_response(address_info)
county_row = table.search('tr.tdresul01')[4]
county = county_row.inner_text.match(/County ([A-Za-z ]+)/)[1].strip
city, state, zip = Helpers.strip_html(city_state_zip_part).split(separator)
- addr.street = street.upcase
- addr.city = city.upcase
- addr.province = state.upcase
- addr.postal_code = zip.upcase
- addr.district = county.upcase
+ addr.street = street
+ addr.city = city
+ addr.province = state
+ addr.postal_code = zip
+ addr.district = county
return addr
end
View
62 spec/google_maps_spec.rb
@@ -13,14 +13,14 @@
:city => "Mountain View",
:state => "CA"
)
- addr.should == AddressStandardization::Address.new(
- "street" => "1600 Amphitheatre Pkwy",
- "city" => "Mountain View",
- "county" => "Santa Clara",
- "state" => "CA",
- "zip" => "94043",
- "country" => "United States"
- )
+ addr.attributes.should == {
+ :street => "1600 AMPHITHEATRE PKWY",
+ :city => "MOUNTAIN VIEW",
+ :district => "SANTA CLARA",
+ :region => "CA",
+ :postal_code => "94043",
+ :country => "UNITED STATES"
+ }
end
it "returns the correct data for a valid Canadian address" do
@@ -30,21 +30,21 @@
:city => "Vancouver",
"province" => "BC"
)
- addr.should == AddressStandardization::Address.new(
- "street" => "55 E Cordova St",
- "city" => "Vancouver",
- "district" => "Greater Vancouver Regional District",
- "province" => "BC",
- "postal_code" => "V6A 1K3",
- "country" => "Canada"
- )
+ addr.attributes.should == {
+ :street => "55 E CORDOVA ST",
+ :city => "VANCOUVER",
+ :district => "GREATER VANCOUVER REGIONAL DISTRICT",
+ :region => "BC",
+ :postal_code => "V6A 1K3",
+ :country => "CANADA"
+ }
end
it "returns nothing for an invalid address" do
addr = AddressStandardization::GoogleMaps.standardize_address(
:street => "123 Imaginary Lane",
- :city => "Some Town",
- :state => "AK"
+ :city => "Some Town",
+ :state => "AK"
)
addr.should be_nil
end
@@ -62,11 +62,14 @@
:city => "Some Town",
:state => "AK"
)
- addr.should == AddressStandardization::Address.new(
- :street => "123 Imaginary Lane",
- :city => "Some Town",
- :state => "AK"
- )
+ addr.attributes.should == {
+ :street => "123 IMAGINARY LANE",
+ :city => "SOME TOWN",
+ :district => nil,
+ :region => "AK",
+ :postal_code => nil,
+ :country => nil
+ }
AddressStandardization::GoogleMaps.canned_response = nil
# block form
@@ -76,11 +79,14 @@
:city => "Some Town",
:state => "AK"
)
- addr.should == AddressStandardization::Address.new(
- :street => "123 Imaginary Lane",
- :city => "Some Town",
- :state => "AK"
- )
+ addr.attributes.should == {
+ :street => "123 IMAGINARY LANE",
+ :city => "SOME TOWN",
+ :district => nil,
+ :region => "AK",
+ :postal_code => nil,
+ :country => nil
+ }
end
end
View
117 spec/melissa_data_spec.rb
@@ -6,119 +6,96 @@
AddressStandardization.test_mode = nil
end
- it "returns the correct data for a valid US address (with implicit country)" do
+ it "returns the correct data for a valid US address" do
addr = AddressStandardization::MelissaData.standardize_address(
# test that it works regardless of symbols or strings
- :street => "1 Infinite Loop",
- "city" => "Cupertino",
+ "street" => "1600 Amphitheatre Parkway",
+ :city => "Mountain View",
:state => "CA"
)
- addr.should == AddressStandardization::Address.new(
- "street" => "1 INFINITE LOOP",
- "city" => "CUPERTINO",
- "district" => "SANTA CLARA",
- "state" => "CA",
- "zip" => "95014-2083",
- "country" => "United States"
- )
- end
-
- it "returns the correct data for a valid US address (with explicit country)" do
- addr = AddressStandardization::MelissaData.standardize_address(
- # test that it works regardless of symbols or strings
- :street => "1 Infinite Loop",
- :city => "Cupertino",
- "state" => "CA",
- "country" => "USA"
- )
- addr.should == AddressStandardization::Address.new(
- "street" => "1 INFINITE LOOP",
- "city" => "CUPERTINO",
- "district" => "SANTA CLARA",
- "state" => "CA",
- "zip" => "95014-2083",
- "country" => "United States"
- )
- end
-
- it "returns nothing for an invalid US address" do
- addr = AddressStandardization::MelissaData.standardize_address(
- :street => "123 Imaginary Lane",
- :city => "Some Town",
- :state => "AK"
- )
- addr.should == nil
+ addr.attributes.should == {
+ :street => "1600 AMPHITHEATRE PKWY",
+ :city => "MOUNTAIN VIEW",
+ :district => "SANTA CLARA",
+ :region => "CA",
+ :postal_code => "94043-1351",
+ :country => "UNITED STATES"
+ }
end
it "returns the correct data for a valid Canadian address" do
addr = AddressStandardization::MelissaData.standardize_address(
# test that it works regardless of symbols or strings
- "street" => "55 Cordova St E #415",
+ :street => "55 East Cordova St. Apt 415",
:city => "Vancouver",
- "province" => "BC",
- # FIXME: This must be postalcode, it doesn't work with zip...
- :postalcode => "V6A0A5",
- :country => "CANADA"
- )
- addr.should == AddressStandardization::Address.new(
- "street" => "415-55 CORDOVA ST E",
- "city" => "VANCOUVER",
- "province" => "BC",
- "postal_code" => "V6A 0A5",
- "country" => "Canada"
+ "province" => "BC"
)
+ addr.attributes.should == {
+ :street => "55 E CORDOVA ST",
+ :city => "VANCOUVER",
+ :district => "GREATER VANCOUVER REGIONAL DISTRICT",
+ :region => "BC",
+ :postal_code => "V6A 1K3",
+ :country => "CANADA"
+ }
end
- it "returns nothing for an invalid Canadian address" do
+ it "returns nothing for an invalid address" do
addr = AddressStandardization::MelissaData.standardize_address(
:street => "123 Imaginary Lane",
- :city => "Some Town",
- :province => "BC"
+ :city => "Some Town",
+ :state => "AK"
)
addr.should be_nil
end
end
context 'in test mode' do
- before :each do
+ before do
AddressStandardization.test_mode = true
end
- it "returns the correct data for a valid address" do
+ it "returns the correct data for a successful response" do
AddressStandardization::MelissaData.canned_response = :success
addr = AddressStandardization::MelissaData.standardize_address(
:street => "123 Imaginary Lane",
:city => "Some Town",
- :province => "BC"
- )
- addr.should == AddressStandardization::Address.new(
- :street => "123 Imaginary Lane",
- :city => "Some Town",
- :province => "BC"
+ :state => "AK"
)
+ addr.attributes.should == {
+ :street => "123 IMAGINARY LANE",
+ :city => "SOME TOWN",
+ :district => nil,
+ :region => "AK",
+ :postal_code => nil,
+ :country => nil
+ }
AddressStandardization::MelissaData.canned_response = nil
# block form
AddressStandardization::MelissaData.with_canned_response(:success) do
addr = AddressStandardization::MelissaData.standardize_address(
:street => "123 Imaginary Lane",
:city => "Some Town",
- :province => "BC"
- )
- addr.should == AddressStandardization::Address.new(
- :street => "123 Imaginary Lane",
- :city => "Some Town",
- :province => "BC"
+ :state => "AK"
)
+ addr.attributes.should == {
+ :street => "123 IMAGINARY LANE",
+ :city => "SOME TOWN",
+ :district => nil,
+ :region => "AK",
+ :postal_code => nil,
+ :country => nil
+ }
end
end
- it "returns the correct data for an invalid address" do
+ it "returns nothing for an unsuccessful response" do
AddressStandardization::MelissaData.canned_response = :failure
addr = AddressStandardization::MelissaData.standardize_address(
:street => "123 Imaginary Lane",
:city => "Some Town",
- :province => "BC"
+ :state => "AK"
)
addr.should be_nil
AddressStandardization::MelissaData.canned_response = nil
@@ -128,7 +105,7 @@
addr = AddressStandardization::MelissaData.standardize_address(
:street => "123 Imaginary Lane",
:city => "Some Town",
- :province => "BC"
+ :state => "AK"
)
addr.should be_nil
end

0 comments on commit 18b736a

Please sign in to comment.