Class: FriendlyShipping::Services::UpsJson::ParseRateModifierHash

Inherits:
Object
  • Object
show all
Defined in:
lib/friendly_shipping/services/ups_json/parse_rate_modifier_hash.rb

Class Method Summary collapse

Class Method Details

.call(rate_modifier, currency_code:) ⇒ Array

Returns The label and the amount of the rate modifier.

Parameters:

  • hash (Hash)

    the RateModifier hash from the source JSON

  • currency_code (String)

    The currency code for this modifier's amount (i.e. 'USD')

Returns:

  • (Array)

    The label and the amount of the rate modifier



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/friendly_shipping/services/ups_json/parse_rate_modifier_hash.rb', line 10

def self.call(rate_modifier, currency_code:)
  return unless rate_modifier

  amount = rate_modifier['Amount'].to_d
  return if amount.zero?

  currency = Money::Currency.new(currency_code)
  amount = Money.new(amount * currency.subunit_to_unit, currency)

  modifier_type = rate_modifier['ModifierType']
  modifier_description = rate_modifier['ModifierDesc']
  label = "#{modifier_type} (#{modifier_description})"

  [label, amount]
end