Sha256: 315dd3d2434fa3dcf38872c349a321266f6540e67d91548b2f3727cc15f39eea
Contents?: true
Size: 1.42 KB
Versions: 1
Compression:
Stored size: 1.42 KB
Contents
# frozen_string_literal: true class CPF require "cpf_cnpj" require "cpf/formatter" require "cpf/verifier_digit" attr_reader :number attr_reader :strict REGEX = /\A\d{3}\.\d{3}\.\d{3}-\d{2}\Z/ VALIDATION_SIZE_REGEX = /^[0-9]{11}$/ NUMBER_SIZE = 9 BLACKLIST = %w[ 00000000000 11111111111 22222222222 33333333333 44444444444 55555555555 66666666666 77777777777 88888888888 99999999999 12345678909 01234567890 ].freeze def self.valid?(number, strict: false) new(number, strict).valid? end def self.generate(formatted = false) number = CpfCnpj::Generator.generate(NUMBER_SIZE, VerifierDigit) cpf = new(number) formatted ? cpf.formatted : cpf.stripped end def initialize(number, strict = false) @number = number.to_s @strict = strict end def number=(number) @stripped = nil @formatted = nil @numbers = nil @number = number end def stripped @stripped ||= Formatter.strip(number, strict) end def formatted @formatted ||= Formatter.format(number) end def valid? return unless stripped =~ VALIDATION_SIZE_REGEX return if BLACKLIST.include?(stripped) digits = numbers[0...9] digits << VerifierDigit.generate(digits) digits << VerifierDigit.generate(digits) digits[-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.4.1 | lib/cpf.rb |