Class: Strikeiron::TaxResult

Inherits:
Object
  • Object
show all
Defined in:
lib/strikeiron/tax_result.rb

Overview

Returned after performing a tax calculation

Attributes

  • from - The from Address.

  • to - The to Address.

  • tax_values - The TaxValue objects returned from Strikeiron.

  • total_tax - The total tax amount to be applied.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_values = {}) ⇒ TaxResult

Initialize the TaxResult object with the given options



13
14
15
# File 'lib/strikeiron/tax_result.rb', line 13

def initialize(default_values = {})
  default_values.each { |key, value| self.send "#{key}=", value }
end

Instance Attribute Details

#fromObject

Returns the value of attribute from.



10
11
12
# File 'lib/strikeiron/tax_result.rb', line 10

def from
  @from
end

#tax_valuesObject

Returns the value of attribute tax_values.



10
11
12
# File 'lib/strikeiron/tax_result.rb', line 10

def tax_values
  @tax_values
end

#toObject

Returns the value of attribute to.



10
11
12
# File 'lib/strikeiron/tax_result.rb', line 10

def to
  @to
end

#total_taxObject

Returns the value of attribute total_tax.



10
11
12
# File 'lib/strikeiron/tax_result.rb', line 10

def total_tax
  @total_tax
end

Class Method Details

.from_soap(response) ⇒ Object

Convert the object from the Strikeiron response



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/strikeiron/tax_result.rb', line 19

def from_soap(response)
  tax_values       = []
  response_records = response[:results][:tax_value_record]
  response_records = [ response_records ] if response_records.is_a?(Hash)

  response_records.each do |record|
    jurisdictions = record[:jurisdictions][:sales_tax_value_jurisdiction]
    jurisdictions = [ jurisdictions ] if jurisdictions.is_a?(Hash)

    tax_values << TaxValue.new(
      :category      => record[:category],
      :category_id   => record[:category_id],
      :tax_amount    => record[:sales_tax_amount].to_f,
      :jurisdictions => jurisdictions.map { |j| Jurisdiction.new(:fips => j[:fips], :name => j[:name], :tax_amount => j[:sales_tax_amount].to_f) }
    )
  end

  new(
    :from       => Address.new(response[:resolved_from_address]),
    :to         => Address.new(response[:resolved_to_address]),
    :tax_values => tax_values,
    :total_tax  => tax_values.inject(0) { |sum, tax_value| sum + tax_value.tax_amount }
  )
end