From c7bcc30f9aa142694e84195063a6093f428950b4 Mon Sep 17 00:00:00 2001 From: andrea longhi Date: Mon, 13 Jan 2020 20:31:47 +0100 Subject: Add tax rate calculator class This new calculator allows to retrieve tax rate information starting from a `Spree::Address` model. It shares some logic with the tax calculator, for example the ability to handle exceptions using `SolidusTaxJar.exception_handler` lambda. --- .../solidus_taxjar/tax_rate_calculator.rb | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 lib/super_good/solidus_taxjar/tax_rate_calculator.rb (limited to 'lib/super_good/solidus_taxjar') diff --git a/lib/super_good/solidus_taxjar/tax_rate_calculator.rb b/lib/super_good/solidus_taxjar/tax_rate_calculator.rb new file mode 100644 index 0000000..dc3d771 --- /dev/null +++ b/lib/super_good/solidus_taxjar/tax_rate_calculator.rb @@ -0,0 +1,36 @@ +module SuperGood + module SolidusTaxJar + class TaxRateCalculator + include CalculatorHelper + def initialize(address, api: self.class.default_api) + @address = address + @api = api + end + + def calculate + return no_rate if SuperGood::SolidusTaxJar.test_mode + return no_rate if incomplete_address?(address) + return no_rate unless taxable_address?(address) + cache do + api.tax_rate_for(address).to_d + end + + rescue StandardError => e + exception_handler.(e) + no_rate + end + + private + + attr_reader :address, :api + + def no_rate + BigDecimal(0) + end + + def cache_key + SuperGood::SolidusTaxJar.cache_key.(address) + end + end + end +end -- cgit v1.2.3