Class: CpfCnpjTools::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/cpf_cnpj_tools.rb,
sig/cpf_cnpj_tools/generator.rbs

Overview

Class responsible for generating and validating CPF and CNPJ numbers

Constant Summary collapse

BLACKLIST_CPF =
%w[
  00000000000 11111111111 22222222222 33333333333 44444444444
  55555555555 66666666666 77777777777 88888888888 99999999999
].freeze
REGEX_CPF_FORMATTED =
/^\d{3}\.\d{3}\.\d{3}-\d{2}$/.freeze
REGEX_CNPJ_FORMATTED =
%r{^[A-Za-z0-9]{2}\.[A-Za-z0-9]{3}\.[A-Za-z0-9]{3}/[A-Za-z0-9]{4}-\d{2}$}.freeze
REGEX_CNPJ_UNFORMATTED =
/^[A-Z0-9]{12}\d{2}$/.freeze
CPF1DIGIT =

Returns:

  • (Array[Integer])
[10, 9, 8, 7, 6, 5, 4, 3, 2].freeze
CPF2DIGIT =

Returns:

  • (Array[Integer])
[11, 10, 9, 8, 7, 6, 5, 4, 3, 2].freeze
CNPJ1DIGIT =

Returns:

  • (Array[Integer])
[5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2].freeze
CNPJ2DIGIT =

Returns:

  • (Array[Integer])
[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 (Handles Alphanumeric).

Parameters:

  • cnpj (Integer, String)

Returns:

  • (Boolean)

    bool



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/cpf_cnpj_tools.rb', line 87

def cnpj_valid?(cnpj)
  unformatted = remove_formatting(cnpj.to_s).upcase

  return false unless unformatted.match?(REGEX_CNPJ_UNFORMATTED)

  cnpj_array = unformatted.chars
  first_digit = cnpj_array[-2].to_i
  second_digit = cnpj_array[-1].to_i

  base_cnpj_values = cnpj_array[0..11].map { |char| cnpj_char_to_value(char) }

  calculated_first_digit = generate_identifier(base_cnpj_values, true, cpf: false)
  calculated_second_digit = generate_identifier(base_cnpj_values + [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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cpf_cnpj_tools.rb', line 65

def cpf_valid?(cpf)
  unformatted = remove_formatting(cpf.to_s)
  return false if BLACKLIST_CPF.include?(unformatted)

  cpf_array = remove_formatting(cpf.to_s).split("").map!(&:to_i)
  return false if cpf_array.length != 11

  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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/cpf_cnpj_tools.rb', line 134

def format(cpf_or_cnpj)
  if cpf_valid?(cpf_or_cnpj)
    cpf = remove_formatting(cpf_or_cnpj).dup
    cpf.insert(3, ".")
       .insert(7, ".")
       .insert(-3, "-")
  elsif cnpj_valid?(cpf_or_cnpj)
    cnpj = remove_formatting(cpf_or_cnpj).upcase.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



111
112
113
114
115
116
117
# File 'lib/cpf_cnpj_tools.rb', line 111

def formatted?(cpf_or_cnpj)
  number = cpf_or_cnpj.to_s
  return true if number =~ REGEX_CPF_FORMATTED
  return true if number =~ REGEX_CNPJ_FORMATTED

  false
end

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

Returns an array of valid random generated CNPJ numbers

Parameters:

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

Returns:

  • (Array[String])


169
170
171
172
173
174
175
# File 'lib/cpf_cnpj_tools.rb', line 169

def generate_array_of_cnpj(number, formatted: true, alphanumeric: false)
  array = []
  number.times do
    array << generate_cnpj(formatted: formatted, alphanumeric: alphanumeric)
  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])


156
157
158
159
160
161
162
# File 'lib/cpf_cnpj_tools.rb', line 156

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

#generate_cnpj(formatted: true, alphanumeric: false) ⇒ Object

Method for generating valid CNPJ numbers (Numeric) Standard numeric CNPJs remain 100% valid under the new rules.

Parameters:

  • formatted (Boolean) (defaults to: true)

Returns:

  • String



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cpf_cnpj_tools.rb', line 49

def generate_cnpj(formatted: true, alphanumeric: false)
  base_chars, base_values = generate_base(cnpj: true, alphanumeric: alphanumeric)

  first_digit = generate_identifier(base_values, true, cpf: false)
  second_digit = generate_identifier(base_values + [first_digit], false, cpf: false)

  result = (base_chars + [first_digit, second_digit]).join
  return result unless formatted

  format(result)
end

#generate_cpf(formatted: true) ⇒ Object

Method for generating valid CPF numbers

Parameters:

  • formatted (Boolean) (defaults to: true)

Returns:

  • String



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cpf_cnpj_tools.rb', line 29

def generate_cpf(formatted: true)
  # Destructure the two arrays returned by the new generate_base
  base_chars, base_values = generate_base(cnpj: false)

  # Calculate the verification digits using base_values
  first_digit = generate_identifier(base_values, true)
  second_digit = generate_identifier(base_values + [first_digit], false)

  # Combine the characters with the calculated digits
  result = (base_chars + [first_digit, second_digit]).join
  return result unless formatted

  format(result)
end

#remove_formatting(cpf_or_cnpj) ⇒ Object

Returns an unformatted CPF or CNPJ.

Parameters:

  • cpf_or_cnpj (String, Integer)

Returns:

  • String



123
124
125
126
127
128
# File 'lib/cpf_cnpj_tools.rb', line 123

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

  cpf_or_cnpj.to_s
end