phony 2.20.13 → 2.22.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.textile +1 -1
- data/lib/phony/config.rb +25 -26
- data/lib/phony/countries/argentina.rb +311 -314
- data/lib/phony/countries/austria.rb +71 -72
- data/lib/phony/countries/bangladesh.rb +27 -28
- data/lib/phony/countries/belarus.rb +110 -111
- data/lib/phony/countries/brazil.rb +92 -92
- data/lib/phony/countries/cambodia.rb +9 -9
- data/lib/phony/countries/china.rb +22 -21
- data/lib/phony/countries/croatia.rb +6 -5
- data/lib/phony/countries/georgia.rb +84 -84
- data/lib/phony/countries/germany.rb +4067 -4068
- data/lib/phony/countries/guinea.rb +8 -8
- data/lib/phony/countries/india.rb +31 -34
- data/lib/phony/countries/indonesia.rb +57 -46
- data/lib/phony/countries/ireland.rb +4 -5
- data/lib/phony/countries/italy.rb +54 -53
- data/lib/phony/countries/japan.rb +456 -456
- data/lib/phony/countries/kyrgyzstan.rb +109 -109
- data/lib/phony/countries/latvia.rb +34 -34
- data/lib/phony/countries/libya.rb +106 -106
- data/lib/phony/countries/malaysia.rb +12 -12
- data/lib/phony/countries/moldova.rb +42 -42
- data/lib/phony/countries/montenegro.rb +24 -20
- data/lib/phony/countries/myanmar.rb +39 -39
- data/lib/phony/countries/namibia.rb +35 -29
- data/lib/phony/countries/nepal.rb +60 -62
- data/lib/phony/countries/netherlands.rb +13 -13
- data/lib/phony/countries/pakistan.rb +135 -111
- data/lib/phony/countries/paraguay.rb +135 -135
- data/lib/phony/countries/russia_kazakhstan_abkhasia_south_ossetia.rb +125 -128
- data/lib/phony/countries/saudi_arabia.rb +5 -5
- data/lib/phony/countries/serbia.rb +47 -39
- data/lib/phony/countries/somalia.rb +18 -18
- data/lib/phony/countries/south_korea.rb +16 -13
- data/lib/phony/countries/sweden.rb +28 -28
- data/lib/phony/countries/taiwan.rb +20 -19
- data/lib/phony/countries/tajikistan.rb +70 -70
- data/lib/phony/countries/turkmenistan.rb +65 -65
- data/lib/phony/countries/ukraine.rb +607 -607
- data/lib/phony/countries/united_kingdom.rb +66 -66
- data/lib/phony/countries/uruguay.rb +40 -41
- data/lib/phony/countries/vietnam.rb +19 -17
- data/lib/phony/countries/zimbabwe.rb +31 -31
- data/lib/phony/countries.rb +457 -443
- data/lib/phony/country.rb +44 -44
- data/lib/phony/country_codes.rb +67 -60
- data/lib/phony/dsl.rb +23 -26
- data/lib/phony/local_splitters/fixed.rb +23 -30
- data/lib/phony/local_splitters/regex.rb +30 -31
- data/lib/phony/national_code.rb +7 -10
- data/lib/phony/national_splitters/default.rb +9 -11
- data/lib/phony/national_splitters/dsl.rb +8 -18
- data/lib/phony/national_splitters/fixed.rb +12 -15
- data/lib/phony/national_splitters/none.rb +7 -11
- data/lib/phony/national_splitters/regex.rb +13 -17
- data/lib/phony/national_splitters/variable.rb +37 -38
- data/lib/phony/trunk_code.rb +20 -20
- data/lib/phony/vanity.rb +12 -13
- data/lib/phony.rb +84 -84
- metadata +5 -42
- data/spec/functional/config_spec.rb +0 -44
- data/spec/functional/plausibility_spec.rb +0 -689
- data/spec/lib/phony/countries_spec.rb +0 -1459
- data/spec/lib/phony/country_codes_spec.rb +0 -227
- data/spec/lib/phony/country_spec.rb +0 -104
- data/spec/lib/phony/dsl_spec.rb +0 -28
- data/spec/lib/phony/local_splitters/fixed_spec.rb +0 -56
- data/spec/lib/phony/local_splitters/regex_spec.rb +0 -94
- data/spec/lib/phony/national_code_spec.rb +0 -61
- data/spec/lib/phony/national_splitters/default_spec.rb +0 -34
- data/spec/lib/phony/national_splitters/fixed_spec.rb +0 -45
- data/spec/lib/phony/national_splitters/none_spec.rb +0 -28
- data/spec/lib/phony/national_splitters/regex_spec.rb +0 -23
- data/spec/lib/phony/national_splitters/variable_spec.rb +0 -35
- data/spec/lib/phony/trunk_code_spec.rb +0 -85
- data/spec/lib/phony/vanity_spec.rb +0 -30
- data/spec/lib/phony_spec.rb +0 -70
@@ -1,7 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Phony
|
2
|
-
|
3
4
|
module LocalSplitters
|
4
|
-
|
5
5
|
# Local splitter class to split the last part of
|
6
6
|
# a number, i.e. minus cc or ndc.
|
7
7
|
#
|
@@ -11,64 +11,57 @@ module Phony
|
|
11
11
|
# to avoid getting new local splitter instances.
|
12
12
|
#
|
13
13
|
class Fixed
|
14
|
-
|
15
14
|
@mapping = {}
|
16
|
-
|
15
|
+
|
17
16
|
# Get a splitter for the given format.
|
18
17
|
#
|
19
18
|
# Caches the created splitter for the given format.
|
20
19
|
#
|
21
|
-
def self.instance_for
|
20
|
+
def self.instance_for(format = nil)
|
22
21
|
@mapping[format] ||= new(format)
|
23
22
|
end
|
24
|
-
|
23
|
+
|
25
24
|
# Initialize with a local format, like [3, 2, 2] (also the default).
|
26
25
|
#
|
27
26
|
# The format [3, 2, 2] splits a number like '3332222' into ['333', '22', '22'].
|
28
27
|
#
|
29
|
-
def initialize
|
30
|
-
format = format
|
28
|
+
def initialize(format = nil)
|
29
|
+
format = format&.dup || [3, 2, 2]
|
31
30
|
@format, @length = extract_params format
|
32
31
|
@format << @format.pop + 10
|
33
32
|
end
|
34
|
-
|
35
|
-
|
36
|
-
#
|
37
|
-
def extract_params format
|
33
|
+
|
34
|
+
def extract_params(format)
|
38
35
|
if format.last.respond_to? :max
|
39
36
|
last = format.pop
|
40
|
-
length = format.
|
41
|
-
length = (length+last.min..length+last.max)
|
37
|
+
length = format.sum
|
38
|
+
length = (length + last.min..length + last.max)
|
42
39
|
format << last.min
|
43
40
|
else
|
44
|
-
length = format.
|
41
|
+
length = format.sum
|
45
42
|
end
|
46
43
|
[format, length]
|
47
44
|
end
|
48
|
-
|
45
|
+
|
49
46
|
# Split a local number according to an assumed country specific format.
|
50
47
|
#
|
51
48
|
# Examples
|
52
49
|
# * split '3643533' # => ['364', '35', '33'] # (Switzerland)
|
53
50
|
#
|
54
|
-
def split
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
51
|
+
def split(number)
|
52
|
+
cursor = 0
|
53
|
+
@format.each_with_object([]) do |size, result|
|
54
|
+
result << number.slice(cursor...cursor + size)
|
55
|
+
cursor += size
|
56
|
+
return result if cursor >= number.size
|
59
57
|
end
|
60
58
|
end
|
61
|
-
|
62
|
-
|
63
|
-
#
|
64
|
-
def plausible? rest, hints = {}
|
59
|
+
|
60
|
+
def plausible?(rest, hints = {})
|
65
61
|
return true if hints[:check_length] == false
|
66
|
-
|
62
|
+
|
67
63
|
@length === rest.inject(0) { |total, part| total + part.size }
|
68
64
|
end
|
69
|
-
|
70
65
|
end
|
71
|
-
|
72
66
|
end
|
73
|
-
|
74
|
-
end
|
67
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Phony
|
2
|
-
|
3
4
|
module LocalSplitters
|
4
|
-
|
5
5
|
# Local splitter class to split the last part of
|
6
6
|
# a number, i.e. minus cc or ndc.
|
7
7
|
#
|
@@ -11,63 +11,62 @@ module Phony
|
|
11
11
|
# to avoid getting new local splitter instances.
|
12
12
|
#
|
13
13
|
class Regex
|
14
|
-
|
15
14
|
attr_reader :fallback, :mapping
|
16
|
-
|
15
|
+
|
17
16
|
# Get a splitter for the given format.
|
18
17
|
#
|
19
18
|
# Note: Not cached.
|
20
19
|
#
|
21
|
-
def self.instance_for
|
20
|
+
def self.instance_for(mapping)
|
22
21
|
new mapping
|
23
22
|
end
|
24
|
-
|
23
|
+
|
25
24
|
# Initialize with a regex => format mapping.
|
26
25
|
#
|
27
|
-
def initialize
|
26
|
+
def initialize(mapping)
|
28
27
|
@fallback = mapping.delete(:fallback) || [12]
|
29
28
|
@mapping = mapping
|
30
29
|
end
|
31
|
-
|
30
|
+
|
32
31
|
# Split a local number according to an assumed country specific format.
|
33
32
|
#
|
34
33
|
# Examples
|
35
34
|
# * split '3643533' # => ['364', '35', '33'] # (Switzerland)
|
36
35
|
#
|
37
|
-
def split
|
36
|
+
def split(number)
|
38
37
|
mapping.each do |regex, format|
|
39
|
-
next unless number
|
38
|
+
next unless number&.match?(regex)
|
39
|
+
|
40
40
|
return split_with(number, format)
|
41
41
|
end
|
42
42
|
split_with number, fallback
|
43
43
|
end
|
44
|
-
|
45
|
-
def plausible?
|
46
|
-
number = rest.
|
44
|
+
|
45
|
+
def plausible?(rest, _hints = {})
|
46
|
+
number = rest.sum('')
|
47
47
|
mapping.each do |regex, format|
|
48
|
-
next unless number
|
48
|
+
next unless number&.match?(regex)
|
49
|
+
|
49
50
|
return plausible_with? number, format
|
50
51
|
end
|
51
52
|
plausible_with? number, fallback
|
52
53
|
end
|
53
|
-
|
54
|
+
|
54
55
|
private
|
55
|
-
|
56
|
-
def split_with number, format
|
57
|
-
format.inject([]) do |result, size|
|
58
|
-
result << number.slice!(0..size-1)
|
59
|
-
return result if number.empty?
|
60
|
-
result
|
61
|
-
end << number
|
62
|
-
end
|
63
|
-
|
64
|
-
def plausible_with? number, format
|
65
|
-
length = format.inject 0, :+
|
66
|
-
number.length == length
|
67
|
-
end
|
68
56
|
|
57
|
+
def split_with(number, format)
|
58
|
+
cursor = 0
|
59
|
+
format.each_with_object([]) do |size, result|
|
60
|
+
result << number.slice(cursor...cursor + size)
|
61
|
+
cursor += size
|
62
|
+
return result if cursor >= number.size
|
63
|
+
end << number
|
64
|
+
end
|
65
|
+
|
66
|
+
def plausible_with?(number, format)
|
67
|
+
length = format.sum
|
68
|
+
number.length == length
|
69
|
+
end
|
69
70
|
end
|
70
|
-
|
71
71
|
end
|
72
|
-
|
73
|
-
end
|
72
|
+
end
|
data/lib/phony/national_code.rb
CHANGED
@@ -1,14 +1,12 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
+
module Phony
|
3
4
|
# NationalCodes have a special numbers splitter, a national code splitter and a local code splitter.
|
4
5
|
#
|
5
6
|
class NationalCode
|
6
|
-
|
7
7
|
attr_reader :local_splitter
|
8
|
-
|
9
|
-
|
10
|
-
#
|
11
|
-
def initialize national_splitter, local_splitter
|
8
|
+
|
9
|
+
def initialize(national_splitter, local_splitter)
|
12
10
|
@national_splitter = national_splitter
|
13
11
|
@local_splitter = local_splitter
|
14
12
|
end
|
@@ -16,9 +14,10 @@ module Phony
|
|
16
14
|
# Split gets a number without country code and splits it into
|
17
15
|
# its parts.
|
18
16
|
#
|
19
|
-
def split
|
17
|
+
def split(national_number)
|
20
18
|
zero, ndc_or_rest, rest = @national_splitter.split national_number.dup
|
21
19
|
return [zero, ndc_or_rest] unless rest
|
20
|
+
|
22
21
|
[zero, ndc_or_rest, *@local_splitter.split(rest)]
|
23
22
|
end
|
24
23
|
|
@@ -26,10 +25,8 @@ module Phony
|
|
26
25
|
#
|
27
26
|
# Note: Some cases, like Italy, don't remove the relative zero.
|
28
27
|
#
|
29
|
-
def normalize
|
28
|
+
def normalize(national_number, _options = {})
|
30
29
|
national_number.gsub(/\A0+/, EMPTY_STRING)
|
31
30
|
end
|
32
|
-
|
33
31
|
end
|
34
|
-
|
35
32
|
end
|
@@ -1,15 +1,14 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
+
module Phony
|
3
4
|
module NationalSplitters
|
4
|
-
|
5
|
-
# TODO Default = Fixed.new(...)?
|
5
|
+
# TODO: Default = Fixed.new(...)?
|
6
6
|
#
|
7
7
|
class Default < DSL
|
8
|
-
|
9
8
|
def self.instance_for
|
10
|
-
@
|
9
|
+
@instance_for ||= new
|
11
10
|
end
|
12
|
-
|
11
|
+
|
13
12
|
# "Splits" the national part of a phone number into a single piece.
|
14
13
|
#
|
15
14
|
# @param [String] national_number An national part of a number.
|
@@ -19,10 +18,10 @@ module Phony
|
|
19
18
|
# @example Split the national part of a Swiss number.
|
20
19
|
# Phony.split("1234567") # => ["1234567"]
|
21
20
|
#
|
22
|
-
def split
|
21
|
+
def split(national_number)
|
23
22
|
[nil, national_number]
|
24
23
|
end
|
25
|
-
|
24
|
+
|
26
25
|
# By default, the national part of a number is always plausible.
|
27
26
|
#
|
28
27
|
# @param [String] rest An national part of a number (ignored).
|
@@ -34,16 +33,15 @@ module Phony
|
|
34
33
|
# @example Split the national part of a Swiss number.
|
35
34
|
# Phony.plausible?("1234567") # => true
|
36
35
|
#
|
37
|
-
def plausible?
|
36
|
+
def plausible?(_rest, _size, _hints = {})
|
38
37
|
true
|
39
38
|
end
|
40
|
-
|
39
|
+
|
41
40
|
# A valid length (at least 3).
|
42
41
|
#
|
43
42
|
def length
|
44
43
|
3
|
45
44
|
end
|
46
|
-
|
47
45
|
end
|
48
46
|
end
|
49
47
|
end
|
@@ -1,31 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Phony
|
2
|
-
|
3
4
|
module NationalSplitters
|
4
|
-
|
5
5
|
# TODO
|
6
6
|
#
|
7
7
|
class DSL
|
8
|
-
|
9
|
-
#
|
10
|
-
#
|
11
|
-
def >> local_splitter
|
8
|
+
def >>(local_splitter)
|
12
9
|
country_for local_splitter
|
13
10
|
end
|
14
|
-
|
15
|
-
|
16
|
-
#
|
17
|
-
def country_for local_splitter
|
11
|
+
|
12
|
+
def country_for(local_splitter)
|
18
13
|
Phony::Country.new Phony::NationalCode.new(self, local_splitter)
|
19
14
|
end
|
20
15
|
|
21
|
-
# TODO Remove?
|
16
|
+
# TODO: Remove?
|
22
17
|
#
|
23
|
-
def reserved
|
24
|
-
|
25
|
-
end
|
26
|
-
|
18
|
+
def reserved; end
|
27
19
|
end
|
28
|
-
|
29
20
|
end
|
30
|
-
|
31
|
-
end
|
21
|
+
end
|
@@ -1,15 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Phony
|
2
|
-
|
3
4
|
module NationalSplitters
|
4
|
-
|
5
5
|
# TODO
|
6
6
|
#
|
7
7
|
class Fixed < DSL
|
8
|
-
|
9
8
|
attr_writer :special_splitter
|
10
|
-
|
9
|
+
|
11
10
|
@mapping = {}
|
12
|
-
|
11
|
+
|
13
12
|
# Get a splitter for the given format.
|
14
13
|
#
|
15
14
|
# Caches the created splitter for the given format.
|
@@ -17,27 +16,25 @@ module Phony
|
|
17
16
|
def self.instance_for *parameters
|
18
17
|
@mapping[parameters] ||= new(*parameters)
|
19
18
|
end
|
20
|
-
|
21
|
-
def initialize
|
19
|
+
|
20
|
+
def initialize(size, _options = {})
|
22
21
|
@size = size
|
23
22
|
@zero = nil # options[:zero] == false ? nil : '0'
|
24
23
|
end
|
25
|
-
|
24
|
+
|
26
25
|
# Takes a national number and splits it into ndc and rest.
|
27
26
|
#
|
28
|
-
def split
|
27
|
+
def split(national_number)
|
29
28
|
return [@zero, national_number] unless @size
|
30
|
-
|
29
|
+
|
30
|
+
[@zero, national_number.slice(0...@size), national_number.slice(@size..)]
|
31
31
|
end
|
32
|
-
|
32
|
+
|
33
33
|
# A valid length.
|
34
34
|
#
|
35
35
|
def length
|
36
36
|
@size
|
37
37
|
end
|
38
|
-
|
39
38
|
end
|
40
|
-
|
41
39
|
end
|
42
|
-
|
43
|
-
end
|
40
|
+
end
|
@@ -1,18 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Phony
|
2
|
-
|
3
4
|
module NationalSplitters
|
4
|
-
|
5
5
|
# This is a national splitter for countries
|
6
6
|
# which have no NDC / Area Code.
|
7
7
|
#
|
8
8
|
class None < Default
|
9
|
-
|
10
9
|
# Get a splitter. Caches.
|
11
10
|
#
|
12
11
|
def self.instance_for(*)
|
13
|
-
@
|
12
|
+
@instance_for ||= new
|
14
13
|
end
|
15
|
-
|
14
|
+
|
16
15
|
# On false:
|
17
16
|
#
|
18
17
|
# This is a hack to make phony's
|
@@ -33,18 +32,15 @@ module Phony
|
|
33
32
|
#
|
34
33
|
# Note: Decided it stays in. When formatting, it's turned into nil.
|
35
34
|
#
|
36
|
-
def split
|
35
|
+
def split(national_number)
|
37
36
|
[nil, false, national_number]
|
38
37
|
end
|
39
|
-
|
38
|
+
|
40
39
|
# A valid length.
|
41
40
|
#
|
42
41
|
def length
|
43
42
|
0
|
44
43
|
end
|
45
|
-
|
46
44
|
end
|
47
|
-
|
48
45
|
end
|
49
|
-
|
50
|
-
end
|
46
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Phony
|
2
|
-
|
3
4
|
module NationalSplitters
|
4
|
-
|
5
5
|
# National splitter class to split the ndc-local part of a number.
|
6
6
|
#
|
7
7
|
# Countries can create new instances according to their needs.
|
@@ -10,46 +10,42 @@ module Phony
|
|
10
10
|
# to avoid getting new local splitter instances.
|
11
11
|
#
|
12
12
|
class Regex < Fixed
|
13
|
-
|
14
13
|
attr_reader :on_fail_take, :regex
|
15
|
-
|
14
|
+
|
16
15
|
# Get a splitter for the given format.
|
17
16
|
#
|
18
17
|
# Note: Not cached.
|
19
18
|
#
|
20
|
-
def self.instance_for
|
19
|
+
def self.instance_for(regex, on_fail_take = nil, options = {})
|
21
20
|
new regex, on_fail_take, options
|
22
21
|
end
|
23
22
|
|
24
|
-
def initialize
|
25
|
-
super
|
23
|
+
def initialize(regex, on_fail_take = nil, options = {})
|
24
|
+
super(on_fail_take, options)
|
26
25
|
|
27
26
|
@regex = regex
|
28
27
|
end
|
29
|
-
|
28
|
+
|
30
29
|
# Split a local number according to an assumed country specific format.
|
31
30
|
#
|
32
31
|
# Examples
|
33
32
|
# * split '3643533' # => ['364', '35', '33'] # (Switzerland)
|
34
33
|
#
|
35
|
-
def split
|
34
|
+
def split(national_number)
|
36
35
|
# Improve matching.
|
37
36
|
#
|
38
|
-
return [@zero, national_number.slice!(0
|
39
|
-
|
37
|
+
return [@zero, national_number.slice!(0..::Regexp.last_match(1).size - 1), national_number] if national_number =~ regex
|
38
|
+
|
40
39
|
# Not found.
|
41
40
|
#
|
42
|
-
super
|
41
|
+
super(national_number)
|
43
42
|
end
|
44
|
-
|
43
|
+
|
45
44
|
# A valid length.
|
46
45
|
#
|
47
46
|
def length
|
48
47
|
# raise "#{self.class.name} has no length that can be automatically extracted."
|
49
48
|
end
|
50
|
-
|
51
49
|
end
|
52
|
-
|
53
50
|
end
|
54
|
-
|
55
|
-
end
|
51
|
+
end
|
@@ -1,68 +1,67 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Phony
|
2
|
-
|
3
4
|
module NationalSplitters
|
4
|
-
|
5
5
|
class Variable < Fixed
|
6
|
-
|
7
|
-
|
8
|
-
#
|
9
|
-
def initialize fallback, ndcs, options = {}
|
10
|
-
super fallback, options
|
6
|
+
def initialize(fallback, ndcs, options = {})
|
7
|
+
super(fallback, options)
|
11
8
|
@ndcs = optimize ndcs
|
12
9
|
end
|
13
|
-
|
10
|
+
|
14
11
|
# Takes a national number and splits it into ndc and rest.
|
15
12
|
#
|
16
|
-
def split
|
13
|
+
def split(national_number)
|
14
|
+
national_number = national_number.dup
|
17
15
|
fallback_number = national_number.dup
|
18
|
-
|
16
|
+
|
19
17
|
# Extract a starting point.
|
20
18
|
#
|
21
19
|
# This if can possibly be removed.
|
22
20
|
#
|
23
|
-
presumed_code = if @mapped_ndc_min_length
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
21
|
+
presumed_code = if @mapped_ndc_min_length.positive?
|
22
|
+
presumed_code = national_number.slice!(0..@mapped_ndc_min_length - 1)
|
23
|
+
else
|
24
|
+
''
|
25
|
+
end
|
26
|
+
|
29
27
|
# Try for all possible mapped.
|
30
28
|
#
|
31
29
|
@mapped_ndc_min_length.upto @mapped_ndc_max_length do |i|
|
32
30
|
ndcs_of_size_i = @ndcs[i]
|
33
|
-
|
31
|
+
unless ndcs_of_size_i && !ndcs_of_size_i.include?(presumed_code)
|
32
|
+
return [@zero, presumed_code,
|
33
|
+
national_number]
|
34
|
+
end
|
35
|
+
|
34
36
|
presumed_code << national_number.slice!(0..0)
|
35
37
|
end
|
36
|
-
|
38
|
+
|
37
39
|
# Not found.
|
38
40
|
#
|
39
|
-
super
|
41
|
+
super(fallback_number)
|
40
42
|
end
|
41
|
-
|
43
|
+
|
42
44
|
# A valid length.
|
43
45
|
#
|
44
46
|
def length
|
45
47
|
(@mapped_ndc_min_length..@mapped_ndc_max_length)
|
46
48
|
end
|
47
|
-
|
49
|
+
|
48
50
|
private
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
end
|
58
|
-
keys = ndcs.keys
|
59
|
-
@mapped_ndc_min_length = keys.min # || 1
|
60
|
-
@mapped_ndc_max_length = keys.max
|
61
|
-
ndcs
|
51
|
+
|
52
|
+
# Optimizes and restructures the given ndcs array.
|
53
|
+
#
|
54
|
+
def optimize(ndc_ary)
|
55
|
+
ndcs = {}
|
56
|
+
ndc_ary.each do |ndc|
|
57
|
+
ndcs[ndc.length] ||= []
|
58
|
+
ndcs[ndc.length] << ndc
|
62
59
|
end
|
63
|
-
|
60
|
+
keys = ndcs.keys
|
61
|
+
@mapped_ndc_min_length = keys.min # || 1
|
62
|
+
@mapped_ndc_max_length = keys.max
|
63
|
+
ndcs
|
64
|
+
end
|
64
65
|
end
|
65
|
-
|
66
66
|
end
|
67
|
-
|
68
|
-
end
|
67
|
+
end
|
data/lib/phony/trunk_code.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
+
module Phony
|
3
4
|
class TrunkCode
|
4
|
-
|
5
5
|
# Parameters:
|
6
6
|
# * code: The trunk code, e.g. 0.
|
7
7
|
#
|
@@ -10,17 +10,17 @@ module Phony
|
|
10
10
|
# * split: [true, false (default)] Remove the trunk code when splitting (only use if number scheme is defined unambiguously).
|
11
11
|
# * format: [true (default), false] Add the trunk code when formatting (passing `false` will not add it).
|
12
12
|
#
|
13
|
-
def initialize
|
13
|
+
def initialize(code, options = {})
|
14
14
|
@code = code
|
15
|
-
@trunk_code_replacement = /\A#{code.gsub(
|
15
|
+
@trunk_code_replacement = /\A#{code.gsub(/%s/, '')}/
|
16
16
|
@normalize = options[:normalize] || options[:normalize].nil?
|
17
17
|
@split = options[:split]
|
18
18
|
@format = options[:format] || options[:format].nil?
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
# Prepends itself to the other codes.
|
22
22
|
#
|
23
|
-
def |
|
23
|
+
def |(other)
|
24
24
|
other.codes.unshift self
|
25
25
|
other
|
26
26
|
end
|
@@ -28,30 +28,30 @@ module Phony
|
|
28
28
|
# Split gets a number without country code and splits it into
|
29
29
|
# its parts.
|
30
30
|
#
|
31
|
-
def split
|
32
|
-
|
33
|
-
|
31
|
+
def split(national_number)
|
32
|
+
return [self, national_number] unless @split
|
33
|
+
|
34
|
+
without_trunk = national_number.gsub @trunk_code_replacement, EMPTY_STRING
|
35
|
+
[self, without_trunk]
|
34
36
|
end
|
35
|
-
|
37
|
+
|
36
38
|
# Normalize normalizes the given national number.
|
37
39
|
#
|
38
|
-
def normalize
|
39
|
-
national_number
|
40
|
-
|
40
|
+
def normalize(national_number, options = {})
|
41
|
+
return national_number unless @normalize && options[:cc]
|
42
|
+
|
43
|
+
national_number.gsub @trunk_code_replacement, EMPTY_STRING
|
41
44
|
end
|
42
|
-
|
45
|
+
|
43
46
|
# Format the trunk code using the spaces given.
|
44
47
|
#
|
45
|
-
def format
|
46
|
-
|
48
|
+
def format(space, force = nil)
|
49
|
+
return unless force || @format
|
47
50
|
if @code.size > 1
|
48
51
|
(@code % space).gsub(/\D/, ' ')
|
49
52
|
else
|
50
53
|
@code
|
51
54
|
end
|
52
|
-
end
|
53
55
|
end
|
54
|
-
|
55
56
|
end
|
56
|
-
|
57
|
-
end
|
57
|
+
end
|