Class: Faker::CNPJ
- Inherits:
-
Object
- Object
- Faker::CNPJ
- Defined in:
- lib/faker/cnpj.rb
Class Method Summary collapse
-
.numeric ⇒ Object
(also: numbers, number)
Generates a valid CNPJ number with numbers only.
-
.pretty ⇒ Object
(also: cnpj, formatted)
Generates a valid CNPJ number, including separators.
Class Method Details
.numeric ⇒ Object Also known as: numbers, number
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/faker/cnpj.rb', line 8 def numeric cnpj_root = Array.new(12) { rand(10) } # calculate first digit factor = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2] sum = (0..11).inject(0) do |sum, i| sum + cnpj_root[i] * factor[i] end first_validator = sum % 11 cnpj_root << first_validator = first_validator < 2 ? 0 : 11 - first_validator # calculate second digit factor.unshift 6 sum = (0..12).inject(0) do |sum, i| sum + cnpj_root[i] * factor[i] end second_validator = sum % 11 (cnpj_root << second_validator = second_validator < 2 ? 0 : 11 - second_validator).join end |
.pretty ⇒ Object Also known as: cnpj, formatted
38 39 40 41 |
# File 'lib/faker/cnpj.rb', line 38 def pretty cnpj = numeric.to_s.rjust(14, "0") "%s.%s.%s/%s-%s" % [cnpj[0,2], cnpj[2,3], cnpj[5,3], cnpj[8,4], cnpj[12,2]] end |