diff options
author | Jared Norman <jared@super.gd> | 2020-07-09 12:20:23 -0700 |
---|---|---|
committer | Jared Norman <jared@super.gd> | 2020-07-14 13:07:55 -0700 |
commit | fa3abe77f2e05ccabda975c82eec73929e1b3bee (patch) | |
tree | f1df8217b05c7b97a1f23415c248a693b6b1a649 | |
parent | c348c0be29f772b4a0ca32ee3061f74a602e2bd9 (diff) |
Add ability to fetch address possibilities
-rw-r--r-- | lib/super_good/solidus_taxjar/addresses.rb | 22 | ||||
-rw-r--r-- | spec/super_good/solidus_taxjar/addresses_spec.rb | 142 |
2 files changed, 162 insertions, 2 deletions
diff --git a/lib/super_good/solidus_taxjar/addresses.rb b/lib/super_good/solidus_taxjar/addresses.rb index 8103269..0993a03 100644 --- a/lib/super_good/solidus_taxjar/addresses.rb +++ b/lib/super_good/solidus_taxjar/addresses.rb @@ -5,6 +5,10 @@ module SuperGood def normalize(spree_address) new.normalize(spree_address) end + + def possibilities(spree_address) + new.possibilities(spree_address) + end end def initialize(api: ::SuperGood::SolidusTaxJar.api) @@ -12,7 +16,7 @@ module SuperGood end def normalize(spree_address) - taxjar_address = api.validate_spree_address(spree_address).first + taxjar_address = taxjar_addresses(spree_address).first return if taxjar_address.nil? @@ -25,10 +29,26 @@ module SuperGood }) end + def possibilities(spree_address) + taxjar_addresses(spree_address).map { |taxjar_address| + Spree::Address.immutable_merge(spree_address, { + country: us, # TaxJar only supports the US currently. + state: state(taxjar_address.state), + zipcode: taxjar_address.zip, + city: taxjar_address.city, + address1: taxjar_address.street + }) + } + end + private attr_reader :api + def taxjar_addresses(spree_address) + api.validate_spree_address(spree_address) + end + def us Spree::Country.find_by iso: "US" end diff --git a/spec/super_good/solidus_taxjar/addresses_spec.rb b/spec/super_good/solidus_taxjar/addresses_spec.rb index b0c0c76..a5042d1 100644 --- a/spec/super_good/solidus_taxjar/addresses_spec.rb +++ b/spec/super_good/solidus_taxjar/addresses_spec.rb @@ -104,7 +104,7 @@ RSpec.describe SuperGood::SolidusTaxJar::Addresses do ] } - it "uses the first result to sanitize the addrses" do + it "uses the first result to sanitize the addresses" do expect(subject).to eq( build( :address, @@ -121,4 +121,144 @@ RSpec.describe SuperGood::SolidusTaxJar::Addresses do end end end + + describe "#normalize" do + subject { described_class.new(api: dummy_api).possibilities(spree_address) } + + let(:spree_address) { + create( + :address, + address1: "475 North Beverly Drive", + city: "Los Angeles", + country: country_us, + first_name: "Chuck", + last_name: "Schuldiner", + phone: "1-250-555-4444", + state: state_california, + zipcode: "90210" + ) + } + + let(:country_us) { + Spree::Country.create!( + iso3: "USA", + iso: "US", + iso_name: "UNITED STATES", + name: "United States", + numcode: 840, + states_required: true + ) + } + + let(:state_california) { + Spree::State.create!( + abbr: "CA", + country: country_us, + name: "California" + ) + } + + let(:dummy_api) { + instance_double ::SuperGood::SolidusTaxJar::API + } + + before do + allow(dummy_api) + .to receive(:validate_spree_address) + .with(spree_address) + .and_return(results) + end + + context "when there are no possibilities for the address" do + let(:results) { [] } + + it { is_expected.to be_empty } + end + + context "when there is one possibility for the address" do + let(:results) { + [ + Taxjar::Address.new( + country: "US", + state: "CA", + zip: "90210-4606", + city: "Beverly Hills", + street: "475 N Beverly Dr" + ) + ] + } + + it "returns the possibilities" do + expect(subject).to eq([ + build( + :address, + address1: "475 N Beverly Dr", + city: "Beverly Hills", + country: country_us, + first_name: "Chuck", + last_name: "Schuldiner", + phone: "1-250-555-4444", + state: state_california, + zipcode: "90210-4606" + ) + ]) + end + end + + context "when there are multiple possibilities for the address" do + let(:results) { + [ + Taxjar::Address.new( + country: "US", + state: "CA", + zip: "90210-4606", + city: "Beverly Hills", + street: "475 N Beverly Dr" + ), + Taxjar::Address.new( + country: "US", + state: "AZ", + zip: "90213-1234", + city: "Phoenix", + street: "473 N Beverly Dr" + ) + ] + } + + let!(:state_arizona) { + Spree::State.create!( + abbr: "AZ", + country: country_us, + name: "Arizona" + ) + } + + it "returns all the possibilities" do + expect(subject).to eq([ + build( + :address, + address1: "475 N Beverly Dr", + city: "Beverly Hills", + country: country_us, + first_name: "Chuck", + last_name: "Schuldiner", + phone: "1-250-555-4444", + state: state_california, + zipcode: "90210-4606" + ), + build( + :address, + address1: "473 N Beverly Dr", + city: "Phoenix", + country: country_us, + first_name: "Chuck", + last_name: "Schuldiner", + phone: "1-250-555-4444", + state: state_arizona, + zipcode: "90213-1234" + ) + ]) + end + end + end end |