Class: FriendlyShipping::Services::UpsJson::ParseMoneyHash

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

Class Method Summary collapse

Class Method Details

.call(money_hash, key_name) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/friendly_shipping/services/ups_json/parse_money_hash.rb', line 7

def self.call(money_hash, key_name)
  return unless money_hash

  monetary_value = money_hash['MonetaryValue']&.to_d
  return if monetary_value.zero?

  currency_code = money_hash['CurrencyCode']
  currency = Money::Currency.new(currency_code)
  amount = Money.new(monetary_value * currency.subunit_to_unit, currency)

  surcharge_code = money_hash['Code']
  label = UPS_SURCHARGE_CODES[surcharge_code] || surcharge_code || key_name

  [label, amount]
end