Module: NodexPay::AmountSerializer

Defined in:
lib/nodex_pay/amount_serializer.rb

Constant Summary collapse

MINIMUM =
"0.000001"
MAXIMUM =
"100000000"
DECIMAL_PATTERN =
/\A(?:0|[1-9][0-9]*)(?:\.[0-9]+)?\z/

Class Method Summary collapse

Class Method Details

.serialize(amount) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/nodex_pay/amount_serializer.rb', line 11

def serialize(amount)
  raw = case amount
        when String
          validate_string!(amount)
          amount
        when Integer
          amount.to_s
        else
          bigdecimal_value(amount)
        end

  normalized = normalize(raw)
  validate_string!(normalized)
  unless compare_decimals(normalized, MINIMUM) >= 0 && compare_decimals(normalized, MAXIMUM) <= 0
    raise ValidationError, "amount must be between 0.000001 and 100000000"
  end

  normalized
rescue ArgumentError
  raise ValidationError, "amount must be a plain decimal value"
end