/
national_code.rb
35 lines (28 loc) · 924 Bytes
/
national_code.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
module Phony
# NationalCodes have a special numbers splitter, a national code splitter and a local code splitter.
#
class NationalCode
attr_reader :local_splitter
#
#
def initialize national_splitter, local_splitter
@national_splitter = national_splitter
@local_splitter = local_splitter
end
# Split gets a number without country code and splits it into
# its parts.
#
def split national_number
zero, ndc_or_rest, rest = @national_splitter.split national_number.dup
return [zero, ndc_or_rest] unless rest
[zero, ndc_or_rest, *@local_splitter.split(rest)]
end
# Split gets a number without country code and removes a relative zero.
#
# Note: Some cases, like Italy, don't remove the relative zero.
#
def normalize national_number, options = {}
national_number.gsub(/\A0+/, EMPTY_STRING)
end
end
end