-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathvalidateSAID.rb
63 lines (52 loc) · 1.42 KB
/
validateSAID.rb
1
2
3
4
5
6
7
8
9
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
# This class validates the Saudi National ID / Iqama Number.
# It accepts the NID / Iqama in integer or string.
# It also handles input stripping & Arabic digits conversion (١٢٣)
# You can use it like:
# NationalIdValidator.new('١٢٣٤٥٦٧٨٩٠').call
# NationalIdValidator.new('1234567890').call
# NationalIdValidator.new(1234567890).call
#
#
# Responses:
# -1 => Invalid Saudi NID / Iqama Number
# 1 => valid Saudi NID
# 2 => valid Iqama Number
#
class NationalIdValidator
EN_DIGITS = "0123456789".freeze
AR_DIGITS = "٠١٢٣٤٥٦٧٨٩".freeze
EMPTY_STRING = "".freeze
attr_reader :id_str, :id_num, :type
def initialize(nid)
@id_str = nid_formatter(nid)
@type = id_str[0].to_i
@id_num = id_str.to_i
end
def call
return -1 unless id_str == id_num.to_s
return -1 unless type == 1 || type == 2
return -1 unless id_str.length == 10
return -1 unless calculate_sum % 10 == 0
type
end
private
def nid_formatter(nid)
nid
.to_s
.tr(AR_DIGITS, EN_DIGITS)
.strip
end
def calculate_sum
sum = 0
id_num_arr = id_str.split(EMPTY_STRING).map(&:to_i)
id_num_arr.each_with_index do |digit, i|
if i % 2 == 0
check_number = (digit * 2).to_s.rjust(2, '0')
sum += check_number[0].to_i + check_number[1].to_i
else
sum += digit;
end
end
sum
end
end