Sha256: 748b552413f2a6b58ef47cbdc766dc069e17a4eef7f59b0b2e8e0767e5487839

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

module Faker
  module Russian
    module Inn
      def inn(options = {})
        options.assert_valid_keys(:sequence_number, :region_number, :kind)

        sequence = find_sequence(options[:sequence_number])
        region_number = find_region_number(options[:region_number], sequence, :region_numbers)
        kind = find_kind(options[:kind], sequence)
        sequence_digits = find_digits(kind, sequence)

        inn_without_check_digit = region_number + sequence_digits
        inn_without_check_digit + check_digit(inn_without_check_digit)
      end

    private

      def find_kind(kind, sequence)
        case kind
        when :individual, :legal then kind
        when nil then [:individual, :legal][sequence.rand(2)]
        else raise 'there is no any kind other than :individual or :legal'
        end
      end

      def find_digits(kind, sequence)
        (kind == :legal) ? sequence.rand(1_000_000..9_999_999).to_s : sequence.rand(10_000_000..99_999_999).to_s
      end

      def check_digit(digits)
        if digits.length == 9
          calc_inn(P10, digits)
        else
          calc_inn(P11, digits) + calc_inn(P12, digits + calc_inn(P11, digits))
        end
      end

      def calc_inn(p, inn)
        (p.each_with_index.inject(0){ |s, p| s + p[0] * inn[p[1]].to_i } % 11 % 10).to_s
      end

      P10 = [2, 4, 10, 3, 5, 9, 4, 6, 8]
      P11 = [7, 2, 4, 10, 3, 5, 9, 4, 6, 8]
      P12 = [3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8]
      private_constant :P10, :P11, :P12
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
faker-russian-0.0.6 lib/faker/russian/inn.rb
faker-russian-0.0.5 lib/faker/russian/inn.rb