summaryrefslogtreecommitdiff
path: root/lib/super_good/solidus_taxjar/tax_rate_calculator.rb
blob: dc3d77160a2eaf8a68f0683c8f2adf4ea7572dd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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