summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJared Norman <jared@super.gd>2019-01-21 18:13:04 -0800
committerJared Norman <jared@super.gd>2019-01-21 18:13:04 -0800
commitd5b84c70cfad6bcb98bee422ce2865dbf8ae386f (patch)
tree7bbc38161af1fe51a578e474caf0e6d392894329 /lib
parent9c2d4462f4d8626f95fb21d2a920ad7b087d4254 (diff)
Cache requests for ten minutes
Diffstat (limited to 'lib')
-rw-r--r--lib/super_good/solidus_taxjar/tax_calculator.rb47
1 files changed, 41 insertions, 6 deletions
diff --git a/lib/super_good/solidus_taxjar/tax_calculator.rb b/lib/super_good/solidus_taxjar/tax_calculator.rb
index 445087a..d683cf6 100644
--- a/lib/super_good/solidus_taxjar/tax_calculator.rb
+++ b/lib/super_good/solidus_taxjar/tax_calculator.rb
@@ -12,13 +12,16 @@ module SuperGood
def calculate
return no_tax if order.tax_address.empty?
- return no_tax unless taxjar_breakdown
- Spree::Tax::OrderTax.new(
- order_id: order.id,
- line_item_taxes: line_item_taxes,
- shipment_taxes: shipment_taxes
- )
+ cache do
+ next no_tax unless taxjar_breakdown
+
+ Spree::Tax::OrderTax.new(
+ order_id: order.id,
+ line_item_taxes: line_item_taxes,
+ shipment_taxes: shipment_taxes
+ )
+ end
end
private
@@ -99,6 +102,38 @@ module SuperGood
def tax_rate
Spree::TaxRate.find_by(name: "Sales Tax")
end
+
+ def cache
+ if !Rails.env.test?
+ Rails.cache.fetch(cache_key, expires_in: 10.minutes) { yield }
+ else
+ yield
+ end
+ end
+
+ def cache_key
+ 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: -line_item.adjustment_total,
+ product_tax_code: line_item.tax_category&.tax_code
+ }
+ end.hash
+ }
+ end
end
end
end