Class: NitroPay::Currency

Inherits:
Object
  • Object
show all
Defined in:
lib/nitro_pay/currency.rb

Overview

Currency Obj, useful methods to work with many different countries

Class Method Summary collapse

Class Method Details

.get_cents(amount) ⇒ Object

Get it cents from operator amount formatted



62
63
64
65
66
# File 'lib/nitro_pay/currency.rb', line 62

def self.get_cents(amount)
  aux = amount.to_s
  cents_length = 2
  aux[aux.length-cents_length, aux.length]
end

.have_cents?(amount) ⇒ Boolean

Receive a numeric form operator format & retrieve it



69
70
71
72
73
74
# File 'lib/nitro_pay/currency.rb', line 69

def self.have_cents?(amount)
  aux = "#{amount.to_f/100}" if amount.is_a?(String) || amount.is_a?(Integer)
  aux = amount.to_s if amount.is_a?Float
  cents = aux[aux.index('.')+1, aux.length]
  cents.length == 2 ? true : false
end

.to_decimal(amount) ⇒ Object

Receive it amount in Integer String (like 1000 that means 10,00 or 10.00) and convert to Decimal value



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/nitro_pay/currency.rb', line 45

def self.to_decimal(amount)
  # If it was passed a BigDecimal it must be passed to operator format
  amount = Currency.to_operator_str(amount) if amount.is_a?BigDecimal

  # Base vars
  aux_amount = amount
  cents_chars_counter = 2

  # Building the currency str like BigDecimal understands
  cents_str = aux_amount[aux_amount.length-cents_chars_counter..aux_amount.length]
  integer_str = aux_amount[0, aux_amount.length-cents_chars_counter]
  new_amount = "#{integer_str}.#{cents_str}"

  BigDecimal.new new_amount
end

.to_operator_str(amount) ⇒ Object

Receive it amount in Decimal and convert to operator str, like 10,00 or 10.00 to 1000



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/nitro_pay/currency.rb', line 5

def self.to_operator_str(amount)
  # Check invalid entry
  return nil if amount.nil?
  return amount.to_s if amount.is_a?Integer
  amount = format('%.2f', amount) if amount.to_s.float?
  return amount if amount.is_a?(String) && amount.index('.').nil? && amount.index(',').nil?

  if amount.is_a?String
    return amount if amount.index('.').nil? && amount.index(',').nil?

    amount = amount.remove('.')
    amount = amount.remove(',')

    return amount
  end

  # Convert from BigDecimal
  if amount.is_a?BigDecimal
    aux_amount_str = amount.to_s('F')
    cents = aux_amount_str[aux_amount_str.index('.'), aux_amount_str.length]

    # Check if there is a bug because the Decimal didn't recognize the second 0
    aux_amount_str = "#{aux_amount_str}0"if cents.index('.')

    return aux_amount_str.remove('.')
  end

  # Create the amount as String
  amount.is_a?(String) ? amount_str = amount : amount_str = amount.to_s
  amount_str_not_formatted = amount_str.remove('.').remove(',')

  # Create the full value
  cents_value = amount_str_not_formatted[amount_str_not_formatted.length-2, amount_str_not_formatted.length-1]
  integer_value = amount_str_not_formatted[0, amount_str_not_formatted.length-2]

  # The return constraint
  "#{integer_value}#{cents_value}"
end