Sha256: 18a7e4dbb206fb6b649b7d1b97e70d326ab4d66f0741b550ccc6b1ed05ed0e67

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

module CnsBrazil
  class Generator
    def call
      result = generate

      result = generate while result.to_s.length != 15

      result
    end

    def block
      first_number.to_s + block_number + block_number
    end

    private

    # first number should be 1,2,7,8 or 9
    def first_number
      range_value = []
      range_value << rand(1..2)
      range_value << rand(7..9)

      range_value.sample
    end

    def block_number
      "0#{rand(11_111..99_999).to_s.chop}"
    end

    def generate
      cns               = block
      rest              = sum_result(cns) % 11
      verificator_digit = rest.zero? ? 0 : 11 - rest

      if verificator_digit == 10
        sum_result = sum_result(cns)
        sum_result += 2

        resto             = sum_result % 11
        verificator_digit = 11 - resto
        cns += "001#{verificator_digit}"
      else
        cns += "000#{verificator_digit}"
      end

      cns
    end

    def sum_result(cns)
      cns_to_array = cns.chars.map(&:to_i)

      cns_to_array.each_with_index.reduce(0) do |sum, (element, index)|
        sum += element * (15 - index)
        sum
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cns_brazil-1.0.4 lib/cns_brazil/generator.rb