Sha256: 5a00b97c01d07ee2953b4a6d3fc644e86b2a0c08a4cd0a38c2abf88985fcaf02

Contents?: true

Size: 934 Bytes

Versions: 5

Compression:

Stored size: 934 Bytes

Contents

require_relative './commons/mod11'

module BrDocuments
  class Suframa
    include BrDocuments::Commons::Mod11

    def initialize(number)
      @number = number
    end

    def formatted
      valid? ? format_number : ''
    end

    def valid?
      valid_pattern? &&
        !sequence_of_equal_numbers? &&
        valid_check_digit?
    end

    private

    def valid_pattern?
      /^(\d{2}\.\d{4}\.\d{3})$|^(\d{9})$/.match(@number).present?
    end

    def sequence_of_equal_numbers?
      number_without_mask.chars.uniq.length == 1
    end

    def number_without_mask
      @number_without_mask ||= @number.delete('.')
    end

    def valid_check_digit?
      weights = [9, 8, 7, 6, 5, 4, 3, 2]

      check_digit = generate_check_digit(number_without_mask, weights)
      number_without_mask[8] == check_digit.to_s
    end

    def format_number
      @number.sub(/(\d{2})(\d{4})(\d{3})/, '\\1.\\2.\\3')
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
br_documents-0.2.5 lib/br_documents/suframa.rb
br_documents-0.2.4 lib/br_documents/suframa.rb
br_documents-0.2.3 lib/br_documents/suframa.rb
br_documents-0.2.2 lib/br_documents/suframa.rb
br_documents-0.2.1 lib/br_documents/suframa.rb