Sha256: 52ac045e542d46814088b007d7726489384f78f114ab5377683f2cf82d980f2c

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

class CNPJ
  autoload :CLI, "cnpj/cli"
  autoload :Formatter, "cnpj/formatter"
  autoload :Generator, "cnpj/generator"
  autoload :VerifierDigit, "cnpj/verifier_digit"

  attr_reader :number

  REGEX = /\A\d{2}\.\d{3}.\d{3}\/\d{4}-\d{2}\Z/

  BLACKLIST = [
    "00000000000000",
    "11111111111111",
    "22222222222222",
    "33333333333333",
    "44444444444444",
    "55555555555555",
    "66666666666666",
    "77777777777777",
    "88888888888888",
    "99999999999999"
  ]

  def self.valid?(number)
    new(number).valid?
  end

  def self.generate
    Generator.generate
  end

  def initialize(number)
    @number = number.to_s
  end

  def number=(number)
    @stripped = nil
    @formatted = nil
    @numbers = nil
    @number = number
  end

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

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

  def valid?
    return unless stripped.size == 14
    return if BLACKLIST.include?(stripped)

    _numbers = numbers[0...12]
    _numbers << VerifierDigit.generate(_numbers)
    _numbers << VerifierDigit.generate(_numbers)

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

  private
  def numbers
    @numbers ||= stripped.each_char.to_a.map(&:to_i)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cpf_cnpj-0.1.0 lib/cnpj.rb