Class: BrDocuments::CnpjCpf::Cnpj

Inherits:
Object
  • Object
show all
Includes:
Document
Defined in:
lib/br_documents/cnpj_cpf/cnpj.rb

Instance Method Summary collapse

Methods included from BrDocuments::Commons::Mod11

#generate_digital_check, #reduce_weights

Constructor Details

#initialize(number) ⇒ Cnpj

Returns a new instance of Cnpj.



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

def initialize(number)
  @number = number
end

Instance Method Details

#formattedObject



20
21
22
# File 'lib/br_documents/cnpj_cpf/cnpj.rb', line 20

def formatted
  @number.sub(/(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})/, "\\1.\\2.\\3/\\4-\\5")
end

#valid?Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
# File 'lib/br_documents/cnpj_cpf/cnpj.rb', line 12

def valid?
  regex = /^(\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2})$|^(\d{14})$/
  weight13 = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
  weight14 = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]

  valid_number?(@number, regex, weight13, weight14)
end