Class: RegexForms::ValidateCNPJ

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cnpj) ⇒ ValidateCNPJ

Returns a new instance of ValidateCNPJ.



39
40
41
# File 'lib/regex_forms.rb', line 39

def initialize(cnpj)
  @cnpj = cnpj
end

Instance Attribute Details

#cnpjObject (readonly)

Returns the value of attribute cnpj.



37
38
39
# File 'lib/regex_forms.rb', line 37

def cnpj
  @cnpj
end

Instance Method Details

#validateObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/regex_forms.rb', line 43

def validate
  return invalid_response("CNPJ is nil, empty, or all zeros") if invalid_cnpj?

  regex = /^(?<t_1>\d{2})\.(?<t_2>\d{3})\.(?<t_3>\d{3})\/0001-(?<t_4>\d{2})$/

  if regex.match(cnpj)
    valid_response("Validated CNPJ")
  else
    invalid_response("Invalid CNPJ: #{cnpj}")
  end
end