Sha256: aaad650ed17af97f2e0ea9b2343cb9808df39c885d341b5acfccad80d9e2dec6

Contents?: true

Size: 714 Bytes

Versions: 3

Compression:

Stored size: 714 Bytes

Contents

module Faker
  module Russian
    module Okpo
      def okpo(options = {})
        options.assert_valid_keys(:sequence_number)

        sequence = find_sequence(options[:sequence_number])
        okpo_digits = okpo_region_numbers(sequence)
        okpo_digits + okpo_calc(okpo_digits)
      end

    private

      def okpo_region_numbers(sequence)
        if [true, false][sequence.rand(2)]
          sprintf '%07d', sequence.rand(10_000_000)
        else
          sprintf '%09d', sequence.rand(1_000_000_000)
        end
      end

      def okpo_calc(okpo_digits)
        (okpo_digits.split(//).map(&:to_i).each_with_index.inject(0) { |s, (p, i)| s + p * (i + 1) } % 11 % 10).to_s
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
faker-russian-0.0.6 lib/faker/russian/okpo.rb
faker-russian-0.0.5 lib/faker/russian/okpo.rb
faker-russian-0.0.4 lib/faker/russian/okpo.rb