phony 2.18.3 → 3.0.3

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.
Files changed (79) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +117 -0
  3. data/lib/phony/config.rb +25 -26
  4. data/lib/phony/countries/argentina.rb +352 -0
  5. data/lib/phony/countries/austria.rb +73 -74
  6. data/lib/phony/countries/bangladesh.rb +27 -28
  7. data/lib/phony/countries/belarus.rb +110 -111
  8. data/lib/phony/countries/brazil.rb +93 -93
  9. data/lib/phony/countries/cambodia.rb +10 -14
  10. data/lib/phony/countries/china.rb +22 -18
  11. data/lib/phony/countries/croatia.rb +6 -5
  12. data/lib/phony/countries/georgia.rb +84 -84
  13. data/lib/phony/countries/germany.rb +4067 -4068
  14. data/lib/phony/countries/guinea.rb +8 -8
  15. data/lib/phony/countries/india.rb +31 -34
  16. data/lib/phony/countries/indonesia.rb +57 -46
  17. data/lib/phony/countries/ireland.rb +10 -9
  18. data/lib/phony/countries/italy.rb +79 -60
  19. data/lib/phony/countries/japan.rb +456 -406
  20. data/lib/phony/countries/kyrgyzstan.rb +109 -109
  21. data/lib/phony/countries/latvia.rb +34 -34
  22. data/lib/phony/countries/libya.rb +109 -107
  23. data/lib/phony/countries/malaysia.rb +12 -12
  24. data/lib/phony/countries/moldova.rb +42 -42
  25. data/lib/phony/countries/montenegro.rb +24 -20
  26. data/lib/phony/countries/myanmar.rb +39 -39
  27. data/lib/phony/countries/namibia.rb +35 -29
  28. data/lib/phony/countries/nepal.rb +60 -62
  29. data/lib/phony/countries/netherlands.rb +13 -13
  30. data/lib/phony/countries/pakistan.rb +135 -111
  31. data/lib/phony/countries/paraguay.rb +135 -135
  32. data/lib/phony/countries/russia_kazakhstan_abkhasia_south_ossetia.rb +125 -128
  33. data/lib/phony/countries/saudi_arabia.rb +5 -5
  34. data/lib/phony/countries/serbia.rb +48 -36
  35. data/lib/phony/countries/somalia.rb +18 -18
  36. data/lib/phony/countries/south_korea.rb +16 -13
  37. data/lib/phony/countries/sweden.rb +356 -48
  38. data/lib/phony/countries/taiwan.rb +20 -18
  39. data/lib/phony/countries/tajikistan.rb +70 -70
  40. data/lib/phony/countries/turkmenistan.rb +65 -65
  41. data/lib/phony/countries/ukraine.rb +607 -607
  42. data/lib/phony/countries/united_kingdom.rb +66 -66
  43. data/lib/phony/countries/uruguay.rb +40 -41
  44. data/lib/phony/countries/vietnam.rb +20 -14
  45. data/lib/phony/countries/zimbabwe.rb +31 -31
  46. data/lib/phony/countries.rb +509 -453
  47. data/lib/phony/country.rb +50 -44
  48. data/lib/phony/country_codes.rb +77 -57
  49. data/lib/phony/dsl.rb +25 -28
  50. data/lib/phony/local_splitters/fixed.rb +24 -29
  51. data/lib/phony/local_splitters/regex.rb +30 -31
  52. data/lib/phony/national_code.rb +7 -10
  53. data/lib/phony/national_splitters/default.rb +9 -11
  54. data/lib/phony/national_splitters/dsl.rb +8 -18
  55. data/lib/phony/national_splitters/fixed.rb +12 -15
  56. data/lib/phony/national_splitters/none.rb +8 -14
  57. data/lib/phony/national_splitters/regex.rb +13 -17
  58. data/lib/phony/national_splitters/variable.rb +37 -38
  59. data/lib/phony/trunk_code.rb +23 -23
  60. data/lib/phony/vanity.rb +12 -13
  61. data/lib/phony.rb +84 -83
  62. metadata +14 -45
  63. data/README.textile +0 -112
  64. data/spec/functional/config_spec.rb +0 -44
  65. data/spec/functional/plausibility_spec.rb +0 -608
  66. data/spec/lib/phony/countries_spec.rb +0 -1391
  67. data/spec/lib/phony/country_codes_spec.rb +0 -203
  68. data/spec/lib/phony/country_spec.rb +0 -84
  69. data/spec/lib/phony/dsl_spec.rb +0 -24
  70. data/spec/lib/phony/local_splitters/fixed_spec.rb +0 -56
  71. data/spec/lib/phony/local_splitters/regex_spec.rb +0 -97
  72. data/spec/lib/phony/national_code_spec.rb +0 -91
  73. data/spec/lib/phony/national_splitters/default_spec.rb +0 -34
  74. data/spec/lib/phony/national_splitters/fixed_spec.rb +0 -49
  75. data/spec/lib/phony/national_splitters/none_spec.rb +0 -28
  76. data/spec/lib/phony/national_splitters/regex_spec.rb +0 -23
  77. data/spec/lib/phony/national_splitters/variable_spec.rb +0 -39
  78. data/spec/lib/phony/vanity_spec.rb +0 -30
  79. data/spec/lib/phony_spec.rb +0 -70
data/lib/phony/country.rb CHANGED
@@ -1,22 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Phony
4
-
5
- #
6
- #
7
4
  class Country
8
-
9
5
  attr_accessor :codes
10
6
 
11
7
  @@international_absolute_format = '+%s%s%s%s%s'
12
8
  @@international_relative_format = '00%s%s%s%s%s'
13
9
  @@national_format = '%s%s%s%s'
14
-
10
+
15
11
  @@default_space = ' '
16
12
  @@default_local_space = ' '
17
13
  @@default_parentheses = false
18
14
 
19
- # TODO Doc.
15
+ # TODO: Doc.
20
16
  #
21
17
  def initialize *codes
22
18
  @codes = codes
@@ -26,8 +22,8 @@ module Phony
26
22
  #
27
23
  # Chain two codes together.
28
24
  #
29
- def | other
30
- self.codes = self.codes + other.codes
25
+ def |(other)
26
+ self.codes = codes + other.codes
31
27
  self
32
28
  end
33
29
 
@@ -35,11 +31,11 @@ module Phony
35
31
  #
36
32
  # TODO Rewrite.
37
33
  #
38
- def with cc, options = {}
34
+ def with(cc, options = {})
39
35
  @cc = cc
40
-
36
+
41
37
  @invalid_ndcs = options[:invalid_ndcs]
42
-
38
+
43
39
  @format = options[:format]
44
40
  @space = options[:space]
45
41
  @local_space = options[:local_space]
@@ -52,43 +48,46 @@ module Phony
52
48
  #
53
49
  # @return [Trunk, String (ndc), Array<String> (national pieces)]
54
50
  #
55
- def split national_number
51
+ def split(national_number)
56
52
  _, trunk, ndc, *rest = internal_split national_number
57
53
  [trunk, ndc, *rest]
58
54
  end
55
+
59
56
  #
60
57
  #
61
58
  # @return [Splitters::Local, Trunk, String (ndc), Array<String> (national pieces)]
62
59
  #
63
- def internal_split national_number
60
+ def internal_split(national_number)
64
61
  trunk = nil
65
62
  @codes.each do |national_splitter|
66
63
  new_trunk, ndc, *rest = national_splitter.split national_number
67
64
  trunk ||= new_trunk
65
+ national_number = ndc if new_trunk
68
66
  return [national_splitter.local_splitter, trunk, ndc, *rest] if rest && !rest.empty?
69
67
  end
70
-
68
+
71
69
  # Best effort.
72
70
  [nil, trunk, national_number, []]
73
71
  end
74
-
72
+
75
73
  # Format the number, given the national part of it.
76
74
  #
77
- def format national_number, options = {}
75
+ def format(national_number, options = {})
78
76
  type = options[:format] || @format
79
77
  space = options[:spaces] || @space || @@default_space
80
- local_space = options[:local_spaces] || @local_space || space || @@default_local_space
78
+ local_space = options[:local_spaces] || @local_space || space || @@default_local_space
81
79
  parentheses = options[:parentheses]
82
80
  parentheses = @parentheses || @@default_parentheses if parentheses.nil?
83
81
  use_trunk = options[:trunk]
84
-
82
+
85
83
  trunk, ndc, *local_pieces = split national_number
86
-
84
+
87
85
  local = format_local local_pieces, local_space
88
-
86
+
89
87
  format_cc_ndc trunk, ndc, local, type, space, parentheses, use_trunk
90
88
  end
91
- def format_local local, local_space
89
+
90
+ def format_local(local, local_space)
92
91
  if local.empty?
93
92
  EMPTY_STRING
94
93
  else
@@ -96,11 +95,15 @@ module Phony
96
95
  local.join local_space.to_s
97
96
  end
98
97
  end
99
- def format_cc_ndc trunk, ndc, local, type, space, parentheses, use_trunk
98
+
99
+ def format_cc_ndc(trunk, ndc, local, type, space, parentheses, use_trunk)
100
+ # NOTE: We mark NDCs that are of type "none" with false (nil trips plausible?). This would result in false being printed.
101
+ # Therefore we set NDC to nil when formatting.
102
+ ndc = nil if ndc == false
100
103
  case type
101
104
  when String
102
105
  trunk &&= trunk.format(space, use_trunk)
103
- type % { :trunk => trunk, :cc => @cc, :ndc => ndc, :local => local }
106
+ type % { trunk: trunk, cc: @cc, ndc: ndc, local: local }
104
107
  when nil, :international_absolute, :international, :+
105
108
  if ndc
106
109
  format_with_ndc(@@international_absolute_format, @cc, format_ndc(ndc, parentheses), local, space)
@@ -118,19 +121,23 @@ module Phony
118
121
  if ndc && !ndc.empty?
119
122
  @@national_format % [trunk, format_ndc(ndc, parentheses), space, local]
120
123
  else
121
- @@national_format % [trunk, nil, nil, local]
124
+ @@national_format % [trunk, nil, nil, local]
122
125
  end
123
126
  when :local
124
127
  local
125
128
  end
126
129
  end
127
- def format_ndc ndc, parentheses
130
+
131
+ def format_ndc(ndc, parentheses)
132
+ ndc = nil if ndc == false # TODO
128
133
  parentheses ? "(#{ndc})" : ndc
129
134
  end
130
- def format_with_ndc format, cc, ndc, local, space
135
+
136
+ def format_with_ndc(format, cc, ndc, local, space)
131
137
  format % [cc, space, ndc, space, local]
132
138
  end
133
- def format_without_ndc format, cc, local, space
139
+
140
+ def format_without_ndc(format, cc, local, space)
134
141
  format % [cc, space, local, nil, nil]
135
142
  end
136
143
 
@@ -139,12 +146,13 @@ module Phony
139
146
  @@basic_cleaning_pattern = /\(0|\D/
140
147
  # Clean number of all non-numeric characters and return a copy.
141
148
  #
142
- def clean number
149
+ def clean(number)
143
150
  clean! number && number.dup
144
151
  end
152
+
145
153
  # Clean number of all non-numeric characters and return it.
146
154
  #
147
- def clean! number
155
+ def clean!(number)
148
156
  number.gsub!(@@basic_cleaning_pattern, EMPTY_STRING) || number
149
157
  end
150
158
 
@@ -156,21 +164,22 @@ module Phony
156
164
  #
157
165
  # In some cases it doesn't, like Italy.
158
166
  #
159
- def normalize national_number
167
+ # Note: Options such as CC
168
+ #
169
+ def normalize(national_number, options = {})
170
+ national_number = national_number.dup
160
171
  clean! national_number
161
- normalized = @codes.reduce national_number do |number, code|
162
- result = code.normalize number
172
+ @codes.each_with_object national_number do |code, number|
173
+ result = code.normalize number, options
163
174
  break result if result
164
- number
165
175
  end
166
- normalized
167
176
  end
168
177
 
169
178
  # Tests for plausibility of this national number.
170
179
  #
171
- def plausible? rest, hints = {}
180
+ def plausible?(rest, hints = {})
172
181
  local, _, ndc, *rest = internal_split rest
173
-
182
+
174
183
  # Element based checking.
175
184
  #
176
185
  # Note: ndc == false means the country has none.
@@ -187,28 +196,25 @@ module Phony
187
196
  #
188
197
  ndc_needed = hints[:ndc]
189
198
  return false if ndc_needed && !(ndc_needed === ndc)
190
-
199
+
191
200
  # If there is no local part, we can assume it's not a plausible number.
192
201
  # (Or, not defined correctly in Phony yet)
193
202
  return false unless local
194
203
 
195
204
  # Local code specific checks.
196
205
  #
197
- return local.plausible? rest, hints
206
+ local.plausible? rest, hints
198
207
  end
199
208
 
200
209
  # Is this national number a vanity number?
201
210
  #
202
- def vanity? national_number
211
+ def vanity?(national_number)
203
212
  Vanity.vanity? national_number
204
213
  end
205
- #
206
- #
207
- def vanity_to_number vanity_number
214
+
215
+ def vanity_to_number(vanity_number)
208
216
  _, ndc, *rest = split vanity_number
209
217
  "#{ndc}#{Vanity.replace(rest.join)}"
210
218
  end
211
-
212
219
  end
213
-
214
220
  end
@@ -1,11 +1,11 @@
1
- module Phony
1
+ # frozen_string_literal: true
2
2
 
3
+ module Phony
3
4
  EMPTY_STRING = '' unless defined?(EMPTY_STRING)
4
5
 
5
6
  # Handles determining the correct national code handler.
6
7
  #
7
8
  class CountryCodes
8
-
9
9
  attr_reader :countries
10
10
  attr_accessor :international_absolute_format, :international_relative_format, :national_format
11
11
 
@@ -14,11 +14,11 @@ module Phony
14
14
  def self.instance
15
15
  @instance ||= new
16
16
  end
17
-
17
+
18
18
  # Add the given country to the mapping under the
19
19
  # given country code.
20
20
  #
21
- def add country_code, country
21
+ def add(country_code, country)
22
22
  country_code = country_code.to_s
23
23
  optimized_country_code_access = country_code.size
24
24
 
@@ -29,7 +29,7 @@ module Phony
29
29
 
30
30
  # Get the Country object for the given CC.
31
31
  #
32
- def [] cc
32
+ def [](cc)
33
33
  countries[cc.size][cc]
34
34
  end
35
35
 
@@ -38,12 +38,13 @@ module Phony
38
38
  @@basic_cleaning_pattern = /\A00?|\(0|\D/
39
39
  # Clean number of all non-numeric characters, initial zeros or (0 and return it.
40
40
  #
41
- def clean number
41
+ def clean(number)
42
42
  clean! number && number.dup
43
43
  end
44
+
44
45
  # Clean number of all non-numeric characters, initial zeros or (0 and return a copy.
45
46
  #
46
- def clean! number
47
+ def clean!(number)
47
48
  number.gsub!(@@basic_cleaning_pattern, EMPTY_STRING) || number
48
49
  end
49
50
 
@@ -58,33 +59,34 @@ module Phony
58
59
  # * (0) anywhere.
59
60
  # * Non-digits.
60
61
  #
61
- def normalize number, options = {}
62
- country = if cc = options[:cc]
63
- self[cc]
64
- else
65
- clean! number
66
- country, cc, number = partial_split number
67
- country
68
- end
69
- number = country.normalize number
62
+ def normalize(number, options = {})
63
+ number = number.dup
64
+ country = if (cc = options[:cc])
65
+ self[cc]
66
+ else
67
+ clean! number
68
+ country, cc, number = partial_split number
69
+ country
70
+ end
71
+ number = country.normalize number, cc: cc
70
72
  countrify! number, cc
71
73
  end
72
74
 
73
75
  # Splits this number into cc, ndc and locally split number parts.
74
76
  #
75
- def split number
77
+ def split(number)
76
78
  # Split the number into country, cc, and national part.
77
79
  country, cc, national_number = partial_split number
78
-
80
+
79
81
  # Split the national number into ndc and local part.
80
82
  _, ndc, *local = country.split national_number
81
-
83
+
82
84
  [cc, ndc, *local]
83
85
  end
84
86
 
85
87
  # Format the number.
86
88
  #
87
- def format number, options = {}
89
+ def format(number, options = {})
88
90
  country, _, national_number = partial_split number
89
91
  country.format national_number, options
90
92
  end
@@ -92,15 +94,31 @@ module Phony
92
94
 
93
95
  # Is this number plausible?
94
96
  #
95
- def plausible? number, hints = {}
97
+ def plausible?(number, hints = {})
98
+ # Fail if it contains too many of certain phone specific markers:
99
+ # * more than 1 +
100
+ #
101
+ return false if number.count('+') > 1
102
+
96
103
  normalized = clean number
97
104
 
98
105
  # False if it fails the basic check.
99
106
  #
100
- return false unless (4..16) === normalized.size
107
+ return false unless (4..16).include?(normalized.size) # unless hints[:check_length] == false
101
108
 
102
109
  country, cc, rest = partial_split normalized
103
110
 
111
+ # Was a country calling code given?
112
+ #
113
+ if (ccc = hints[:ccc])
114
+ cc, ndc, *local = split ccc
115
+
116
+ raise ArgumentError.new("The provided ccc option is too long and includes more than a cc ('#{cc}') and ndc ('#{ndc}'). It also includes '#{local.join}'.") unless local.size == 1 && local[0].empty?
117
+
118
+ hints[:cc] = cc
119
+ hints[:ndc] = ndc
120
+ end
121
+
104
122
  # Country code plausible?
105
123
  #
106
124
  cc_needed = hints[:cc]
@@ -109,56 +127,58 @@ module Phony
109
127
  # Country specific tests.
110
128
  #
111
129
  country.plausible? rest, hints
130
+ rescue ArgumentError
131
+ raise
112
132
  rescue StandardError
113
- return false
133
+ false
114
134
  end
115
-
135
+
116
136
  # Is the given number a vanity number?
117
137
  #
118
- def vanity? number
138
+ def vanity?(number)
119
139
  country, _, national = partial_split number
120
140
  country.vanity? national
121
141
  end
142
+
122
143
  # Converts a vanity number into a normalized E164 number.
123
144
  #
124
- def vanity_to_number vanity_number
145
+ def vanity_to_number(vanity_number)
125
146
  country, cc, national = partial_split vanity_number
126
147
  "#{cc}#{country.vanity_to_number(national)}"
127
148
  end
128
149
 
129
150
  private
130
-
131
- # Return a country for the number.
132
- #
133
- def country_for number
134
- country, _ = partial_split number
135
- country
136
- end
137
-
138
- # Split off the country and the cc, and also return the national number part.
139
- #
140
- def partial_split number
141
- cc = ''
142
- 1.upto(3) do |i|
143
- cc << number.slice!(0..0)
144
- country = countries[i][cc]
145
- return [country, cc, number] if country
146
- end
147
- # This line is never reached as CCs are in prefix code.
148
- end
149
-
150
- # Adds the country code to the front
151
- # if it does not already start with it.
152
- #
153
- # Note: This won't be correct in some cases, but it is the best we can do.
154
- #
155
- def countrify number, cc
156
- countrify!(number, cc) || number
157
- end
158
- def countrify! number, cc
159
- number.sub!(/\A/, cc) # @countrify_regex, @cc
151
+
152
+ # Return a country for the number.
153
+ #
154
+ def country_for(number)
155
+ country, = partial_split number
156
+ country
157
+ end
158
+
159
+ # Split off the country and the cc, and also return the national number part.
160
+ #
161
+ def partial_split(number)
162
+ 1.upto(3) do |i|
163
+ cc = number.slice(...i)
164
+ national_number = number.slice(i..)
165
+ country = countries[i][cc]
166
+ return [country, cc, national_number] if country
160
167
  end
168
+ # This line is never reached as CCs are in prefix code.
169
+ end
161
170
 
162
- end
171
+ # Adds the country code to the front
172
+ # if it does not already start with it.
173
+ #
174
+ # Note: This won't be correct in some cases, but it is the best we can do.
175
+ #
176
+ def countrify(number, cc)
177
+ countrify!(number.dup, cc) || number
178
+ end
163
179
 
180
+ def countrify!(number, cc)
181
+ number.sub!(/\A/, cc) # @countrify_regex, @cc
182
+ end
183
+ end
164
184
  end
data/lib/phony/dsl.rb CHANGED
@@ -1,5 +1,6 @@
1
- module Phony
1
+ # frozen_string_literal: true
2
2
 
3
+ module Phony
3
4
  # For country definitions.
4
5
  #
5
6
  # There are two styles: With or without block.
@@ -12,9 +13,9 @@ module Phony
12
13
  #
13
14
  # Phony.define.country ...
14
15
  #
15
- def self.define
16
+ def self.define(&block)
16
17
  dsl = DSL.new
17
- dsl.instance_eval(&Proc.new) if block_given?
18
+ dsl.instance_eval(&block) if block_given?
18
19
  dsl
19
20
  end
20
21
 
@@ -27,7 +28,6 @@ module Phony
27
28
  # Phony.define { ... }
28
29
  #
29
30
  class DSL
30
-
31
31
  # Define a country's rules.
32
32
  #
33
33
  # Use the other DSL methods to define the country's rules.
@@ -40,13 +40,13 @@ module Phony
40
40
  # @example Add a country with country code 27.
41
41
  # country '27', # CC, followed by rules, for example fixed(2) >> ...
42
42
  #
43
- def country country_code, definition, options = {}
43
+ def country(country_code, definition, options = {})
44
44
  return unless Phony.config.load?(country_code)
45
-
45
+
46
46
  definition.with country_code, options
47
47
  Phony::CountryCodes.instance.add country_code, definition
48
48
  end
49
-
49
+
50
50
  # Designates a country code as reserved.
51
51
  # A reserved country will result in an exception when trying to be used.
52
52
  #
@@ -57,10 +57,10 @@ module Phony
57
57
  # @example Designate country code 27 as reserved.
58
58
  # reserved('27')
59
59
  #
60
- def reserved country_code
60
+ def reserved(country_code)
61
61
  # Does nothing, will just fail with an exception.
62
62
  end
63
-
63
+
64
64
  # Define a country to use default rules (and to be done at some point).
65
65
  #
66
66
  # @return Rules for a country.
@@ -71,7 +71,7 @@ module Phony
71
71
  def todo
72
72
  none >> split(10)
73
73
  end
74
-
74
+
75
75
  # This country uses a trunk code.
76
76
  #
77
77
  # @param [String] code The trunk code.
@@ -88,7 +88,7 @@ module Phony
88
88
  # @example Most countries which use a trunk code use 0. E.g. Romania.
89
89
  # country '40', trunk('0') | ...
90
90
  #
91
- def trunk code, options = {}
91
+ def trunk(code, options = {})
92
92
  TrunkCode.new code, options
93
93
  end
94
94
 
@@ -107,11 +107,11 @@ module Phony
107
107
  # @example France. Uses a fixed NDC of size 1.
108
108
  # country '33', fixed(1) >> split(2,2,2,2)
109
109
  #
110
- def fixed length, options = {}
110
+ def fixed(length, options = {})
111
111
  options[:zero] = true if options[:zero].nil?
112
112
  NationalSplitters::Fixed.instance_for length, options
113
113
  end
114
-
114
+
115
115
  # Marks the country as not using an NDC. This rule will always match.
116
116
  #
117
117
  # @return NationalSplitters::None A no-ndc national splitter.
@@ -121,7 +121,7 @@ module Phony
121
121
  def none
122
122
  NationalSplitters::None.instance_for
123
123
  end
124
-
124
+
125
125
  # If you have a number of (possibly) variable length NDCs
126
126
  # that cannot be well expressed via regexp, use this.
127
127
  #
@@ -135,15 +135,15 @@ module Phony
135
135
  # one_of('103', '105') >> split(3,3)
136
136
  #
137
137
  def one_of *ndcs
138
- options = Hash === ndcs.last ? ndcs.pop : {}
138
+ options = ndcs.last.is_a?(Hash) ? ndcs.pop : {}
139
139
 
140
140
  # Ruby 1.8 compatibility mode.
141
141
  #
142
- ndcs = ndcs.first if Array === ndcs.first
142
+ ndcs = ndcs.first if ndcs.first.is_a?(Array)
143
143
 
144
144
  NationalSplitters::Variable.new options[:max_length], ndcs.map(&:freeze)
145
145
  end
146
-
146
+
147
147
  # If you have a number of (possibly) variable length NDCs
148
148
  # that can be well expressed via regexp, use this.
149
149
  #
@@ -157,11 +157,11 @@ module Phony
157
157
  # match(/^(33|55|81)\d+$/) >> split(2,2,2,2) |
158
158
  # match(/^(\d{3})\d+$/) >> split(3,2,2)
159
159
  #
160
- def match regex, options = {}
160
+ def match(regex, options = {})
161
161
  # Check if regexp has a group in it.
162
162
  #
163
- raise "Regexp /#{regex.source}/ needs a group in it that defines which digits belong to the NDC." unless regex.source =~ /\(/
164
-
163
+ raise "Regexp /#{regex.source}/ needs a group in it that defines which digits belong to the NDC." unless /\(/.match?(regex.source)
164
+
165
165
  NationalSplitters::Regex.instance_for regex, options[:on_fail_take], options
166
166
  end
167
167
 
@@ -183,7 +183,7 @@ module Phony
183
183
  # local << local.pop + 10 # Allow for call-through numbers with an arbitrary size.
184
184
  LocalSplitters::Fixed.instance_for local
185
185
  end
186
-
186
+
187
187
  # Matches on the rest of the number and splits according
188
188
  # to the given value for the regexp key.
189
189
  #
@@ -199,18 +199,18 @@ module Phony
199
199
  # /^[489].*$/ => [3,2,3],
200
200
  # :fallback => [2,2,2,2])
201
201
  #
202
- def matched_split options = {}
202
+ def matched_split(options = {})
203
203
  LocalSplitters::Regex.instance_for options
204
204
  end
205
-
205
+
206
206
  # Validators
207
207
  #
208
-
208
+
209
209
  # Which NDCs are explicitly invalid?
210
210
  #
211
211
  # @param [Regexp, String] ndc A regexp or a string of invalid NDCs.
212
212
  #
213
- # @return Validators::NDC An NDC validator
213
+ # @return Validators::NDC An NDC validator
214
214
  #
215
215
  # @example NANP
216
216
  # country '1',
@@ -220,8 +220,5 @@ module Phony
220
220
  def invalid_ndcs *ndc
221
221
  Validators::NDC.new invalid: ndc
222
222
  end
223
-
224
-
225
223
  end
226
-
227
224
  end