phonelib 0.10.19 → 0.10.20

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 (94) hide show
  1. checksums.yaml +4 -4
  2. data/lib/phonelib/version.rb +1 -1
  3. metadata +3 -185
  4. data/spec/benchmark_helper.rb +0 -32
  5. data/spec/coverage/assets/0.10.0/application.css +0 -799
  6. data/spec/coverage/assets/0.10.0/application.js +0 -1707
  7. data/spec/coverage/assets/0.10.0/colorbox/border.png +0 -0
  8. data/spec/coverage/assets/0.10.0/colorbox/controls.png +0 -0
  9. data/spec/coverage/assets/0.10.0/colorbox/loading.gif +0 -0
  10. data/spec/coverage/assets/0.10.0/colorbox/loading_background.png +0 -0
  11. data/spec/coverage/assets/0.10.0/favicon_green.png +0 -0
  12. data/spec/coverage/assets/0.10.0/favicon_red.png +0 -0
  13. data/spec/coverage/assets/0.10.0/favicon_yellow.png +0 -0
  14. data/spec/coverage/assets/0.10.0/loading.gif +0 -0
  15. data/spec/coverage/assets/0.10.0/magnify.png +0 -0
  16. data/spec/coverage/assets/0.10.0/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
  17. data/spec/coverage/assets/0.10.0/smoothness/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
  18. data/spec/coverage/assets/0.10.0/smoothness/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
  19. data/spec/coverage/assets/0.10.0/smoothness/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  20. data/spec/coverage/assets/0.10.0/smoothness/images/ui-bg_glass_75_dadada_1x400.png +0 -0
  21. data/spec/coverage/assets/0.10.0/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
  22. data/spec/coverage/assets/0.10.0/smoothness/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
  23. data/spec/coverage/assets/0.10.0/smoothness/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
  24. data/spec/coverage/assets/0.10.0/smoothness/images/ui-icons_222222_256x240.png +0 -0
  25. data/spec/coverage/assets/0.10.0/smoothness/images/ui-icons_2e83ff_256x240.png +0 -0
  26. data/spec/coverage/assets/0.10.0/smoothness/images/ui-icons_454545_256x240.png +0 -0
  27. data/spec/coverage/assets/0.10.0/smoothness/images/ui-icons_888888_256x240.png +0 -0
  28. data/spec/coverage/assets/0.10.0/smoothness/images/ui-icons_cd0a0a_256x240.png +0 -0
  29. data/spec/coverage/index.html +0 -72
  30. data/spec/dummy/README.rdoc +0 -261
  31. data/spec/dummy/Rakefile +0 -8
  32. data/spec/dummy/app/assets/config/manifest.js +0 -0
  33. data/spec/dummy/app/assets/javascripts/application.js +0 -13
  34. data/spec/dummy/app/assets/javascripts/phones.js +0 -2
  35. data/spec/dummy/app/assets/stylesheets/application.css +0 -13
  36. data/spec/dummy/app/assets/stylesheets/phones.css +0 -4
  37. data/spec/dummy/app/assets/stylesheets/scaffold.css +0 -56
  38. data/spec/dummy/app/controllers/application_controller.rb +0 -3
  39. data/spec/dummy/app/controllers/phones_controller.rb +0 -98
  40. data/spec/dummy/app/helpers/application_helper.rb +0 -2
  41. data/spec/dummy/app/helpers/phones_helper.rb +0 -2
  42. data/spec/dummy/app/models/phone.rb +0 -22
  43. data/spec/dummy/app/views/layouts/application.html.erb +0 -14
  44. data/spec/dummy/app/views/phones/_form.html.erb +0 -25
  45. data/spec/dummy/app/views/phones/edit.html.erb +0 -6
  46. data/spec/dummy/app/views/phones/index.html.erb +0 -25
  47. data/spec/dummy/app/views/phones/new.html.erb +0 -5
  48. data/spec/dummy/app/views/phones/show.html.erb +0 -14
  49. data/spec/dummy/config/application.rb +0 -62
  50. data/spec/dummy/config/boot.rb +0 -10
  51. data/spec/dummy/config/database.yml +0 -25
  52. data/spec/dummy/config/environment.rb +0 -5
  53. data/spec/dummy/config/environments/development.rb +0 -39
  54. data/spec/dummy/config/environments/production.rb +0 -67
  55. data/spec/dummy/config/environments/test.rb +0 -37
  56. data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
  57. data/spec/dummy/config/initializers/inflections.rb +0 -15
  58. data/spec/dummy/config/initializers/mime_types.rb +0 -5
  59. data/spec/dummy/config/initializers/phonelib.rb +0 -2
  60. data/spec/dummy/config/initializers/secret_token.rb +0 -7
  61. data/spec/dummy/config/initializers/session_store.rb +0 -8
  62. data/spec/dummy/config/initializers/wrap_parameters.rb +0 -14
  63. data/spec/dummy/config/locales/en.yml +0 -5
  64. data/spec/dummy/config/routes.rb +0 -60
  65. data/spec/dummy/config/storage.yml +0 -4
  66. data/spec/dummy/config.ru +0 -4
  67. data/spec/dummy/db/development.sqlite3 +0 -0
  68. data/spec/dummy/db/migrate/20130121173847_create_phones.rb +0 -9
  69. data/spec/dummy/db/migrate/20130122075331_add_possible_number_to_phone.rb +0 -5
  70. data/spec/dummy/db/migrate/20140318165956_add_more_fields_to_phone.rb +0 -6
  71. data/spec/dummy/db/migrate/20161009182201_add_strict_field_to_phone.rb +0 -5
  72. data/spec/dummy/db/migrate/20161230181632_add_country_to_phone.rb +0 -5
  73. data/spec/dummy/db/migrate/20180522050128_add_country_number_to_phone.rb +0 -5
  74. data/spec/dummy/db/migrate/20180731040557_add_type_mobile_number_to_phone.rb +0 -5
  75. data/spec/dummy/db/migrate/20181021065112_add_country_specifier_proc_number_to_phone.rb +0 -5
  76. data/spec/dummy/db/schema.rb +0 -30
  77. data/spec/dummy/db/test.sqlite3 +0 -0
  78. data/spec/dummy/lib/override_phone_data.dat +0 -0
  79. data/spec/dummy/log/development.log +0 -219
  80. data/spec/dummy/log/test.log +0 -5375
  81. data/spec/dummy/public/404.html +0 -26
  82. data/spec/dummy/public/422.html +0 -26
  83. data/spec/dummy/public/500.html +0 -25
  84. data/spec/dummy/public/favicon.ico +0 -0
  85. data/spec/dummy/script/rails +0 -6
  86. data/spec/dummy/spec/controllers/phones_controller_spec.rb +0 -88
  87. data/spec/dummy/spec/fixtures/phones.yml +0 -65
  88. data/spec/dummy/spec/spec_helper.rb +0 -35
  89. data/spec/dummy/spec/unit/phone_spec.rb +0 -122
  90. data/spec/dummy/tmp/development_secret.txt +0 -1
  91. data/spec/phonelib_bench.rb +0 -20
  92. data/spec/phonelib_ips_bench.rb +0 -27
  93. data/spec/phonelib_memory_bench.rb +0 -29
  94. data/spec/phonelib_spec.rb +0 -1534
@@ -1,1534 +0,0 @@
1
- require 'phonelib'
2
-
3
- require 'simplecov'
4
- SimpleCov.start
5
-
6
- describe Phonelib do
7
- before(:all) do
8
- Phonelib.override_phone_data = "spec/dummy/lib/override_phone_data.dat"
9
- end
10
-
11
- before(:each) do
12
- Phonelib.default_country = nil
13
- Phonelib.extension_separator = ';'
14
- Phonelib.extension_separate_symbols = '#;'
15
- Phonelib.parse_special = false
16
- Phonelib.strict_check = false
17
- Phonelib.vanity_conversion = false
18
- end
19
-
20
- it 'must be a Module' do
21
- expect(Phonelib).to be_a_kind_of(Module)
22
- end
23
-
24
- it 'responds to eager_load!' do
25
- expect{Phonelib.eager_load!}.to_not raise_error
26
- end
27
-
28
- context '.parse' do
29
- before(:each) { @phone = Phonelib.parse '9721234567' }
30
-
31
- it 'returns a Phone object' do
32
- expect(@phone).to be_a(Phonelib::Phone)
33
- end
34
-
35
- it 'must be possible but not valid phone number' do
36
- expect(@phone.valid?).to be false
37
- expect(@phone.possible?).to be true
38
- end
39
-
40
- context 'with international formatting' do
41
- before(:each) { @phone = Phonelib.parse('+1 (972) 123-4567', 'US') }
42
- it 'returns exact original' do
43
- expect(@phone.original).to eq('+1 (972) 123-4567')
44
- end
45
- end
46
- end
47
-
48
- context '.valid?' do
49
- context 'with malformed phone number' do
50
- it 'should be false' do
51
- expect(Phonelib.valid?('sdffsd')).to be false
52
- end
53
- end
54
-
55
- context 'with valid phone number' do
56
- it 'should be true' do
57
- expect(Phonelib.valid?('972542234567')).to be true
58
- end
59
- end
60
-
61
- context 'with invalid phone number' do
62
- it 'should be false' do
63
- expect(Phonelib.valid?('97254123')).to be false
64
- end
65
- end
66
- end
67
-
68
- context '.invalid?' do
69
- context 'with malformed phone number' do
70
- it 'should be true' do
71
- expect(Phonelib.invalid?('sdffsd')).to be true
72
- end
73
- end
74
-
75
- context 'with valid phone number' do
76
- it 'should be false' do
77
- expect(Phonelib.invalid?('972542234567')).to be false
78
- end
79
- end
80
-
81
- context 'with invalid phone number' do
82
- it 'should be true' do
83
- expect(Phonelib.invalid?('97254123')).to be true
84
- end
85
- end
86
- end
87
-
88
- context '.possible?' do
89
- context 'with valid phone number' do
90
- it 'should be true' do
91
- expect(Phonelib.possible?('972542234567')).to be true
92
- end
93
- end
94
-
95
- context 'with invalid phone number' do
96
- it 'should be false' do
97
- expect(Phonelib.possible?('97254')).to be false
98
- end
99
- end
100
- end
101
-
102
- context '.impossible?' do
103
- context 'with valid phone number' do
104
- it 'should be false' do
105
- expect(Phonelib.impossible?('972542234567')).to be false
106
- end
107
- end
108
-
109
- context 'with invalid phone number' do
110
- it 'should be true' do
111
- expect(Phonelib.impossible?('97254')).to be true
112
- end
113
- end
114
- end
115
-
116
- context 'valid_for_country?' do
117
- context 'with correct data' do
118
- ['IL', 'il', :il].each do |country|
119
- context "with #{country} as country" do
120
- it 'should be true' do
121
- expect(Phonelib.valid_for_country?('972542234567', country)).to\
122
- be true
123
- end
124
-
125
- context 'and national number' do
126
- it 'should be true' do
127
- expect(Phonelib.valid_for_country?('0542234567', country)).to\
128
- be true
129
- end
130
- end
131
-
132
- context 'and without prefix' do
133
- it 'should be true' do
134
- expect(Phonelib.valid_for_country?('542234567', country)).to\
135
- be true
136
- end
137
- end
138
- end
139
- end
140
-
141
- context 'with entry in overidden data file' do
142
- ['UG', 'ug', :ug].each do |country|
143
- context "with #{country} as country" do
144
- context 'with correct data' do
145
- it 'should be true' do
146
- # the number provided would be invalid if it weren't for the override file
147
- expect(Phonelib.valid_for_country?('812345678', country)).to\
148
- be true
149
- end
150
- end
151
- end
152
- end
153
- end
154
- end
155
-
156
- ['US', 'us', :us].each do |country|
157
- context "with #{country} as country" do
158
- context 'with incorrect data' do
159
- it 'should be false' do
160
- expect(Phonelib.valid_for_country?('972542234567', country)).to\
161
- be false
162
- end
163
-
164
- context 'and without prefix' do
165
- it 'should be false' do
166
- expect(Phonelib.valid_for_country?('542234567', country)).to\
167
- be false
168
- end
169
- end
170
- end
171
- end
172
- end
173
- end
174
-
175
- context '.invalid_for_country?' do
176
- context 'with correct data' do
177
- ['IL', 'il', :il].each do |country|
178
- context "with #{country} as country" do
179
- it 'should be false' do
180
- expect(Phonelib.invalid_for_country?('972542234567', country)).to\
181
- be false
182
- end
183
- end
184
- end
185
- end
186
-
187
- context 'with incorrect data' do
188
- ['US', 'us', :us].each do |country|
189
- context "with #{country} as country" do
190
- it 'should be true' do
191
- expect(Phonelib.invalid_for_country?('972542234567', country)).to\
192
- be true
193
- end
194
- end
195
- end
196
- end
197
- end
198
-
199
- context '#international' do
200
- it 'returns right formatting when possible' do
201
- phone = Phonelib.parse('+15555555555')
202
- expect(phone.international).to eq('+1 555-555-5555')
203
- end
204
-
205
- it 'returns right formatting' do
206
- phone = Phonelib.parse('972542234567')
207
- expect(phone.international).to eq('+972 54-223-4567')
208
- end
209
-
210
- it 'returns unformatted when false passed' do
211
- phone = Phonelib.parse('972542234567')
212
- expect(phone.international(false)).to eq('+972542234567')
213
- end
214
-
215
- it 'returns nil when number is nil' do
216
- expect(Phonelib.parse(nil).international).to be_nil
217
- end
218
-
219
- it 'returns nil when number is empty' do
220
- expect(Phonelib.parse('').international).to be_nil
221
- end
222
- end
223
-
224
- context '#national' do
225
- it 'returns right formatting' do
226
- phone = Phonelib.parse('972542234567')
227
- expect(phone.national).to eq('054-223-4567')
228
- end
229
-
230
- it 'returns unformatted when false passed' do
231
- phone = Phonelib.parse('972542234567')
232
- expect(phone.national(false)).to eq('0542234567')
233
- end
234
-
235
- it 'returns sanitized national when number invalid but possible' do
236
- phone = Phonelib.parse('9721234567')
237
- expect(phone.valid?).to be false
238
- expect(phone.possible?).to be true
239
- expect(phone.national).to eq('1234567')
240
- end
241
-
242
- it 'return without leading digit for CN number' do
243
- phone = Phonelib.parse('18621374266', 'CN')
244
- expect(phone.national).to eq('186 2137 4266')
245
- end
246
- end
247
-
248
- context '#e164' do
249
- it 'returns right e164 phone' do
250
- phone = Phonelib.parse('972542234567')
251
- expect(phone.e164).to eq('+972542234567')
252
- end
253
-
254
- it 'returns sanitized when number invalid but possible' do
255
- phone = Phonelib.parse('9721234567')
256
- expect(phone.e164).to eq('+9721234567')
257
- end
258
-
259
- it 'returns nil when number is blank' do
260
- expect(Phonelib.parse(nil).e164).to be_nil
261
- end
262
-
263
- it 'returns nil when number is empty' do
264
- expect(Phonelib.parse('').e164).to be_nil
265
- end
266
- end
267
-
268
- context 'types' do
269
- before(:all) { @phone = Phonelib.parse('972542234567') }
270
- it 'returns :mobile type' do
271
- expect(@phone.type).to eq(:mobile)
272
- end
273
-
274
- it 'returns Mobile human type' do
275
- expect(@phone.human_type).to eq('Mobile')
276
- end
277
-
278
- it 'returns [:mobile] as all types and possible_types' do
279
- expect(@phone.types).to eq([:mobile])
280
- possible_types = [:voip, :mobile]
281
- expect(@phone.possible_types).to eq(possible_types)
282
- end
283
-
284
- it 'returns [Mobile] as all human types' do
285
- expect(@phone.human_types).to eq(%w(Mobile))
286
- end
287
- end
288
-
289
- context 'country' do
290
- it 'returns IL as country' do
291
- phone = Phonelib.parse('972542234567')
292
- expect(phone.country).to eq('IL')
293
- end
294
-
295
- it 'returns RU as country' do
296
- phone = Phonelib.parse('78005500500')
297
- expect(phone.country).to eq('RU')
298
- end
299
- end
300
-
301
- context 'country_code' do
302
- it 'returns 1 as country code' do
303
- phone = Phonelib.parse('17731231234')
304
- expect(phone.country_code).to eq("1")
305
- end
306
-
307
- it 'returns 7 as country code' do
308
- phone = Phonelib.parse('78005500500')
309
- expect(phone.country_code).to eq("7")
310
- end
311
-
312
- it 'returns nil as country code if no country' do
313
- phone = Phonelib.parse('7731231234')
314
- expect(phone.country_code).to be_nil
315
- end
316
-
317
- it 'returns correct country code for prefix 1 countries' do
318
- expect(Phonelib.parse("+1 809-538-0020").country_code).to eq('1809')
319
- expect(Phonelib.parse("+1 232-671-1234").country_code).to eq('1')
320
- expect(Phonelib.parse("+1 787-671-1234").country_code).to eq('1787')
321
- end
322
- end
323
-
324
- context 'default_country' do
325
- it 'should be invalid with no default country set' do
326
- phone = Phonelib.parse('542234567')
327
- expect(phone.valid?).to be false
328
- end
329
-
330
- it 'should be valid with default country set' do
331
- Phonelib.default_country = :IL
332
- phone = Phonelib.parse('542234567')
333
- expect(phone.valid?).to be true
334
- end
335
-
336
- it 'should be valid with wrong default country set' do
337
- Phonelib.default_country = :UA
338
- phone = Phonelib.parse('972542234567')
339
- expect(phone.valid?).to be true
340
- end
341
-
342
- it 'should not fail when no phone passed and default country set' do
343
- Phonelib.default_country = :UA
344
- phone = Phonelib.parse(nil)
345
- expect(phone.invalid?).to be true
346
- end
347
-
348
- it 'should be valid when number invalid for default country' do
349
- Phonelib.default_country = :CN
350
- phone = Phonelib.parse('+41 44 668 18 00')
351
- expect(phone.valid?).to be true
352
- Phonelib.default_country = nil
353
- end
354
-
355
- it 'should be valid when number valid and several default countries' do
356
- Phonelib.default_country = [:us, :pr, :as, :gu, :mp, :vi]
357
- phone = Phonelib.parse('7876711234')
358
- expect(phone.valid?).to be true
359
- Phonelib.default_country = nil
360
- end
361
-
362
- it 'should be valid when number valid pr and several default countries without pr' do
363
- Phonelib.default_country = [:us, :as, :gu, :mp, :vi]
364
- phone = Phonelib.parse('7876711234')
365
- expect(phone.possible?).to be true
366
- expect(phone.valid?).to be false
367
- expect(phone.countries).to eq(['US'])
368
- Phonelib.default_country = nil
369
- end
370
-
371
- it 'should be valid when number not valid and several default countries' do
372
- Phonelib.default_country = [:us, :as, :gu, :mp, :vi]
373
- phone = Phonelib.parse('123123')
374
- expect(phone.possible?).to be false
375
- expect(phone.valid?).to be false
376
- Phonelib.default_country = nil
377
- end
378
- end
379
-
380
- context 'extended data' do
381
- it 'should have geo_name' do
382
- phone = Phonelib.parse('12015551234')
383
- expect(phone.geo_name).to eq('New Jersey')
384
- end
385
-
386
- it 'should have timezone' do
387
- phone = Phonelib.parse('12015551234')
388
- expect(phone.timezones).to eq(['America/New_York'])
389
- expect(phone.timezone).to eq('America/New_York')
390
- end
391
-
392
- it 'should have multiple timezones' do
393
- phone = Phonelib.parse('+3911111111')
394
- expect(phone.timezones).to eq(["Europe/Rome", "Europe/Vatican"])
395
- expect(phone.timezone).to eq('Europe/Rome')
396
- end
397
-
398
- it 'should have carrier' do
399
- phone = Phonelib.parse('+4915123456789')
400
- expect(phone.carrier).to eq('T-Mobile')
401
- end
402
-
403
- it 'should be present when invalid but possible' do
404
- phone = Phonelib.parse('9721234567', :il)
405
- expect(phone.valid?).to be false
406
- expect(phone.possible?).to be true
407
- expect(phone.timezone).to eq('Asia/Jerusalem')
408
- end
409
-
410
- it 'should not have ext data when impossible' do
411
- phone = Phonelib.parse('71')
412
- expect(phone.valid?).to be false
413
- expect(phone.possible?).to be false
414
- expect(phone.geo_name).to be_nil
415
- expect(phone.timezone).to be_nil
416
- expect(phone.carrier).to be_nil
417
- end
418
-
419
- it 'should be nil when not exist geo name' do
420
- phone = Phonelib.parse('0145-61-1234', 'JP')
421
- expect(phone.valid?).to be true
422
- expect(phone.geo_name).to be_nil
423
- end
424
- end
425
-
426
- context 'issue #16' do
427
- it 'should parse as LT country' do
428
- phone = Phonelib.parse('00370 611 11 111')
429
- expect(phone.country).to eq('LT')
430
- end
431
-
432
- it 'shows correct international' do
433
- phone = Phonelib.parse('370 611 11 111')
434
- expect(phone.international).to eq('+370 611 11111')
435
- end
436
- end
437
-
438
- context 'issue #18' do
439
- it 'not raise exceptions' do
440
- expect(Phonelib.parse('54932', 'DE').national).to be_kind_of(String)
441
- expect(Phonelib.parse('33251304029', 'LU').national).to be_kind_of(String)
442
- expect(Phonelib.parse('61130374', 'AU').national).to be_kind_of(String)
443
- end
444
- end
445
-
446
- context 'issue #19' do
447
- it 'should parse as valid numbers with international prefix' do
448
- phone1 = Phonelib.parse('0049032123456789', 'GB')
449
- phone2 = Phonelib.parse('81049032123456789', 'RU')
450
- phone3 = Phonelib.parse('81049032123456789', 'GB')
451
- phone4 = Phonelib.parse('00962796820700','DE')
452
- expect(phone1.valid?).to be true
453
- expect(phone1.country).to eq('DE')
454
- expect(phone2.valid?).to be true
455
- expect(phone2.country).to eq('DE')
456
- expect(phone3.valid?).to be false
457
- expect(phone4.country).to eq('JO')
458
- end
459
- end
460
-
461
- context 'issue #20' do
462
- it 'should parse with special characters' do
463
- expect(Phonelib.parse('(202) 867-5309', 'US').valid?).to be true
464
- expect(Phonelib.parse('2028675309', 'US').valid?).to be true
465
- end
466
- end
467
-
468
- context 'issue #21' do
469
- it 'should parse without country code' do
470
- phone1 = Phonelib.parse '+81 90 1234 5678', 'JP'
471
- expect(phone1.valid_for_country?('JP')).to be true
472
- phone2 = Phonelib.parse '90 1234 5678', 'JP'
473
- expect(phone2.valid_for_country?('JP')).to be true
474
- end
475
- end
476
-
477
- context 'issue #27' do
478
- it 'should not raise error while parsing invalid numbers' do
479
- test_cases = [
480
- ['0000', 'PH'], ['0000', 'IN'],
481
- ['01114552586', 'US'], ['01148209679', 'CA'],
482
- ['000000000000000', 'CN'], ['0050016323', 'KR']
483
- ]
484
- test_cases.each do |test_case|
485
- number, country = test_case
486
- phone = Phonelib.parse number, country
487
- expect(phone.valid_for_country?(country)).to be false
488
- end
489
- end
490
- end
491
-
492
- context 'issue #33' do
493
- it 'should be valid for mexico numbers' do
494
- number = Phonelib.parse('+525545258448', 'mx')
495
- expect(number.valid?).to be true
496
- expect(number.international).to eq('+52 55 4525 8448')
497
- expect(number.national).to eq('55 4525 8448')
498
-
499
- intl = number.international
500
-
501
- expect(Phonelib.valid?(intl)).to be true
502
- expect(Phonelib.valid_for_country?(intl, 'mx')).to be true
503
- end
504
- end
505
-
506
- context 'issue #43' do
507
- it 'should parse german five-digit area codes correctly' do
508
- number = Phonelib.parse('+492304973401', 'de')
509
- expect(number.valid?).to be true
510
- expect(number.international).to eq('+49 2304 973401')
511
- expect(number.national).to eq('02304 973401')
512
- expect(number.geo_name).to eq('Schwerte')
513
- end
514
- end
515
-
516
- context 'issue #45' do
517
- it 'should parse possible finish number' do
518
- number = Phonelib.parse('030710', :fi)
519
- expect(number.valid?).to be false
520
- expect(number.possible?).to be true
521
- end
522
- end
523
-
524
- context 'issue #46' do
525
- it "2503019 should be possible number for us, but can't" do
526
- # this number can't be possible, it matches only with generalDesc
527
- # possible pattern, but it is not possible for any of the country types.
528
- # Google's library returns possible because of generalDesc match,
529
- # this library works in a different way, it should now the type of phone,
530
- # so this library can't determine number as possible
531
- number = Phonelib.parse('2503019', :us)
532
- expect(number.valid?).to be false
533
- expect(number.possible?).to be false
534
- end
535
-
536
- it '026875105 should be possible number for hk' do
537
- number = Phonelib.parse('026875105', :hk)
538
- expect(number.valid?).to be false
539
- expect(number.possible?).to be true
540
- end
541
- end
542
-
543
- context 'issue #49' do
544
- it 'should be invalid for countries if + present' do
545
- expect(Phonelib.valid_for_country?('+591 3 3466166', 'DE')).to be false
546
- expect(Phonelib.valid_for_country?('+55 11 2606-1011', 'DE')).to be false
547
- expect(Phonelib.valid_for_country?('+7 926 398-00-95', 'DE')).to be false
548
- expect(Phonelib.valid_for_country?('+55 1 5551234', 'AT')).to be false
549
- expect(Phonelib.valid_for_country?('+57 1 2265858', 'DE')).to be false
550
- end
551
-
552
- it 'should be valid for countries if no + in number' do
553
- expect(Phonelib.valid_for_country?('591 3 3466166', 'DE')).to be true
554
- expect(Phonelib.valid_for_country?('55 11 2606-1011', 'DE')).to be true
555
- expect(Phonelib.valid_for_country?('55 1 5551234', 'AT')).to be true
556
- expect(Phonelib.valid_for_country?('57 1 2265858', 'DE')).to be true
557
- end
558
- end
559
-
560
- context 'the country has a specific rule for parsing a national code' do
561
- let(:valid_belarus_national_number){ Phonelib.parse('80298570767', 'BY') }
562
-
563
- it { expect(valid_belarus_national_number).to be_valid }
564
- end
565
-
566
- context 'issue #51, outdated data' do
567
- it 'should return TT as country' do
568
- Phonelib.default_country = nil
569
- phone = Phonelib.parse('+18682739106')
570
- expect(phone.country).to eq('TT')
571
- end
572
- end
573
-
574
- context 'issue #54' do
575
- it 'should be fixed_or_mobile when phone valid for both but different patterns' do
576
- phone = Phonelib.parse '+15146591112'
577
- expect(phone.valid?).to be true
578
- expect(phone.type).to eq(:fixed_or_mobile)
579
- expect(phone.types).to eq([:fixed_or_mobile])
580
- end
581
- end
582
-
583
- context 'issue #55' do
584
- it 'should not throw error' do
585
- phone = Phonelib.parse('119660086441')
586
- expect(phone.possible?).to be true
587
- end
588
- end
589
-
590
- context 'issue #57' do
591
- it 'should return US as country' do
592
- phone = Phonelib.parse('+17295470713')
593
- expect(phone.valid?).to be false
594
- expect(phone.possible?).to be true
595
- expect(phone.country).to eq('US')
596
- expect(phone.valid_country).to be_nil
597
- end
598
- end
599
-
600
- context 'area_code method' do
601
- it 'should return area code' do
602
- expect(Phonelib.parse('+61 3 9876 0010').area_code).to eq('3')
603
- expect(Phonelib.parse('+44 (0) 20-7031-3000').area_code).to eq('20')
604
- expect(Phonelib.parse('+852 2699 2838').area_code).to be_nil
605
- end
606
-
607
- it 'should return area code if number is geo' do
608
- expect(Phonelib.parse('+16502530000').area_code).to eq('650')
609
- expect(Phonelib.parse('+18002530000').area_code).to be_nil
610
- expect(Phonelib.parse('+442070313000').area_code).to eq('20')
611
- expect(Phonelib.parse('+447912345678').area_code).to be_nil
612
- expect(Phonelib.parse('+61236618300').area_code).to eq('2')
613
- expect(Phonelib.parse('+390236618300').area_code).to eq('02')
614
- expect(Phonelib.parse('+6565218000').area_code).to be_nil
615
- expect(Phonelib.parse('+1650253000').area_code).to be_nil
616
- expect(Phonelib.parse('+80012345678').area_code).to be_nil
617
- expect(Phonelib.parse('+61236618300').area_code).to eq('2')
618
- expect(Phonelib.parse('+5491132277150').area_code).to eq('11')
619
-
620
- end
621
- end
622
-
623
- context 'local_number method' do
624
- it 'should return local number' do
625
- expect(Phonelib.parse('+61 3 9876 0010').local_number).to eq('9876 0010')
626
- expect(Phonelib.parse('+44 (0) 20-7031-3000').local_number).to eq('7031 3000')
627
- expect(Phonelib.parse('+852 2699 2838').local_number).to eq('2699 2838')
628
- end
629
- end
630
-
631
- context 'phone with extension' do
632
- it 'should parse phone as valid' do
633
- %w(972542234567#123 972542234567#ext=123 972542234567;123
634
- 972542234567;ext=123 972542234567#12;3 972542234567;1#23).each do |p|
635
-
636
- phone = Phonelib.parse(p)
637
- expect(phone.valid?).to be true
638
- expect(phone.e164).to eq('+972542234567')
639
- expect(phone.extension).to eq('123')
640
- expect(phone.full_e164).to eq('+972542234567;123')
641
- expect(phone.full_international).to eq('+972 54-223-4567;123')
642
- expect(phone.full_national).to eq('054-223-4567;123')
643
- end
644
- end
645
-
646
- it 'should return nil if extension was not passed' do
647
- phone = Phonelib.parse('972542234567')
648
- expect(phone.valid?).to be true
649
- expect(phone.extension).to eq('')
650
- expect(phone.full_e164).to eq('+972542234567')
651
- end
652
-
653
- it 'should sanitize extension' do
654
- phone = Phonelib.parse('972542234567#sdfsdf')
655
- expect(phone.valid?).to be true
656
- expect(phone.extension).to eq('')
657
- end
658
-
659
- it 'should set different extension separator' do
660
- Phonelib.extension_separator = '#'
661
-
662
- phone = Phonelib.parse('972542234567#123')
663
- expect(phone.valid?).to be true
664
- expect(phone.e164).to eq('+972542234567')
665
- expect(phone.extension).to eq('123')
666
- expect(phone.full_e164).to eq('+972542234567#123')
667
- expect(phone.full_international).to eq('+972 54-223-4567#123')
668
- end
669
-
670
- it 'should support nil separator' do
671
- Phonelib.extension_separate_symbols = nil
672
-
673
- phone = Phonelib.parse('972542234567#123')
674
- expect(phone.original).to eq('972542234567#123')
675
- expect(phone.sanitized).to eq('972542234567123')
676
- expect(phone.extension).to eq('')
677
- end
678
- end
679
-
680
- context 'issue #59' do
681
- it 'should be invalid if parse_special is false' do
682
- expect(Phonelib.parse_special).to be false
683
- expect(Phonelib.valid?("really1511@now.com")).to be false
684
- end
685
-
686
- it 'should be valid if parse_special is true' do
687
- Phonelib.parse_special = true
688
- expect(Phonelib.parse_special).to be true
689
- expect(Phonelib.valid?("really1511@now.com")).to be true
690
- end
691
- end
692
-
693
- context 'issue #61' do
694
- it 'should be valid number in India' do
695
- phone = Phonelib.parse('9111844757')
696
- expect(phone.valid?).to be true
697
- expect(phone.sanitized).to eq('9111844757')
698
- expect(phone.e164).to eq('+919111844757')
699
- expect(Phonelib.valid?('919111844757')).to be true
700
-
701
- phone = Phonelib.parse('49266444201')
702
- expect(phone.valid?).to be true
703
- expect(phone.sanitized).to eq('49266444201')
704
- expect(phone.e164).to eq('+49266444201')
705
- phone = Phonelib.parse('4949266444201')
706
- expect(phone.valid?).to be true
707
- expect(phone.sanitized).to eq('4949266444201')
708
- expect(phone.e164).to eq('+4949266444201')
709
- end
710
- end
711
-
712
- context 'issue #60' do
713
- it 'should be valid for CN with national prefix' do
714
- expect(Phonelib.valid_for_country?('2987388888', 'CN')).to be true
715
- expect(Phonelib.valid_for_country?('02987388888', 'CN')).to be true
716
- end
717
- end
718
-
719
- context 'issue #67' do
720
- it 'should parse CA numbers as valid numbers' do
721
- expect(Phonelib.parse('3065555555', 'CA').valid?).to be true
722
- expect(Phonelib.parse('4165555555', 'CA').valid?).to be true
723
- end
724
- end
725
-
726
- context 'issue #70' do
727
- after :each do
728
- Phonelib.strict_check = false
729
- end
730
-
731
- it 'should be invalid if strict_check is true' do
732
- Phonelib.strict_check = true
733
- expect(Phonelib.valid?("1212a5551234")).to be false
734
- end
735
-
736
- it 'should be valid if strict_check is false' do
737
- expect(Phonelib.strict_check).to be false
738
- expect(Phonelib.valid?("1212a5551234")).to be true
739
- end
740
-
741
- it 'should be valid if strict_check is true' do
742
- Phonelib.strict_check = true
743
- expect(Phonelib.valid?("12125551234")).to be true
744
- end
745
- end
746
-
747
- context 'issue #72' do
748
- it 'should be invalid number' do
749
- expect(Phonelib.parse('+49157123456789', 'de').international).to eq('+49 15712 345 6789')
750
- expect(Phonelib.parse('+49157123456789', 'de').valid?).to be false
751
- end
752
-
753
- it 'should not try to detect double prefix and keep invalid' do
754
- expect(Phonelib.parse('+491521234567', 'de').international).to eq('+49 152 123 4567')
755
- expect(Phonelib.parse('+491521234567', 'de').valid?).to be false
756
- end
757
-
758
- it 'should try to detect country and change it' do
759
- expect(Phonelib.parse('+521234567891', 'de').international).to eq('+52 123 456 7891')
760
- expect(Phonelib.parse('+521234567891', 'de').country).to eq('MX')
761
- end
762
-
763
- it 'should be invalid numbers without + and when country passed' do
764
- expect(Phonelib.parse('49157123456789', 'de').international).to eq('+49 15712 345 6789')
765
- expect(Phonelib.parse('49157123456789', 'de').valid?).to be false
766
- expect(Phonelib.parse('491521234567', 'de').international).to eq('+49 491 521234567')
767
- expect(Phonelib.parse('491521234567', 'de').valid?).to be true
768
- end
769
-
770
- it 'should try to detect when default country set but not passed' do
771
- Phonelib.default_country = :de
772
- expect(Phonelib.parse('49157123456789').international).to eq('+49 15712 345 6789')
773
- expect(Phonelib.parse('49157123456789').valid?).to be false
774
- expect(Phonelib.parse('491521234567').international).to eq('+49 491 521234567')
775
- expect(Phonelib.parse('491521234567').valid?).to be true
776
- end
777
- end
778
-
779
- context 'issue #75' do
780
- it 'should return e164 with country code' do
781
- Phonelib.default_country = :us
782
- expect(Phonelib.parse('7876711234').e164).to eq('+17876711234')
783
- expect(Phonelib.parse('7876711234').valid?).to be false
784
- Phonelib.default_country = :pr
785
- expect(Phonelib.parse('7876711234').e164).to eq('+17876711234')
786
- expect(Phonelib.parse('7876711234').valid?).to be true
787
- end
788
- end
789
-
790
- context 'issue #77' do
791
- it 'should not throw error' do
792
- expect(Phonelib.parse('1').e164).to eq('+1')
793
- end
794
- end
795
-
796
- context 'issues #76 and #78' do
797
- it 'should parse with right countries with default country' do
798
- Phonelib.default_country = :us
799
-
800
- expect(Phonelib.parse('+6465550123').e164).to eq('+6465550123')
801
- expect(Phonelib.parse('+47 904 48 617').country).to eq('NO')
802
- expect(Phonelib.parse('+47 924 48 617').country).to eq('NO')
803
- end
804
- end
805
-
806
- context 'issues ##81' do
807
- it 'should not raise errors for non-string inputs' do
808
- Phonelib.default_country = :nz
809
-
810
- expect{Phonelib.parse(6421555444)}.to_not raise_error
811
- end
812
- end
813
-
814
- context 'issue #83' do
815
- it 'should not throw error' do
816
- Phonelib.strict_check = true
817
- expect{Phonelib.parse(';')}.not_to raise_error
818
- Phonelib.strict_check = false
819
- end
820
- end
821
-
822
- context 'issue #80' do
823
- it 'should return right area code' do
824
- expect(Phonelib.parse('+15306355653').area_code).to eq('530')
825
- end
826
- end
827
-
828
- context 'issue #79' do
829
- it 'should be valid number for claro colombia' do
830
- expect(Phonelib.parse('+573234827533').valid?).to be true
831
- expect(Phonelib.parse('+573202605272').valid?).to be true
832
- end
833
- end
834
-
835
- context 'issue #85' do
836
- it 'should validate without strict and sanitize non numbers' do
837
- expect(Phonelib.valid?('441684291707')).to be true
838
- expect(Phonelib.valid?('+441684291707')).to be true
839
- expect(Phonelib.valid?('+4416842917076')).to be false
840
- expect(Phonelib.valid?('+441684291707x')).to be true
841
- expect(Phonelib.valid?('+441684291707xxxxxxxxxxxxxxxxxasdasadadas')).to be true
842
- end
843
-
844
- it 'should validate right with strict and sanitize only first +' do
845
- Phonelib.strict_check = true
846
-
847
- expect(Phonelib.valid?('441684291707')).to be true
848
- expect(Phonelib.valid?('+441684291707')).to be true
849
- expect(Phonelib.valid?('+4416842917076')).to be false
850
- expect(Phonelib.valid?('+441684291707x')).to be false
851
- expect(Phonelib.valid?('+441684291707xxxxxxxxxxxxxxxxxasdasadadas')).to be false
852
-
853
- Phonelib.strict_check = false
854
- end
855
- end
856
-
857
- context 'issue #87' do
858
- it 'should parse double IT country prefix' do
859
- expect(Phonelib.parse('3911234567', 'IT').national(false)).to eq('3911234567')
860
- expect(Phonelib.parse('3911234567', 'IT').valid?).to be true
861
- expect(Phonelib.parse('3911234567', 'IT').type).to eq(:mobile)
862
-
863
- expect(Phonelib.parse('+393911234567', 'IT').national(false)).to eq('3911234567')
864
- expect(Phonelib.parse('+393911234567', 'IT').valid?).to be true
865
- expect(Phonelib.parse('+393911234567', 'IT').type).to eq(:mobile)
866
-
867
- expect(Phonelib.parse('3921234567', 'IT').type).to eq(:mobile)
868
- expect(Phonelib.parse('3921234567', 'IT').national(false)).to eq('3921234567')
869
- expect(Phonelib.parse('3921234567', 'IT').valid?).to be true
870
-
871
- expect(Phonelib.parse('39391234', 'IT').valid?).to be false
872
- expect(Phonelib.parse('39391234', 'IT').possible?).to be true
873
- end
874
- end
875
-
876
- context 'issue #88' do
877
- it 'should return raw national number when valid' do
878
- phone = Phonelib.parse('+97221234567')
879
- expect(phone.raw_national).to eq('21234567')
880
- expect(phone.national).to eq('02-123-4567')
881
- end
882
-
883
- it 'should return raw national number when invalid' do
884
- phone = Phonelib.parse('+97221')
885
- expect(phone.raw_national).to eq('97221')
886
- expect(phone.national).to eq('97221')
887
- end
888
-
889
- it 'should return raw national number when possible' do
890
- phone = Phonelib.parse('+9721111111')
891
- expect(phone.raw_national).to eq('1111111')
892
- expect(phone.national).to eq('1111111')
893
- end
894
- end
895
-
896
- context 'issue #90' do
897
- it 'should return same results' do
898
- Phonelib.default_country = 'US'
899
- number = '4035566466'
900
- expect(Phonelib.possible?(number)).to be true
901
- expect(Phonelib.parse(number).possible?).to be true
902
- expect(Phonelib.parse(number, Phonelib.default_country).possible?).to be true
903
- end
904
- end
905
-
906
- context 'issue #100 - for country NO' do
907
- cell_numbers = [
908
- # Control examples
909
- '95098471', '41044927', '92859554',
910
- # Numbers starting with 47 without problems
911
- '47465724', '47944424', '47898180',
912
- # Numers starting with 471 with problems
913
- '47144752', '47152183', '47140633'
914
- ].freeze
915
-
916
- cell_numbers.each do |number|
917
- context "with phone number #{number}" do
918
- before :all do
919
- @number = number
920
- @phone = Phonelib.parse(@number, 'NO')
921
- end
922
-
923
- it 'should be valid' do
924
- expect(@phone.valid?).to be true
925
- end
926
-
927
- it 'should have right national' do
928
- expect(@phone.national(false)).to eq(@number)
929
- end
930
-
931
- it 'should have right e164' do
932
- expect(@phone.e164).to eq("+47#{@number}")
933
- end
934
- end
935
- end
936
- end
937
-
938
- context 'issue #102 vanity numbers' do
939
- it 'should be invalid' do
940
- expect(Phonelib.vanity_conversion).to be false
941
-
942
- p = Phonelib.parse('800-44-STERN', 'US')
943
- expect(p.valid?).to be false
944
- end
945
-
946
- it 'should be invalid' do
947
- Phonelib.vanity_conversion = true
948
-
949
- p = Phonelib.parse('800-44-STERN', 'US')
950
- expect(p.valid?).to be true
951
- expect(p.e164).to eq('+18004478376')
952
- end
953
- end
954
-
955
- context 'issue #103 to_s method' do
956
- it 'should return e164 if valid' do
957
- expect(Phonelib.parse('441684291707').to_s).to eq('+441684291707')
958
- end
959
-
960
- it 'should return original if invalid' do
961
- expect(Phonelib.parse('+442244').to_s).to eq('+442244')
962
- end
963
- end
964
-
965
- context 'issue #105' do
966
- it 'should be valid when original without +' do
967
- expect(Phonelib.valid?('9183082081')).to be true
968
- expect(Phonelib.valid_for_country?('9183082081', 'IN')).to be true
969
- end
970
-
971
- it 'should be invalid when original starts with +' do
972
- expect(Phonelib.valid?('+9183082081')).to be false
973
- expect(Phonelib.valid_for_country?('+9183082081', 'IN')).to be false
974
- end
975
- end
976
-
977
- context 'issue #107' do
978
- it 'should return consistent results for `valid_for_country?` when using the ' +
979
- 'instance method or the class method given the same country and phone number' do
980
- phone_number = '0251092275'
981
- Phonelib.phone_data.keys.each do |country|
982
- expect(Phonelib.valid_for_country?(phone_number, country)).to(
983
- eq(Phonelib.parse(phone_number).valid_for_country?(country))
984
- )
985
- end
986
- end
987
- end
988
-
989
- context 'issue #132' do
990
- it 'should simplify national prefix and make phone valid' do
991
- phone = Phonelib.parse '095532231113', 'MX'
992
- expect(phone.valid?).to be true
993
- expect(phone.international).to eq('+52 55 3223 1113')
994
- expect(phone.country).to eq('MX')
995
- end
996
- end
997
-
998
- context 'issue #133' do
999
- it 'should parse all numbers with extensions correctly' do
1000
- Phonelib.extension_separate_symbols = %w(ext ; # extension)
1001
- ['+1 212-555-5555 ext. 5555', '+1 212-555-5555;5555', '+1 212-555-5555#5555',
1002
- '+1 212-555-5555 extension 5555'].each do |num|
1003
-
1004
- phone = Phonelib.parse(num)
1005
- expect(phone.valid?).to be true
1006
- expect(phone.international).to eq('+1 212-555-5555')
1007
- expect(phone.extension).to eq('5555')
1008
- end
1009
- end
1010
- end
1011
-
1012
- context 'issue #135' do
1013
- it 'should be valid numbers for poland with double country prefix' do
1014
- Phonelib.default_country = 'PL'
1015
-
1016
- %w(716287061 486287061).each do |phone|
1017
- expect(Phonelib.parse(phone).valid?).to be true
1018
- expect(Phonelib.parse("+48#{phone}").valid?).to be true
1019
- end
1020
- Phonelib.default_country = nil
1021
- end
1022
- end
1023
-
1024
- context 'issue #127' do
1025
- it 'should be valid numbers for india starting with 6' do
1026
- expect(Phonelib.parse('916000123456').valid?).to be true
1027
- expect(Phonelib.parse('916000123456').valid?).to be true
1028
- end
1029
- end
1030
-
1031
- context 'issue #138' do
1032
- it 'allowing 00 as international prefix' do
1033
- expect(Phonelib.parse('0012015550123').valid?).to be true
1034
- expect(Phonelib.parse('0012015550123').country).to eq('US')
1035
- expect(Phonelib.parse('00441684291707').valid?).to be true
1036
- expect(Phonelib.parse('00441684291707').country).to eq('GB')
1037
- end
1038
- end
1039
-
1040
- context 'issue #140' do
1041
- it 'should be valid numbers for india with default country' do
1042
- Phonelib.default_country = 'IN'
1043
-
1044
- expect(Phonelib.parse('8340412345').valid?).to be true
1045
- expect(Phonelib.parse('7970012345').valid?).to be true
1046
-
1047
- Phonelib.default_country = nil
1048
- end
1049
- end
1050
-
1051
- # https://github.com/daddyz/phonelib/issues/157
1052
- describe 'equality' do
1053
- let(:parsed_number) { Phonelib.parse(raw_number) }
1054
- let(:raw_number) { '281-330-8004' }
1055
-
1056
- before { Phonelib.default_country = 'US' }
1057
- after { Phonelib.default_country = nil }
1058
-
1059
- context 'when given a number as a string' do
1060
- it 'is equal' do
1061
- expect(parsed_number).to eq raw_number
1062
- end
1063
- end
1064
-
1065
- context 'when given identical parsed numbers' do
1066
- it 'is equal' do
1067
- expect(parsed_number).to eq Phonelib.parse(raw_number)
1068
- end
1069
- end
1070
-
1071
- context 'when given different representations of the same number' do
1072
- it 'is equal' do
1073
- expect(parsed_number).to eq raw_number.tr('-', '')
1074
- end
1075
- end
1076
-
1077
- context 'when given different numbers' do
1078
- it 'is not equal' do
1079
- expect(parsed_number).not_to eq '281-330-8005'
1080
- end
1081
- end
1082
-
1083
- context 'when numbers are invalid' do
1084
- it 'should not be equal' do
1085
- p1 = Phonelib.parse('+12121231234')
1086
- expect(parsed_number).not_to eq p1
1087
- end
1088
- end
1089
- end
1090
-
1091
- context 'issue #161' do
1092
- before do
1093
- Phonelib.strict_double_prefix_check = false
1094
- end
1095
-
1096
- context 'when strict_double_prefix_check is false' do
1097
- it 'should be valid number outside the country' do
1098
- Phonelib.default_country = nil
1099
- phone = Phonelib.parse('9111844757')
1100
- expect(phone.valid?).to be true
1101
- expect(Phonelib.valid?('919111844757')).to be true
1102
- end
1103
-
1104
- it 'should be valid number inside the country' do
1105
- phone = Phonelib.parse('9111844757', 'IN')
1106
- expect(phone.valid?).to be true
1107
- expect(Phonelib.valid?('919111844757')).to be true
1108
-
1109
- Phonelib.default_country = 'IN'
1110
- phone = Phonelib.parse('9111844757')
1111
- expect(phone.valid?).to be true
1112
- end
1113
- end
1114
-
1115
- context 'when strict_double_prefix_check is true' do
1116
- before do
1117
- Phonelib.strict_double_prefix_check = true
1118
- end
1119
-
1120
- it 'should be invalid number outside the country' do
1121
- Phonelib.default_country = nil
1122
- phone = Phonelib.parse('9111844757')
1123
- expect(phone.valid?).to be false
1124
- expect(Phonelib.valid?('919111844757')).to be true
1125
- end
1126
-
1127
- it 'should be valid number inside the country' do
1128
- phone = Phonelib.parse('9111844757', 'IN')
1129
- expect(phone.valid?).to be true
1130
- expect(Phonelib.valid?('919111844757')).to be true
1131
-
1132
- Phonelib.default_country = 'IN'
1133
- phone = Phonelib.parse('9111844757')
1134
- expect(phone.valid?).to be true
1135
- end
1136
- end
1137
- end
1138
-
1139
- context 'valid_country_name method' do
1140
- it 'should not return name for invalid number' do
1141
- phone = Phonelib.parse('+12121231234')
1142
- expect(phone.valid?).to be false
1143
- expect(phone.valid_country_name).to be nil
1144
- end
1145
-
1146
- it 'should return valid country name' do
1147
- phone = Phonelib.parse('+12125551234')
1148
- expect(phone.valid?).to be true
1149
- expect(phone.valid_country_name).to eq('United States')
1150
- end
1151
- end
1152
-
1153
- context 'issue #143' do
1154
- it 'should be valid barbados number' do
1155
- expect(Phonelib.parse('1-246-753-8358', 'BB').valid?).to be true
1156
- end
1157
- end
1158
-
1159
- context 'prefix to international and e164 methods' do
1160
- it 'should accept prefix in international' do
1161
- phone = Phonelib.parse('+12125551234;99')
1162
- expect(phone.valid?).to be true
1163
- expect(phone.international).to eq('+1 212-555-1234')
1164
- expect(phone.international(true, '00')).to eq('001 212-555-1234')
1165
- expect(phone.international('00')).to eq('001 212-555-1234')
1166
- expect(phone.international_00).to eq('001 212-555-1234')
1167
- expect(phone.full_international('00')).to eq('001 212-555-1234;99')
1168
- expect(phone.full_international_00).to eq('001 212-555-1234;99')
1169
- end
1170
-
1171
- it 'should accept prefix in e164' do
1172
- phone = Phonelib.parse('+12125551234;99')
1173
- expect(phone.valid?).to be true
1174
- expect(phone.e164).to eq('+12125551234')
1175
- expect(phone.e164('00')).to eq('0012125551234')
1176
- expect(phone.e164_00).to eq('0012125551234')
1177
- expect(phone.full_e164('00')).to eq('0012125551234;99')
1178
- expect(phone.full_e164_00).to eq('0012125551234;99')
1179
- end
1180
-
1181
- it 'should raise error if bad method name passed' do
1182
- phone = Phonelib.parse('+12125551234;99')
1183
- expect { phone.fff_00 }.to raise_error(NameError)
1184
- end
1185
- end
1186
-
1187
- context 'issue #160' do
1188
- it 'should return international number when intl_format is NA' do
1189
- n = Phonelib.parse('+61 13 12 21', 'au')
1190
- expect(n.valid?).to be(true)
1191
- expect(n.full_international).to eq('+61 131221')
1192
- end
1193
-
1194
- it 'should use intl_format if it is good' do
1195
- p = Phonelib.parse('+12125551234')
1196
- expect(p.valid?).to be(true)
1197
- expect(p.international).to eq('+1 212-555-1234')
1198
- expect(p.national).to eq('(212) 555-1234')
1199
- end
1200
- end
1201
-
1202
- context 'issue #152' do
1203
- it 'should return correct format for MX' do
1204
- p = Phonelib.parse('099991234567', 'MX')
1205
- expect(p.national).to eq('999 123 4567')
1206
- end
1207
- end
1208
-
1209
- context 'issue #171' do
1210
- it 'should return correct format for VN' do
1211
- p = Phonelib.parse('902962207', 'VN')
1212
- expect(p.international).to eq('+84 902 962 207')
1213
- p = Phonelib.parse('844666531', 'VN')
1214
- expect(p.international).to eq('+84 844 666 531')
1215
- end
1216
- end
1217
-
1218
- context 'issue #203' do
1219
- it 'should be valid when sanitize all symbols' do
1220
- p = Phonelib.parse('+1 (713) 555-1212 ; abc')
1221
- expect(p.valid?).to be(true)
1222
- end
1223
-
1224
- it 'should be invalid when sanitize only valuable symbols' do
1225
- Phonelib.sanitize_regex = '[\.\-\(\) \;\+]'
1226
- p = Phonelib.parse('+1 (713) 555-1212 ; abc')
1227
- expect(p.valid?).to be(true)
1228
- end
1229
-
1230
- it 'should be valid when sanitize only valuable symbols' do
1231
- old = Phonelib.sanitize_regex
1232
- Phonelib.sanitize_regex = '[\.\-\(\) \;\+]'
1233
- p = Phonelib.parse('+1 (713) 555-1212')
1234
- expect(p.valid?).to be(true)
1235
- Phonelib.sanitize_regex = old
1236
- end
1237
- end
1238
-
1239
- context 'issue #261' do
1240
- before(:each) do
1241
- Phonelib.ignore_plus = false
1242
- end
1243
-
1244
- after(:each) do
1245
- Phonelib.ignore_plus = false
1246
- end
1247
-
1248
- it 'should parse as valid and change country when plus is not ignored' do
1249
- p = Phonelib.parse("+850 2 381 7980", "US")
1250
- expect(p.valid?).to be(true)
1251
- expect(p.country).to eq('KP')
1252
- end
1253
-
1254
- it 'should parse as invalid when plus is ignored' do
1255
- Phonelib.ignore_plus = true
1256
- p = Phonelib.parse("+850 2 381 7980", "US")
1257
- expect(p.valid?).to be(false)
1258
- expect(p.country).to be(nil)
1259
- end
1260
- end
1261
-
1262
- context 'additional_regexes' do
1263
- before(:each) do
1264
- Phonelib.additional_regexes = []
1265
- end
1266
-
1267
- after(:each) do
1268
- Phonelib.additional_regexes = []
1269
- end
1270
-
1271
- it 'should parse number as valid' do
1272
- phone = '+1-000-000-0000'
1273
- expect(Phonelib.additional_regexes).to eq({})
1274
- p1 = Phonelib.parse(phone)
1275
- expect(p1.valid?).to be(false)
1276
- Phonelib.add_additional_regex :us, Phonelib::Core::MOBILE, '0{10}'
1277
- p2 = Phonelib.parse(phone)
1278
- expect(p2.valid?).to be(true)
1279
- expect(p2.possible?).to be(true)
1280
- expect(p2.international).to eq('+1 000 000 0000')
1281
- expect(p2.country).to eq('US')
1282
- end
1283
-
1284
- it 'dump correct' do
1285
- Phonelib.additional_regexes = []
1286
- expect(Phonelib.additional_regexes).to eq({})
1287
- Phonelib.add_additional_regex :us, Phonelib::Core::MOBILE, '0{10}'
1288
- Phonelib.add_additional_regex :us, Phonelib::Core::MOBILE, '1{10}'
1289
- expect(Phonelib.dump_additional_regexes).to eq([['US', :mobile, '0{10}'], ['US', :mobile, '1{10}']])
1290
- end
1291
-
1292
- it 'load correct' do
1293
- expect(Phonelib.additional_regexes).to eq({})
1294
- Phonelib.additional_regexes = [[:us, :mobile, '0{10}'], [:us, :mobile, '1{10}']]
1295
- expect(Phonelib.dump_additional_regexes).to eq([['US', :mobile, '0{10}'], ['US', :mobile, '1{10}']])
1296
- end
1297
- end
1298
-
1299
- context 'issue #277 double prefix with country' do
1300
- it 'should be invalid unless number starts with country prefix' do
1301
- expect(Phonelib.valid_for_country?("88097679","IN")).to be(false)
1302
- expect(Phonelib.valid_for_country?("+9188097679","IN")).to be(false)
1303
- end
1304
-
1305
- it 'should be valid if number starts with country prefix' do
1306
- expect(Phonelib.valid_for_country?("9188097679","IN")).to be(true)
1307
- end
1308
- end
1309
-
1310
- context 'issue #278 multiple default countries' do
1311
- after(:each) do
1312
- Phonelib.default_country = nil
1313
- end
1314
-
1315
- it 'should be valid if only ca as default country' do
1316
- Phonelib.default_country = :ca
1317
- p1 = Phonelib.parse('6478864691')
1318
- expect(p1.valid?).to be(true)
1319
- expect(p1.countries).to eq(['CA'])
1320
- p2 = Phonelib.parse('6047642951')
1321
- expect(p2.valid?).to be(true)
1322
- expect(p2.countries).to eq(['CA'])
1323
- end
1324
-
1325
- it 'should be valid if only us and ca as default countries' do
1326
- Phonelib.default_country = [:us, :ca]
1327
- p1 = Phonelib.parse('6478864691')
1328
- expect(p1.valid?).to be(true)
1329
- expect(p1.countries).to eq(['CA'])
1330
- p2 = Phonelib.parse('6047642951')
1331
- expect(p2.valid?).to be(true)
1332
- expect(p2.countries).to eq(['CA'])
1333
- end
1334
-
1335
- it 'should be valid if only ca and us as default countries' do
1336
- Phonelib.default_country = [:ca, :us]
1337
- p1 = Phonelib.parse('6478864691')
1338
- expect(p1.valid?).to be(true)
1339
- expect(p1.countries).to eq(['CA'])
1340
- p2 = Phonelib.parse('6047642951')
1341
- expect(p2.valid?).to be(true)
1342
- expect(p2.countries).to eq(['CA'])
1343
- end
1344
- end
1345
-
1346
- context 'issue #304' do
1347
- it 'should strip AR prefix' do
1348
- phone = Phonelib.parse('+540111557447700', 'AR')
1349
- expect(phone.valid_for_country?('AR')).to be(true)
1350
- expect(phone.e164).to eq('+5491157447700')
1351
- expect(phone.national).to eq('011 15-5744-7700')
1352
- expect(phone.international).to eq('+54 9 11 5744-7700')
1353
- end
1354
- end
1355
-
1356
- context 'issue #303' do
1357
- it 'should parse possible numbers if countries array passed' do
1358
- p1 = Phonelib.parse('1212 111 1111', :us)
1359
- expect(p1.valid?).to be(false)
1360
- expect(p1.possible?).to be(true)
1361
-
1362
- p2 = Phonelib.parse('1212 111 1111', %w(US CA))
1363
- expect(p2.valid?).to be(false)
1364
- expect(p2.possible?).to be(true)
1365
-
1366
- expect(p1.e164).to eq(p2.e164)
1367
- end
1368
-
1369
- it 'should parse valid numbers if countries array passed' do
1370
- p1 = Phonelib.parse('16478864691', :ca)
1371
- expect(p1.valid?).to be(true)
1372
- expect(p1.possible?).to be(true)
1373
-
1374
- p2 = Phonelib.parse('16478864691', %w(US CA))
1375
- expect(p2.valid?).to be(true)
1376
- expect(p2.possible?).to be(true)
1377
-
1378
- expect(p1.e164).to eq(p2.e164)
1379
- end
1380
- end
1381
-
1382
- context 'issue #309' do
1383
- it 'should return same result without specifying country' do
1384
- p1 = Phonelib.parse('+55 55 96722 8964', 'BR')
1385
- p2 = Phonelib.parse('+55 55 96722 8964')
1386
- expect(p1.valid?).to be(p2.valid?)
1387
- expect(p1.international).to eq(p2.international)
1388
- end
1389
- end
1390
-
1391
- context 'issue #324' do
1392
- before(:each) do
1393
- Phonelib.additional_regexes = []
1394
- end
1395
-
1396
- after(:each) do
1397
- Phonelib.additional_regexes = []
1398
- end
1399
-
1400
- it 'should not throw error and be valid without additional' do
1401
- expect(Phonelib.parse("+213 551234567").valid?).to be(true)
1402
- end
1403
-
1404
- it 'should not throw error and be valid with additional' do
1405
- Phonelib.additional_regexes = [[:fr, :mobile, '07\d{8}'],[:dz, :mobile, '0(5|6)\d{8}'],]
1406
- p = Phonelib.parse("+213 551234567")
1407
- expect(p.valid?).to be(true)
1408
- expect(p.international).to eq('+213 551 23 45 67')
1409
- end
1410
-
1411
- it 'should not throw error and be valid with additional full match' do
1412
- Phonelib.additional_regexes = [[:fr, :mobile, '^07\d{8}$'],[:dz, :mobile, '^0(5|6)\d{8}$'],]
1413
- p = Phonelib.parse("+213 551234567")
1414
- expect(p.valid?).to be(true)
1415
- expect(p.international).to eq('+213 551 23 45 67')
1416
- end
1417
-
1418
- it 'should not throw error and be valid with several additional' do
1419
- Phonelib.additional_regexes = [[:fr, :mobile, '^07\d{8}$'],[:dz, :mobile, '^0(5|6)\d{18}$'],[:dz, :mobile, '^0(1|2)\d{18}$'],]
1420
- p = Phonelib.parse("+213 551234567")
1421
- expect(p.valid?).to be(true)
1422
- expect(p.international).to eq('+213 551 23 45 67')
1423
- end
1424
-
1425
- it 'should not throw error and be valid with additional without (' do
1426
- Phonelib.additional_regexes = [[:fr, :mobile, '^07\d{8}$'],[:dz, :mobile, '^05|6\d{8}$'],]
1427
- p = Phonelib.parse("+213 551234567")
1428
- expect(p.valid?).to be(true)
1429
- expect(p.international).to eq('+213 551 23 45 67')
1430
- end
1431
-
1432
- it 'should not throw error and be valid with several additional without (' do
1433
- Phonelib.additional_regexes = [[:fr, :mobile, '^07\d{8}$'],[:dz, :mobile, '^05|6\d{18}$'],[:dz, :mobile, '^01|2\d{18}$'],]
1434
- p = Phonelib.parse("+213 551234567")
1435
- expect(p.valid?).to be(true)
1436
- expect(p.international).to eq('+213 551 23 45 67')
1437
- end
1438
- end
1439
-
1440
- context 'issue #332' do
1441
- it 'should parse short number as invalid without replaced prefix' do
1442
- p = Phonelib.parse('+44008123')
1443
- expect(p.valid?).to be(false)
1444
- end
1445
- end
1446
-
1447
- context 'issue #336' do
1448
- it 'should set country without country passed but with plus' do
1449
- p = Phonelib.parse('+813000000000')
1450
- expect(p.valid?).to be(false)
1451
- expect(p.country).to eq('JP')
1452
- end
1453
-
1454
- it 'should set country with country passed' do
1455
- p = Phonelib.parse('+813000000000', :jp)
1456
- expect(p.valid?).to be(false)
1457
- expect(p.country).to eq('JP')
1458
- end
1459
- end
1460
-
1461
- context 'issue #343' do
1462
- it 'should show 1 for country prefix for TT' do
1463
- p = "18687034186"
1464
- phone = Phonelib.parse(p)
1465
- expect(phone.country).to eq('TT')
1466
- expect(phone.country_code).to eq('1')
1467
- end
1468
-
1469
- it 'should match npanxx for DO' do
1470
- p = "18297034186"
1471
- phone = Phonelib.parse(p)
1472
- expect(phone.country).to eq('DO')
1473
- expect(phone.country_code).to eq('1829')
1474
- end
1475
- end
1476
-
1477
- context 'example numbers' do
1478
- it 'are valid' do
1479
- data_file = File.dirname(__FILE__) + '/../data/phone_data.dat'
1480
- phone_data = Marshal.load(File.binread(data_file))
1481
- phone_data.each_value do |data|
1482
- country = data[:id]
1483
- next unless country =~ /[A-Z]{2}/
1484
- data[:types].each do |type, type_data|
1485
- next unless (Phonelib::Core::TYPES_DESC.keys - Phonelib::Core::SHORT_CODES).include? type
1486
- next unless type_data[:example_number]
1487
- type_data[:example_number].split('|').each do |number|
1488
- phone = Phonelib.parse(number, country)
1489
- msg = "Phone #{number} in #{country} of #{type}"
1490
-
1491
- phone_assertions(phone, type, country, msg)
1492
- end
1493
- end
1494
- end
1495
- end
1496
-
1497
- context 'issue #325' do
1498
- it 'should change number according to new format' do
1499
- p = Phonelib.parse("540111557447700")
1500
- expect(p.country).to eq('AR')
1501
- expect(p.e164).to eq('+5491157447700')
1502
- expect(p.valid?).to be(true)
1503
- end
1504
- end
1505
-
1506
- context 'issue #352' do
1507
- it 'should not raise error for area_code' do
1508
- expect(Phonelib.parse("390212345678").area_code).to eq('02')
1509
- expect(Phonelib.parse("6191712222").area_code).to be_nil
1510
- end
1511
- end
1512
-
1513
- def phone_assertions(phone, type, country, msg)
1514
- expect(phone.valid?).to be(true), "#{msg} not valid"
1515
- expect(phone.invalid?).to be(false), "#{msg} not valid"
1516
- expect(phone.possible?).to be(true), "#{msg} not possible"
1517
- expect(phone.impossible?).to be(false), "#{msg} not possible"
1518
- expect(phone.valid_for_country?(country)).to be(true),
1519
- "#{msg} not valid for country"
1520
- expect(phone.invalid_for_country?(country)).to be(false),
1521
- "#{msg} not valid for country"
1522
-
1523
- expect(phone.country).to eq(country), "#{msg} wrong country "
1524
- if phone.type == Phonelib::Core::FIXED_OR_MOBILE
1525
- expect([Phonelib::Core::FIXED_LINE, Phonelib::Core::MOBILE]).to\
1526
- include(type)
1527
- "#{msg} wrong type #{phone.types}"
1528
- else
1529
- expect(phone.types).to include(type),
1530
- "#{msg} wrong type #{phone.types}"
1531
- end
1532
- end
1533
- end
1534
- end