Class: CpfCnpjTools::Generator

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

Overview

Class responsible for generating and validating CPF and CNPJ numbers

Constant Summary collapse

CPF1DIGIT =
[10, 9, 8, 7, 6, 5, 4, 3, 2].freeze
CPF2DIGIT =
[11, 10, 9, 8, 7, 6, 5, 4, 3, 2].freeze
CNPJ1DIGIT =
[5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2].freeze
CNPJ2DIGIT =
[6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2].freeze

Instance Method Summary collapse

Instance Method Details

#cnpj_valid?(cnpj) ⇒ Boolean

Method for validating CNPJ numbers.

Parameters:

  • cpf (Integer, String)

Returns:

  • (Boolean)

    bool



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cpf_cnpj_tools.rb', line 62

def cnpj_valid?(cnpj)
  cnpj_array = remove_formatting(cnpj.to_s).split("").map!(&:to_i)
  first_digit = cnpj_array[-2]
  second_digit = cnpj_array[-1]
  base_cnpj = cnpj_array[0..11]
  calculated_first_digit = generate_identifier(base_cnpj, true, cpf: false)
  calculated_second_digit = generate_identifier(base_cnpj << calculated_first_digit, false, cpf: false)
  return false if (first_digit != calculated_first_digit) || (second_digit != calculated_second_digit)

  true
end

#cpf_valid?(cpf) ⇒ Boolean

Method for validating CPF numbers.

Parameters:

  • cpf (String, Integer)

Returns:

  • (Boolean)

    bool



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cpf_cnpj_tools.rb', line 46

def cpf_valid?(cpf)
  cpf_array = remove_formatting(cpf.to_s).split("").map!(&:to_i)
  first_digit = cpf_array[-2]
  second_digit = cpf_array[-1]
  base_cpf = cpf_array[0..8]
  calculated_first_digit = generate_identifier(base_cpf, true)
  calculated_second_digit = generate_identifier(base_cpf << calculated_first_digit, false)
  return false if (first_digit != calculated_first_digit) || (second_digit != calculated_second_digit)

  true
end

#format(cpf_or_cnpj) ⇒ Object

Returns a String containing a formatted CPF/CNPJ.

Parameters:

  • cpf_or_cnpj (String, Integer)

Returns:

  • String



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/cpf_cnpj_tools.rb', line 103

def format(cpf_or_cnpj)
  if cpf_valid?(cpf_or_cnpj)
    cpf = cpf_or_cnpj.to_s.dup
    cpf.insert(3, ".")
       .insert(7, ".")
       .insert(-3, "-")
  elsif cnpj_valid?(cpf_or_cnpj)
    cnpj = cpf_or_cnpj.to_s.dup
    cnpj.insert(2, ".")
        .insert(6, ".")
        .insert(10, "/")
        .insert(-3, "-")
  else
    raise InvalidCpfCnpjFormatError
  end
end

#formatted?(cpf_or_cnpj) ⇒ Boolean

Return true if the CPF/CNPJ is formatted. Return false if not.

Parameters:

  • cpf_or_cnpj (String, Integer)

Returns:

  • (Boolean)

    bool



79
80
81
82
83
84
# File 'lib/cpf_cnpj_tools.rb', line 79

def formatted?(cpf_or_cnpj)
  number = cpf_or_cnpj.to_s
  return true if (number =~ /\d{3}\.\d{3}\.\d{3}-\d{2}/) || (number =~ %r{\d{2}\.\d{3}\.\d{3}/0001-\d{2}})

  false
end

#generate_array_of_cnpj(number, formatted: true) ⇒ Array[String]

Returns an array of valid random generated CNPJ numbers

Parameters:

  • number (Integer)
  • formatted (bool) (defaults to: true)

Returns:

  • (Array[String])


138
139
140
141
142
143
144
# File 'lib/cpf_cnpj_tools.rb', line 138

def generate_array_of_cnpj(number, formatted: true)
  array = []
  number.times do
    array << generate_cnpj(formatted: formatted)
  end
  array
end

#generate_array_of_cpf(number, formatted: true) ⇒ Array[String]

Returns an array of valid random generated CPF numbers

Parameters:

  • number (Integer)
  • formatted (bool) (defaults to: true)

Returns:

  • (Array[String])


125
126
127
128
129
130
131
# File 'lib/cpf_cnpj_tools.rb', line 125

def generate_array_of_cpf(number, formatted: true)
  array = []
  number.times do
    array << generate_cpf(formatted: formatted)
  end
  array
end

#generate_cnpj(formatted: true) ⇒ Object

Method for generating valid CNPJ numbers

Parameters:

  • formatted (Boolean) (defaults to: true)

Returns:

  • String



33
34
35
36
37
38
39
40
# File 'lib/cpf_cnpj_tools.rb', line 33

def generate_cnpj(formatted: true)
  base = generate_base(cnpj: true)
  base << generate_identifier(base, true, cpf: false)
  base << generate_identifier(base, false, cpf: false)
  return base.join unless formatted

  format(base.join)
end

#generate_cpf(formatted: true) ⇒ Object

Method for generating valid CPF numbers

Parameters:

  • formatted (Boolean) (defaults to: true)

Returns:

  • String



20
21
22
23
24
25
26
27
# File 'lib/cpf_cnpj_tools.rb', line 20

def generate_cpf(formatted: true)
  base = generate_base
  base << generate_identifier(base, true)
  base << generate_identifier(base, false)
  return base.join unless formatted

  format(base.join)
end

#remove_formatting(cpf_or_cnpj) ⇒ Object

Returns an unformatted CPF or CNPJ. If the value is already unformatted, the method returns the value passed as argument.

Parameters:

  • cpf_or_cnpj (String, Integer)

Returns:

  • String



92
93
94
95
96
97
# File 'lib/cpf_cnpj_tools.rb', line 92

def remove_formatting(cpf_or_cnpj)
  unformatted = cpf_or_cnpj.to_s.delete("./-")
  return unformatted unless unformatted.nil?

  cpf_or_cnpj.to_s
end