Sha256: c68ffcf099b440de9735b4803f550c74be2f06b4b429f1f0dc20a5cf30801ec3

Contents?: true

Size: 1.18 KB

Versions: 6

Compression:

Stored size: 1.18 KB

Contents

require_relative 'base'
require_relative '../commons/mod11'

module BrDocuments
  module IE
    class RN < Base
      include Commons::Mod11

      protected

      def format_ie(number)
        if number.gsub(/(\.)|(\-)/, '').length == 9
          number.sub(/(\d{2})(\d{3})(\d{3})(\d{1})/, '\\1.\\2.\\3-\\4')
        else
          number.sub(/(\d{2})(\d{1})(\d{3})(\d{3})(\d{1})/, '\\1.\\2.\\3.\\4-\\5')
        end
      end

      def valid_format?
        valid_old_format or valid_new_format
      end

      def valid_check_digit?
        @number.gsub!(/[\.\/-]/, '')

        weight = []
        @number.length.downto(2).each { |w| weight << w }

        @number[-1] == generate_check_digit(@number, weight).to_s
      end

      private

      def valid_old_format
        regex = /^(\d{2}\.\d{3}\.\d{3}\-\d{1})$|^(\d{9})$/
        regex.match(@number).present?
      end

      def valid_new_format
        regex = /^(\d{2}\.\d{1}\.\d{3}\.\d{3}\-\d{1})$|^(\d{10})$/
        regex.match(@number).present?
      end

      def generate_check_digit(values, weights)
        sum = reduce_weights(values, weights)
        mod = sum * 10 % 11
        mod == 10 ? 0 : mod
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
br_documents-0.2.5 lib/br_documents/ie/rn.rb
br_documents-0.2.4 lib/br_documents/ie/rn.rb
br_documents-0.2.3 lib/br_documents/ie/rn.rb
br_documents-0.2.2 lib/br_documents/ie/rn.rb
br_documents-0.2.1 lib/br_documents/ie/rn.rb
br_documents-0.2.0 lib/br_documents/ie/rn.rb