Class: CNPJ

Inherits:
Object
  • Object
show all
Defined in:
lib/cnpj.rb,
lib/cnpj/formatter.rb,
lib/cpf_cnpj/version.rb,
lib/cnpj/verifier_digit.rb

Defined Under Namespace

Classes: Formatter, VerifierDigit

Constant Summary collapse

REGEX =
%r[\A[\dA-Z]{2}\.[\dA-Z]{3}.[\dA-Z]{3}/[\dA-Z]{4}-\d{2}\Z].freeze
VALIDATION_SIZE_REGEX =
/^[A-Z\d]{12}\d{2}$/.freeze
NUMBER_SIZE =
12
DENYLIST =
%w[
  00000000000000
  11111111111111
  22222222222222
  33333333333333
  44444444444444
  55555555555555
  66666666666666
  77777777777777
  88888888888888
  99999999999999
].freeze
VERSION =
CpfCnpj::VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number, strict = false) ⇒ CNPJ

Returns a new instance of CNPJ.



46
47
48
49
# File 'lib/cnpj.rb', line 46

def initialize(number, strict = false)
  @number = number.to_s.upcase
  @strict = strict
end

Instance Attribute Details

#numberObject

Returns the value of attribute number.



8
9
10
# File 'lib/cnpj.rb', line 8

def number
  @number
end

#strictObject (readonly)

Returns the value of attribute strict.



8
9
10
# File 'lib/cnpj.rb', line 8

def strict
  @strict
end

Class Method Details

.format(number) ⇒ Object



27
28
29
# File 'lib/cnpj.rb', line 27

def self.format(number)
  new(number).formatted
end

.generate(formatted = false) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/cnpj.rb', line 35

def self.generate(formatted = false)
  numbers = Array("0".."9") + ("A".."Z").to_a
  digits = Array.new(NUMBER_SIZE) { numbers.sample }
  numeric_digits = digits.map {|d| d.ord - 48 }
  numeric_digits << VerifierDigit.generate(numeric_digits)
  numeric_digits << VerifierDigit.generate(numeric_digits)

  cnpj = new((digits + numeric_digits[-2, 2]).join)
  formatted ? cnpj.formatted : cnpj.stripped
end

.valid?(number, strict: false) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/cnpj.rb', line 31

def self.valid?(number, strict: false)
  new(number, strict).valid?
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



82
83
84
# File 'lib/cnpj.rb', line 82

def ==(other)
  super || (other.instance_of?(self.class) && other.stripped == stripped)
end

#formattedObject



62
63
64
# File 'lib/cnpj.rb', line 62

def formatted
  @formatted ||= Formatter.format(number)
end

#number_without_verifierObject



87
88
89
# File 'lib/cnpj.rb', line 87

def number_without_verifier
  stripped_chars[0...NUMBER_SIZE].join
end

#strippedObject



58
59
60
# File 'lib/cnpj.rb', line 58

def stripped
  @stripped ||= Formatter.strip(number, strict)
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  if strict && !(number.match?(REGEX) || number.match?(VALIDATION_SIZE_REGEX))
    return false
  end

  return false unless stripped.match?(VALIDATION_SIZE_REGEX)

  return false if DENYLIST.include?(stripped)

  digits = numbers[0...NUMBER_SIZE]
  digits << VerifierDigit.generate(digits)
  digits << VerifierDigit.generate(digits)

  digits[-2, 2] == numbers[-2, 2]
end