Module: Checksum

Defined in:
lib/checksum.rb

Class Method Summary collapse

Class Method Details

.addr_or_name?(n) ⇒ Boolean



43
44
45
# File 'lib/checksum.rb', line 43

def addr_or_name?( n )
  n =~ /\W{4,}/ && n.length > 10
end

.inn?(n) ⇒ Boolean



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
# File 'lib/checksum.rb', line 16

def inn?( n )
  text = n.to_s.freeze
  return false unless [ 10, 12 ].include?(text.length)
  w = [
    [ 7, 2, 4, 10, 3, 5, 9, 4, 6, 8 ],
    [ 3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8 ],
    [ 2, 4, 10, 3, 5, 9, 4, 6, 8 ]
  ].freeze
  if text.length == 12
    i = -1
    n2 = text.split('')[0..-3].reduce(0){ |sum, x| sum + x.to_i * w[0][i += 1] } % 11
    n2 = 0 if n2 == 10

    i = -1
    n1 = text.split('')[0..-2].reduce(0){ |sum, x| sum + x.to_i * w[1][i += 1] } % 11
    n1 = 0 if n1 == 10

    return ( text[-2].to_i == n2 && text[-1].to_i == n1 )
  else
    i = -1
    n1 = text.split('')[0..-2].reduce(0){ |sum, x| sum + x.to_i * w[2][i += 1] } % 11
    n1 = 0 if n1 == 10

    return ( text[-1].to_i == n1 )
  end
end

.ogrn?(n) ⇒ Boolean



47
48
49
50
51
52
53
54
# File 'lib/checksum.rb', line 47

def ogrn?( n )
  text = n.to_s.freeze
  return false unless [13,15].include?( text.length )
  z = 1
  ctrl = text.split('')[0..-2].reduce(0){ |sum,x| z *= -1; sum + z * x.to_i }
  ctrl = ctrl.to_s[-1].to_i if ctrl.abs > 9
  text[-1].to_i == ctrl % (text.length - 2)
end

.okpo?(n) ⇒ Boolean



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/checksum.rb', line 3

def okpo?( n )
  text = n.to_s
  return false unless [ 8, 10 ].include?( text.length )
  mul = 1
  control = text.split('')[1..-2].reduce( text[0].to_i ){ |sum, x| sum + x.to_i * (mul += 1) } % 11
  if control > 9
    mul = 3
    control = text.split('')[1..-2].reduce( text[0].to_i ){ |sum, x| sum + x.to_i * (mul += 1) } % 11
  end
  control = 0 if control > 9
  control == text[-1].to_i
end