summaryrefslogtreecommitdiff
path: root/lib/super_good/solidus_taxjar/tax_rate_calculator.rb
blob: 0096afc65c44a7fce9019123238e1f6fd6a7a5d3 (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
module SuperGood
  module SolidusTaxjar
    class TaxRateCalculator
      include CalculatorHelper
      def initialize(address, api: SuperGood::SolidusTaxjar.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 => e
        exception_handler.call(e)
        no_rate
      end

      private

      attr_reader :address, :api

      def no_rate
        BigDecimal(0)
      end

      def cache_key
        SuperGood::SolidusTaxjar.cache_key.call(address)
      end
    end
  end
end