Module: Paypal::Util

Included in:
Base
Defined in:
lib/paypal/util.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.formatted_amount(x) ⇒ Object



6
7
8
9
# File 'lib/paypal/util.rb', line 6

def self.formatted_amount(x)
  x = '0' if x == '' || x.nil?
  sprintf '%0.2f', BigDecimal(x.to_s)
end

.to_numeric(x) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/paypal/util.rb', line 11

def self.to_numeric(x)
  string = x.to_s
  string = "0" if string == ""  # Ruby 2.5 compatibility.

  decimal = BigDecimal(string)

  if decimal != BigDecimal(string).round(2)
    raise ArgumentError.new(
      'Precision cannot be higher than two decimal places. ' \
      'Truncate or round first.'
    )
  elsif decimal == x.to_i
    x.to_i
  else
    decimal
  end
end

Instance Method Details

#==(other) ⇒ Object



33
34
35
36
37
# File 'lib/paypal/util.rb', line 33

def ==(other)
  instance_variables.all? do |key|
    instance_variable_get(key) == other.instance_variable_get(key)
  end
end

#numeric_attribute?(key) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/paypal/util.rb', line 29

def numeric_attribute?(key)
  !!(key.to_s =~ /(amount|frequency|cycles|paid)/)
end