lib/faker/russian/inn.rb in faker-russian-0.0.1 vs lib/faker/russian/inn.rb in faker-russian-0.0.2
- old
+ new
@@ -1,42 +1,50 @@
module Faker
module Russian
module Inn
def inn(options = {})
- region_number = find_region_number(options[:region_number])
- kind = find_kind(options[:kind])
- sequence_digits = find_sequence_digits(kind, options[:sequence_number])
+ if (options.keys - [:sequence_number, :region_number, :kind]).any?
+ raise 'wrong options. just :kind, :region_number or :sequence_number'
+ end
+ sequence = find_sequence(options[:sequence_number])
+ region_number = find_region_number(options[:region_number], sequence)
+ 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_region_number(number = nil)
+ def find_sequence(number)
+ sequence = number || Random.rand(1_000_000_000)
+ Random.new(sequence)
+ end
+
+ def find_region_number(number = nil, sequence)
+ region_numbers = Faker::Russian::SharedConstants::REGION_NUMBERS
+
if number.present?
- raise('there is no region with that number!') unless REGION_NUMBERS.include?(number)
- number
+ raise('there is no region with that number!') unless region_numbers.include?(number.to_s)
+ number.to_s
else
- REGION_NUMBERS.sample
+ region_numbers[sequence.rand(region_numbers.size)]
end
end
- def find_kind(kind)
+ def find_kind(kind, sequence)
case kind
- when :individual then :individual
- when :legal then :legal
- when nil then [:individual, :legal].sample
+ 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_sequence_digits(kind, sequence_number)
- sequence = sequence_number || Random.new_seed
- Random.srand(sequence)
-
- (kind == :legal) ? Random.rand(1_000_000..9_999_999).to_s : Random.rand(10_000_000..99_999_999).to_s
+ 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(P10, digits)
@@ -46,11 +54,9 @@
end
def calc(p, inn)
(p.each_with_index.inject(0){ |s, p| s + p[0] * inn[p[1]].to_i } % 11 % 10).to_s
end
-
- REGION_NUMBERS = %w{01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 83 86 87 89} # TODO move to specific place
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