class Simplepay::Support::Amount

Amazon often represents dollar values as a combination of a value and a currency. In several types of requests, the combination is required for communication.

At the present time, Amazon only uses USD.

Attributes

amount[R]
currency[R]

Public Class Methods

new(amount, currency = Simplepay::Currency::USD) click to toggle source
# File lib/simplepay/support/amount.rb, line 18
def initialize(amount, currency = Simplepay::Currency::USD)
  self.amount   = amount
  self.currency = currency
end

Public Instance Methods

amount=(amount) click to toggle source

Sets the amount of the currency value, such as “1” for 1 USD. This amount cannot be negative.

# File lib/simplepay/support/amount.rb, line 28
def amount=(amount)
  raise(ArgumentError, "Amount cannot be nil") unless amount
  raise(ArgumentError, "Amount cannot be negative") if amount < 0
  @amount = BigDecimal.new(amount.to_s)
end
currency=(currency) click to toggle source

Sets the type of currency to use in the transaction. The parameter can either be a known currency code (see Simplepay::Currency) or a custom Simplepay::Currency::Currency instance.

# File lib/simplepay/support/amount.rb, line 39
def currency=(currency)
  raise(ArgumentError, "Invalid currency, expected Simplepay::Support::Currency or currency code string.") unless currency.kind_of?(Simplepay::Support::Currency) || currency.kind_of?(String)
  if currency.kind_of?(String)
    currency =  Simplepay::Currencies.detect { |c| c.code == currency } ||
                raise(ArgumentError, "Invalid currency, '#{currency}'. Must be one of: #{Simplepay::Currencies.join(', ')}")
  end
  @currency = currency
end
to_s() click to toggle source
# File lib/simplepay/support/amount.rb, line 48
def to_s
  "#{currency.code} #{currency.format % amount}"
end