diff options
author | Jared Norman <jared@super.gd> | 2019-01-30 13:07:40 -0800 |
---|---|---|
committer | Jared Norman <jared@super.gd> | 2019-01-30 13:07:40 -0800 |
commit | 8f3744abd7f06da848856136a7888c2b01b40ee5 (patch) | |
tree | 697c9c83e5db393f063823444282c0a32c053ae3 | |
parent | 0152c8c4d62300e52e02d1d841e1d59ee36add99 (diff) |
Integrate APIParams class
This removes the logic from the API class and moves all the calls to the
order_params method to our new module.
-rw-r--r-- | lib/super_good/solidus_taxjar.rb | 1 | ||||
-rw-r--r-- | lib/super_good/solidus_taxjar/api.rb | 30 | ||||
-rw-r--r-- | lib/super_good/solidus_taxjar/tax_calculator.rb | 2 | ||||
-rw-r--r-- | spec/super_good/solidus_taxjar/api_spec.rb | 134 |
4 files changed, 15 insertions, 152 deletions
diff --git a/lib/super_good/solidus_taxjar.rb b/lib/super_good/solidus_taxjar.rb index 82f9500..be56453 100644 --- a/lib/super_good/solidus_taxjar.rb +++ b/lib/super_good/solidus_taxjar.rb @@ -3,6 +3,7 @@ require 'solidus_support' require 'taxjar' require "super_good/solidus_taxjar/version" +require "super_good/solidus_taxjar/api_params" require "super_good/solidus_taxjar/api" require "super_good/solidus_taxjar/tax_calculator" require "super_good/solidus_taxjar/discount_calculator" diff --git a/lib/super_good/solidus_taxjar/api.rb b/lib/super_good/solidus_taxjar/api.rb index 2504216..1392a47 100644 --- a/lib/super_good/solidus_taxjar/api.rb +++ b/lib/super_good/solidus_taxjar/api.rb @@ -13,7 +13,7 @@ module SuperGood end def tax_for(order) - taxjar_client.tax_for_order order_params(order) + taxjar_client.tax_for_order APIParams.order_params(order) end def tax_rates_for(address) @@ -26,37 +26,9 @@ module SuperGood ) end - def order_params(order) - tax_address = order.tax_address - - { - to_country: tax_address.country.iso, - to_zip: tax_address.zipcode, - to_city: tax_address.city, - to_state: tax_address&.state&.abbr || tax_address.state_name, - to_street: tax_address.address1, - - shipping: order.shipment_total, - - line_items: order.line_items.map do |line_item| - { - id: line_item.id, - quantity: line_item.quantity, - unit_price: line_item.price, - discount: discount(line_item), - product_tax_code: line_item.tax_category&.tax_code - } - end - } - end - private attr_reader :taxjar_client - - def discount(line_item) - ::SuperGood::SolidusTaxJar.discount_calculator.new(line_item).discount - end end end end diff --git a/lib/super_good/solidus_taxjar/tax_calculator.rb b/lib/super_good/solidus_taxjar/tax_calculator.rb index a521f0d..b9e6444 100644 --- a/lib/super_good/solidus_taxjar/tax_calculator.rb +++ b/lib/super_good/solidus_taxjar/tax_calculator.rb @@ -121,7 +121,7 @@ module SuperGood end def cache_key - api.order_params(order).transform_values do |value| + APIParams.order_params(order).transform_values do |value| case value when Array, Hash then value.hash else diff --git a/spec/super_good/solidus_taxjar/api_spec.rb b/spec/super_good/solidus_taxjar/api_spec.rb index ef1a96d..2c35299 100644 --- a/spec/super_good/solidus_taxjar/api_spec.rb +++ b/spec/super_good/solidus_taxjar/api_spec.rb @@ -4,130 +4,20 @@ RSpec.describe SuperGood::SolidusTaxJar::API do describe "#tax_for" do subject { api.tax_for order } - let(:api) do - described_class.new(taxjar_client: dummy_client) - end - - let(:dummy_client) do - instance_double ::Taxjar::Client - end - - let(:order) do - Spree::Order.create!( - item_total: 28.00, - shipment_total: 3.01, - store: store, - ship_address: ship_address, - line_items: [line_item] - ) - end - - let(:store) do - Spree::Store.create!( - name: "Default Store", - url: "https://store.example.com", - code: "store", - mail_from_address: "contact@example.com", - cart_tax_country_iso: "US" - ) - end - - let(:ship_address) do - Spree::Address.create!( - country: country_us, - state: state_california, - zipcode: "90210", - city: "Los Angeles", - address1: "475 N Beverly Dr", - - first_name: "Chuck", - last_name: "Schuldiner", - phone: "1-250-555-4444" - ) - end - - let(:country_us) do - Spree::Country.create!( - iso_name: "UNITED STATES", - iso: "US", - iso3: "USA", - name: "United States", - numcode: 840, - states_required: true - ) - end - - let(:state_california) do - Spree::State.create!( - country: country_us, - name: "California", - abbr: "CA" - ) - end - - let(:line_item) do - Spree::LineItem.new( - variant: variant, - price: 10, - quantity: 3, - promo_total: -2 - ) - end - - let(:variant) do - Spree::Variant.create!( - product: product, - price: 10 - ) - end - - let(:product) do - Spree::Product.create!( - name: "Product Name", - shipping_category: shipping_category, - tax_category: tax_category, - master: master_variant, - variants: [master_variant] - ) - end - - let(:shipping_category) do - Spree::ShippingCategory.create!(name: "Default Category") - end - - let(:tax_category) do - Spree::TaxCategory.create!( - name: "Default", - is_default: true, - tax_code: "A_GEN_TAX" - ) - end - - let(:master_variant) do - Spree::Variant.new( - is_master: true, - price: 10 - ) - end + let(:api) { described_class.new(taxjar_client: dummy_client) } + let(:dummy_client) { instance_double ::Taxjar::Client } + let(:order) { Spree::Order.new } before do - allow(dummy_client).to receive(:tax_for_order).with( - to_country: "US", - to_zip: "90210", - to_city: "Los Angeles", - to_state: "CA", - to_street: "475 N Beverly Dr", - - shipping: 3.01, - - line_items: [{ - id: order.line_items.first.id, - quantity: 3, - unit_price: 10.00, - discount: 2.00, - product_tax_code: "A_GEN_TAX" - }] - ).and_return({ some_kind_of: "response" }) + allow(SuperGood::SolidusTaxJar::APIParams) + .to receive(:order_params) + .with(order) + .and_return({ order: "params" }) + + allow(dummy_client) + .to receive(:tax_for_order) + .with({ order: "params" }) + .and_return({ some_kind_of: "response" }) end it { is_expected.to eq({ some_kind_of: "response" }) } |