russian_phone 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 16d06f62f45a61c5481be6b1889d0615e889301b
4
+ data.tar.gz: f36afbcdd5309dd268eb4b008e2639e1c56bb163
5
+ SHA512:
6
+ metadata.gz: 7f09c65d39d1aee28f8fbfb1d5b9fd47a21e05ab1da62cd7fcfe38401eeaa14e228937ecb9123e06678afd716d5d96fde22a170fb4c0394a84eb8d515c686ca5
7
+ data.tar.gz: e89db6ae486c9068a29f8523f1cb76a1b5c6966c3eb8e50cbc421af14a1373b30b7c1782f7324508d0b6e67341ff4bf8b888b6eec143f1101599ac4bb50a3a2e
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ russian-phone
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.0.0-p195
data/README.md CHANGED
@@ -12,6 +12,8 @@ https://github.com/joost/phony_rails
12
12
 
13
13
  https://github.com/carr/phone
14
14
 
15
+ https://github.com/sstephenson/global_phone
16
+
15
17
  Указанные решения поддерживают несколько стран
16
18
 
17
19
  russian_phone поддерживает и будет поддерживать только российские телефонные номера
data/lib/russian_phone.rb CHANGED
@@ -13,3 +13,22 @@ module RussianPhone
13
13
  RussianPhone::Field.new(options)
14
14
  end
15
15
  end
16
+
17
+ if Object.const_defined?("RailsAdmin")
18
+ require 'rails_admin/adapters/mongoid'
19
+ require 'rails_admin/config/fields/types/text'
20
+ module RailsAdmin
21
+ module Adapters
22
+ module Mongoid
23
+ alias_method :type_lookup_without_russian_phone, :type_lookup
24
+ def type_lookup(name, field)
25
+ if field.type.class.name == "RussianPhone::Field"
26
+ { :type => :string }
27
+ else
28
+ type_lookup_without_russian_phone(name, field)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,3 +1,3 @@
1
1
  module RussianPhone
2
- VERSION = "0.3.3"
2
+ VERSION = "0.3.4"
3
3
  end
data/spec/phone_spec.rb CHANGED
@@ -1,534 +1,534 @@
1
- # coding: utf-8
2
-
3
- require 'spec_helper'
4
-
5
- describe RussianPhone do
6
- describe 'clean' do
7
- it 'should remove everything except numbers from string' do
8
- RussianPhone::Number.clean('+7 (906) 111-11-11').should eq '79061111111'
9
- end
10
- end
11
-
12
- describe 'extract city' do
13
- it 'should extract city from number correctly' do
14
- RussianPhone::Number.extract('(906) 111-11-11', 7, 3).should eq ['906', '1111111']
15
- RussianPhone::Number.extract('(906) 111-11-11', 6, 4).should eq ['9061', '111111']
16
- RussianPhone::Number.extract('(906) 111-11-11', 5, 5).should eq ['90611', '11111']
17
- end
18
- end
19
-
20
- describe 'when parsing' do
21
- it 'should parse 8-800-111-11-11 number' do
22
- phone = RussianPhone::Number.new('8-800-111-11-11', default_country: 7)
23
- phone.should_not be_nil
24
- phone.valid?.should be_true
25
-
26
- phone.cell?.should be_false
27
- phone.free?.should be_true
28
-
29
- phone.city.should eq '800'
30
- phone.country.should eq '7'
31
- phone.subscriber.should eq '1111111'
32
- phone.full.should eq '8-800-111-11-11'
33
- end
34
-
35
- it 'should parse (906) 111-11-11 number' do
36
- phone = RussianPhone::Number.new('(906) 111-11-11', default_country: 7)
37
- phone.should_not be_nil
38
- phone.valid?.should be_true
39
-
40
- phone.cell?.should be_true
41
- phone.free?.should be_false
42
-
43
- phone.city.should eq '906'
44
- phone.country.should eq '7'
45
- phone.subscriber.should eq '1111111'
46
- phone.full.should eq '+7 (906) 111-11-11'
47
- end
48
-
49
- it 'should parse 111-11-11 number with default city' do
50
- phone = RussianPhone::Number.new('111-11-11', default_country: 7, default_city: 495)
51
- phone.should_not be_nil
52
- phone.valid?.should be_true
53
-
54
- phone.cell?.should be_false
55
- phone.free?.should be_false
56
-
57
- phone.city.should eq '495'
58
- phone.country.should eq '7'
59
- phone.subscriber.should eq '1111111'
60
- phone.full.should eq '+7 (495) 111-11-11'
61
- end
62
-
63
- it 'should not parse 111-11-11 number without default city' do
64
- phone = RussianPhone::Number.new('111-11-11', default_country: 7)
65
- phone.valid?.should be_false
66
- end
67
-
68
- it 'should parse 1111111 number with default city' do
69
- phone = RussianPhone::Number.new('1111111', default_country: 7, default_city: 495)
70
- phone.should_not be_nil
71
- phone.valid?.should be_true
72
-
73
- phone.cell?.should be_false
74
- phone.free?.should be_false
75
-
76
- phone.city.should eq '495'
77
- phone.country.should eq '7'
78
- phone.subscriber.should eq '1111111'
79
- phone.full.should eq '+7 (495) 111-11-11'
80
- end
81
-
82
- it 'should parse +7 (495) 111-11-11 number' do
83
- phone = RussianPhone::Number.new('+7 (495) 111-11-11')
84
- phone.should_not be_nil
85
- phone.valid?.should be_true
86
-
87
- phone.cell?.should be_false
88
- phone.free?.should be_false
89
-
90
- phone.city.should eq '495'
91
- phone.country.should eq '7'
92
- phone.subscriber.should eq '1111111'
93
- phone.full.should eq '+7 (495) 111-11-11'
94
- end
95
-
96
- it 'should parse +7 (495) 111-11-11 number' do
97
- phone = RussianPhone::Number.new('+7 (495) 111-11-11')
98
- phone.should_not be_nil
99
- phone.valid?.should be_true
100
-
101
- phone.cell?.should be_false
102
- phone.free?.should be_false
103
-
104
- phone.city.should eq '495'
105
- phone.country.should eq '7'
106
- phone.subscriber.should eq '1111111'
107
- phone.full.should eq '+7 (495) 111-11-11'
108
- end
109
-
110
- it 'should parse 8(4912)12-34-56 number' do
111
- phone = RussianPhone::Number.new('+7 (4912) 12-34-56', default_country: 7)
112
- phone.should_not be_nil
113
- phone.valid?.should be_true
114
-
115
- phone.cell?.should be_false
116
- phone.free?.should be_false
117
-
118
- phone.city.should eq '4912'
119
- phone.country.should eq '7'
120
- phone.subscriber.should eq '123456'
121
- phone.full.should eq '+7 (4912) 12-34-56'
122
- end
123
-
124
- it 'should parse 2-34-56 number whith default city' do
125
- phone = RussianPhone::Number.new('2-34-56', default_country: 7, default_city: 87252)
126
- phone.should_not be_nil
127
- phone.valid?.should be_true
128
-
129
- phone.cell?.should be_false
130
- phone.free?.should be_false
131
-
132
- phone.city.should eq '87252'
133
- phone.country.should eq '7'
134
- phone.subscriber.should eq '23456'
135
- phone.full.should eq '+7 (87252) 2-34-56'
136
- end
137
-
138
- it 'should parse 12-34-56 number whith default city' do
139
- phone = RussianPhone::Number.new('12-34-56', default_country: 7, default_city: 4912)
140
- phone.should_not be_nil
141
- phone.valid?.should be_true
142
-
143
- phone.cell?.should be_false
144
- phone.free?.should be_false
145
-
146
- phone.city.should eq '4912'
147
- phone.country.should eq '7'
148
- phone.subscriber.should eq '123456'
149
- phone.full.should eq '+7 (4912) 12-34-56'
150
- end
151
-
152
- it 'should parse 8(34356)5-67-89 number correctly' do
153
- # это номер в Екатеринбурге, с неправильно указанным кодом города
154
-
155
- phone = RussianPhone::Number.new('8(34356)5-67-89', default_country: 7)
156
- phone.should_not be_nil
157
- phone.valid?.should be_true
158
-
159
- phone.cell?.should be_false
160
- phone.free?.should be_false
161
-
162
- phone.city.should eq '343'
163
- phone.country.should eq '7'
164
- phone.subscriber.should eq '5656789'
165
- phone.full.should eq '+7 (343) 565-67-89'
166
- end
167
-
168
- it 'should parse 7 (906) 123-45-67 number correctly' do
169
- # это номер в Екатеринбурге, с неправильно указанным кодом города
170
-
171
- phone = RussianPhone::Number.new('7 (906) 123-45-67', default_country: 7)
172
- phone.should_not be_nil
173
- phone.valid?.should be_true
174
-
175
- phone.cell?.should be_true
176
- phone.free?.should be_false
177
- phone.extra.should eq ''
178
- phone.city.should eq '906'
179
- phone.country.should eq '7'
180
- phone.subscriber.should eq '1234567'
181
- phone.full.should eq '+7 (906) 123-45-67'
182
- end
183
-
184
- it 'should handle phones with extra stuff [8 (906) 111-11-11 доб. 123]' do
185
- phone = RussianPhone::Number.new('8 (906) 111-11-11 доб. 123', default_country: 7)
186
- phone.should_not be_nil
187
- phone.valid?.should be_true
188
-
189
- phone.cell?.should be_true
190
- phone.free?.should be_false
191
-
192
- phone.city.should eq '906'
193
- phone.country.should eq '7'
194
- phone.subscriber.should eq '1111111'
195
- phone.full.should eq '+7 (906) 111-11-11 доб. 123'
196
- end
197
-
198
- it 'should handle phones with extra stuff [8 (906) 111-11-11 д. 123]' do
199
- phone = RussianPhone::Number.new('8 (906) 111-11-11 д. 123', default_country: 7)
200
- phone.should_not be_nil
201
- phone.valid?.should be_true
202
-
203
- phone.cell?.should be_true
204
- phone.free?.should be_false
205
-
206
- phone.city.should eq '906'
207
- phone.country.should eq '7'
208
- phone.subscriber.should eq '1111111'
209
- phone.full.should eq '+7 (906) 111-11-11 д. 123'
210
- end
211
-
212
- it 'should handle phones with extra stuff [89061010101 д. 123]' do
213
- phone = RussianPhone::Number.new('89061010101 д. 123', default_country: 7)
214
- phone.should_not be_nil
215
- phone.valid?.should be_true
216
-
217
- phone.cell?.should be_true
218
- phone.free?.should be_false
219
-
220
- phone.city.should eq '906'
221
- phone.country.should eq '7'
222
- phone.subscriber.should eq '1010101'
223
- phone.full.should eq '+7 (906) 101-01-01 д. 123'
224
- end
225
-
226
- it 'should handle phones with extra stuff [89061010101-д. 123]' do
227
- phone = RussianPhone::Number.new('89061010101-д. 123', default_country: 7)
228
- phone.should_not be_nil
229
- phone.valid?.should be_true
230
-
231
- phone.cell?.should be_true
232
- phone.free?.should be_false
233
-
234
- phone.city.should eq '906'
235
- phone.country.should eq '7'
236
- phone.subscriber.should eq '1010101'
237
- phone.full.should eq '+7 (906) 101-01-01 -д. 123'
238
- end
239
-
240
- it 'should handle phones with unknown codes [8 (533) 111-11-11]' do
241
- phone = RussianPhone::Number.new('8 (533) 111-11-11', default_country: 7)
242
- phone.should_not be_nil
243
- phone.valid?.should be_true
244
-
245
- phone.cell?.should be_false
246
- phone.free?.should be_false
247
-
248
- phone.city.should eq '533'
249
- phone.country.should eq '7'
250
- phone.subscriber.should eq '1111111'
251
- phone.full.should eq '+7 (533) 111-11-11'
252
- end
253
-
254
- tests = {
255
- '+79261234567' => [7, 926, 1234567],
256
- '89261234567' => [7, 926, 1234567],
257
- '79261234567' => [7, 926, 1234567],
258
- '+7 926 123 45 67' => [7, 926, 1234567],
259
- '8(926)123-45-67' => [7, 926, 1234567],
260
- '8 (926) 12-3-45-67' => [7, 926, 1234567],
261
- '9261234567' => [7, 926, 1234567],
262
- '(495)1234567' => [7, 495, 1234567],
263
- '(495)123 45 67' => [7, 495, 1234567],
264
- '8-926-123-45-67' => [7, 926, 1234567],
265
- '8 927 1234 234' => [7, 927, 1234234],
266
- '8 927 12 12 234' => [7, 927, 1212234],
267
- '8 927 1 2 3 4 2 3 4' => [7, 927, 1234234],
268
- '8 927 12 34 234' => [7, 927, 1234234],
269
- '8 927 12 342 34' => [7, 927, 1234234],
270
- '8 927 123-4-234' => [7, 927, 1234234],
271
- '8 (927) 12 342 34' => [7, 927, 1234234],
272
- '8-(927)-12-342-34' => [7, 927, 1234234],
273
- '+7 927 1234 234' => [7, 927, 1234234],
274
- '+7 927 12 12 234' => [7, 927, 1212234],
275
- '+7 927 1 2 3 4 2 3 4' => [7, 927, 1234234],
276
- '+7 927 12 34 234' => [7, 927, 1234234],
277
- '+7 927 12 342 34' => [7, 927, 1234234],
278
- '+7 927 123-4-234' => [7, 927, 1234234],
279
- '+7 (927) 12 342 34' => [7, 927, 1234234],
280
- '+7-(927)-12-342-34' => [7, 927, 1234234],
281
- '7 927 1234 234' => [7, 927, 1234234],
282
- '7 927 12 12 234' => [7, 927, 1212234],
283
- '7 927 1 2 3 4 2 3 4' => [7, 927, 1234234],
284
- '7 927 12 34 234' => [7, 927, 1234234],
285
- '7 927 12 342 34' => [7, 927, 1234234],
286
- '7 927 123-4-234' => [7, 927, 1234234],
287
- '7 (927) 12 342 34' => [7, 927, 1234234],
288
- '7-(927)-12-342-34' => [7, 927, 1234234],
289
- '7 84543 123 12' => [7, 84543, 12312],
290
- '78454312312' => [7, 84543, 12312],
291
- '88454312312' => [7, 84543, 12312],
292
- }
293
-
294
- tests.each_pair do |str, result|
295
- it "should parse #{str}" do
296
- phone = RussianPhone::Number.new(str, default_country: 7)
297
- phone.should_not be_nil
298
- phone.valid?.should be_true
299
-
300
- if %w(927 926).include? result[1].to_s
301
- phone.cell?.should be_true
302
- else
303
- phone.cell?.should be_false
304
- end
305
-
306
- phone.free?.should be_false
307
-
308
- phone.country.should eq result[0].to_s
309
- phone.city.should eq result[1].to_s
310
- phone.subscriber.should eq result[2].to_s
311
- phone.clean.should eq "#{result[0]}#{result[1]}#{result[2]}"
312
- end
313
- end
314
-
315
- bad_phones = [
316
- '123 123',
317
- '000000',
318
- '123',
319
- 'пыщ пыщ ололо я водитель НЛО',
320
- '11 1 11 1'
321
- ]
322
-
323
- bad_phones.each do |phone|
324
- it "should not parse #{phone}" do
325
- phone = RussianPhone::Number.new(phone, default_country: 7)
326
- phone.valid?.should be_false
327
- end
328
- end
329
- end
330
-
331
- describe 'when using ::Field' do
332
- it 'should serialize and deserialize correctly' do
333
- RussianPhone::Number.new('8 (906) 111-11-11 д. 123').mongoize.should eq '+7 (906) 111-11-11 д. 123'
334
-
335
- RussianPhone::Number.new('495 111 11 11').mongoize.should eq '+7 (495) 111-11-11'
336
-
337
- RussianPhone::Number.demongoize('+7 (495) 111-11-11').mongoize.should eq '+7 (495) 111-11-11'
338
-
339
- RussianPhone::Field.mongoize(RussianPhone::Number.new('495 111 11 11')).should eq '+7 (495) 111-11-11'
340
- RussianPhone::Field.evolve(RussianPhone::Number.new('495 111 11 11')).should eq '+7 (495) 111-11-11'
341
- RussianPhone::Field.evolve(RussianPhone::Number.new('495 111 11 11 доб 123')).should eq '+7 (495) 111-11-11 доб 123'
342
- end
343
- end
344
-
345
- describe 'when storing with mongoid' do
346
- it 'should parse, store and retrieve numbers correctly' do
347
- u = User.new(name: 'test', phone: '906 111 11 11')
348
- u.save.should be_true
349
- u = User.first
350
-
351
- u.phone.should eq '+7 (906) 111-11-11'
352
- u.phone.cell?.should be_true
353
- u.phone.free?.should be_false
354
-
355
- u.phone.clean.should eq '79061111111'
356
-
357
- u.phone.country.should eq '7'
358
- u.phone.city.should eq '906'
359
- u.phone.subscriber.should eq '1111111'
360
- end
361
-
362
- it 'should respect field options' do
363
- u = User.create(name: 'test', phone_in_495: '111 11 11', phone_in_812: '222 2222')
364
- u.save.should be_true
365
-
366
- u = User.first
367
-
368
- u.phone_in_495.class.name.should eq 'RussianPhone::Number'
369
- u.phone_in_495.should eq '+7 (495) 111-11-11'
370
- u.phone_in_495.cell?.should be_false
371
- u.phone_in_495.free?.should be_false
372
-
373
- u.phone_in_495.country.should eq '7'
374
- u.phone_in_495.city.should eq '495'
375
- u.phone_in_495.subscriber.should eq '1111111'
376
-
377
- u.phone_in_495.clean.should eq '74951111111'
378
- u.phone_in_495.full.should eq '+7 (495) 111-11-11'
379
-
380
- u.phone_in_812.class.name.should eq 'RussianPhone::Number'
381
- u.phone_in_812.should eq '+7 (812) 222-22-22'
382
- u.phone_in_812.cell?.should be_false
383
- u.phone_in_812.free?.should be_false
384
-
385
- u.phone_in_812.country.should eq '7'
386
- u.phone_in_812.city.should eq '812'
387
- u.phone_in_812.subscriber.should eq '2222222'
388
-
389
- u.phone_in_812.clean.should eq '78122222222'
390
- u.phone_in_812.full.should eq '+7 (812) 222-22-22'
391
- end
392
-
393
- it 'should fail validation when validate is on and phone is invalid' do
394
- u = UserWithValidation.new(phone: '123')
395
- u.valid?.should be_false
396
- u.save.should be_false
397
- u.errors.messages.should eq({:phone =>["Неверный телефонный номер"]})
398
- end
399
-
400
- it 'should pass validation when validate is on and phone is valid' do
401
- u = UserWithValidation.new(phone: '495 121 11 11')
402
- u.valid?.should be_true
403
- u.save.should be_true
404
- UserWithValidation.first.read_attribute(:phone).should eq '+7 (495) 121-11-11'
405
- UserWithValidation.first.phone.should eq '+7 (495) 121-11-11'
406
- end
407
-
408
- it 'should pass validation when validate is on and phone is valid' do
409
- u = UserWithValidation.new(phone: '7495 121 11 11')
410
- u.valid?.should be_true
411
- u.save.should be_true
412
- UserWithValidation.first.read_attribute(:phone).should eq '+7 (495) 121-11-11'
413
- UserWithValidation.first.phone.should eq '+7 (495) 121-11-11'
414
- end
415
-
416
- it 'should pass validation when validate is on and phone is valid' do
417
- u = UserWithValidation.new(phone: '7 495 121 11 11')
418
- u.valid?.should be_true
419
- u.save.should be_true
420
- UserWithValidation.first.read_attribute(:phone).should eq '+7 (495) 121-11-11'
421
- UserWithValidation.first.phone.should eq '+7 (495) 121-11-11'
422
- end
423
-
424
- it 'should pass validation when validate is on and phone is valid' do
425
- u = UserWithValidation.new(phone: '8495 121 11 11')
426
- u.valid?.should be_true
427
- u.save.should be_true
428
- UserWithValidation.first.read_attribute(:phone).should eq '+7 (495) 121-11-11'
429
- UserWithValidation.first.phone.should eq '+7 (495) 121-11-11'
430
- end
431
-
432
- it 'should pass validation when validate is on and phone is valid' do
433
- u = UserWithValidation.new(phone: '7495123-12-12 доб 123')
434
-
435
- u.valid?.should be_true
436
- u.save.should be_true
437
- UserWithValidation.first.read_attribute(:phone).should eq '+7 (495) 123-12-12 доб 123'
438
- UserWithValidation.first.phone.should eq '+7 (495) 123-12-12 доб 123'
439
- end
440
-
441
- it 'should pass validation when phone is valid (unknown city code)' do
442
- u = UserWithAnyCode.new(phone: '7701123-12-12 доб 123')
443
- u.valid?.should be_true
444
- u.save.should be_true
445
- UserWithAnyCode.first.read_attribute(:phone).should eq '+7 (701) 123-12-12 доб 123'
446
- UserWithAnyCode.first.phone.should eq '+7 (701) 123-12-12 доб 123'
447
- end
448
-
449
- it 'should pass validation when validate is on and phone is valid (unknown city code)' do
450
- u = UserWithAnyCode.new(phone: '701123-12-12 доб 123')
451
- u.valid?.should be_true
452
- u.save.should be_true
453
- UserWithAnyCode.first.read_attribute(:phone).should eq '+7 (701) 123-12-12 доб 123'
454
- UserWithAnyCode.first.phone.should eq '+7 (701) 123-12-12 доб 123'
455
- end
456
-
457
- it 'should pass validation when validate is on and phone is valid' do
458
- u = UserWithValidation.new(phone: '8 495 121 11 11')
459
- u.valid?.should be_true
460
- u.save.should be_true
461
- UserWithValidation.first.read_attribute(:phone).should eq '+7 (495) 121-11-11'
462
- UserWithValidation.first.phone.should eq '+7 (495) 121-11-11'
463
- end
464
-
465
- it 'should pass validation when validate is on and phone is valid' do
466
- u = UserWithValidation.new(phone: '+7 495 121 11 11')
467
- u.valid?.should be_true
468
- u.save.should be_true
469
- UserWithValidation.first.read_attribute(:phone).should eq '+7 (495) 121-11-11'
470
- UserWithValidation.first.phone.should eq '+7 (495) 121-11-11'
471
- end
472
-
473
- it 'should pass validation when validate is on and phone is valid' do
474
- u = UserWithValidation.new(phone: '495 121 11 11 доб 123')
475
- u.valid?.should be_true
476
- u.save.should be_true
477
- UserWithValidation.first.read_attribute(:phone).should eq '+7 (495) 121-11-11 доб 123'
478
- UserWithValidation.first.phone.should eq '+7 (495) 121-11-11 доб 123'
479
- end
480
-
481
- it 'should fail validation when validate is on and city is not in allowed_cities' do
482
- u = UserWithValidation.new(phone: '906 121 11 11')
483
- u.valid?.should be_false
484
- u.save.should be_false
485
- u.errors.messages.should eq({:phone =>["Неверный телефонный номер"]})
486
- end
487
-
488
- it 'should pass validation when validate is off and phone is invalid' do
489
- u = UserWithoutValidation.new(phone: '123')
490
-
491
- u.valid?.should be_true
492
- u.save.should be_true
493
- end
494
-
495
- it 'should pass validation when validate is off and phone is valid' do
496
- u = UserWithoutValidation.new(phone: '495 121 11 11')
497
-
498
- u.valid?.should be_true
499
- u.save.should be_true
500
- end
501
-
502
- it 'should pass validation when validate is off and city is not in allowed_cities' do
503
- u = UserWithoutValidation.new(phone: '906 121 11 11')
504
-
505
- u.valid?.should be_true
506
- u.save.should be_true
507
- end
508
-
509
- it 'should pass validation when required but not validated' do
510
- u = UserWithRequired.new(phone: '906 121 11 11')
511
-
512
- u.valid?.should be_true
513
- u.save.should be_true
514
- end
515
-
516
- it 'should pass validation when required but not validated' do
517
- u = UserWithRequired.new(phone: '11 11')
518
-
519
- u.valid?.should be_true
520
- u.save.should be_true
521
- end
522
-
523
- it 'should fail validation when required and not present' do
524
- u = UserWithRequired.new()
525
- u.valid?.should be_false
526
- u.save.should be_false
527
-
528
- u = UserWithRequired.new(phone: '')
529
- u.valid?.should be_false
530
- u.save.should be_false
531
- end
532
- end
533
-
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe RussianPhone do
6
+ describe 'clean' do
7
+ it 'should remove everything except numbers from string' do
8
+ RussianPhone::Number.clean('+7 (906) 111-11-11').should eq '79061111111'
9
+ end
10
+ end
11
+
12
+ describe 'extract city' do
13
+ it 'should extract city from number correctly' do
14
+ RussianPhone::Number.extract('(906) 111-11-11', 7, 3).should eq ['906', '1111111']
15
+ RussianPhone::Number.extract('(906) 111-11-11', 6, 4).should eq ['9061', '111111']
16
+ RussianPhone::Number.extract('(906) 111-11-11', 5, 5).should eq ['90611', '11111']
17
+ end
18
+ end
19
+
20
+ describe 'when parsing' do
21
+ it 'should parse 8-800-111-11-11 number' do
22
+ phone = RussianPhone::Number.new('8-800-111-11-11', default_country: 7)
23
+ phone.should_not be_nil
24
+ phone.valid?.should be_true
25
+
26
+ phone.cell?.should be_false
27
+ phone.free?.should be_true
28
+
29
+ phone.city.should eq '800'
30
+ phone.country.should eq '7'
31
+ phone.subscriber.should eq '1111111'
32
+ phone.full.should eq '8-800-111-11-11'
33
+ end
34
+
35
+ it 'should parse (906) 111-11-11 number' do
36
+ phone = RussianPhone::Number.new('(906) 111-11-11', default_country: 7)
37
+ phone.should_not be_nil
38
+ phone.valid?.should be_true
39
+
40
+ phone.cell?.should be_true
41
+ phone.free?.should be_false
42
+
43
+ phone.city.should eq '906'
44
+ phone.country.should eq '7'
45
+ phone.subscriber.should eq '1111111'
46
+ phone.full.should eq '+7 (906) 111-11-11'
47
+ end
48
+
49
+ it 'should parse 111-11-11 number with default city' do
50
+ phone = RussianPhone::Number.new('111-11-11', default_country: 7, default_city: 495)
51
+ phone.should_not be_nil
52
+ phone.valid?.should be_true
53
+
54
+ phone.cell?.should be_false
55
+ phone.free?.should be_false
56
+
57
+ phone.city.should eq '495'
58
+ phone.country.should eq '7'
59
+ phone.subscriber.should eq '1111111'
60
+ phone.full.should eq '+7 (495) 111-11-11'
61
+ end
62
+
63
+ it 'should not parse 111-11-11 number without default city' do
64
+ phone = RussianPhone::Number.new('111-11-11', default_country: 7)
65
+ phone.valid?.should be_false
66
+ end
67
+
68
+ it 'should parse 1111111 number with default city' do
69
+ phone = RussianPhone::Number.new('1111111', default_country: 7, default_city: 495)
70
+ phone.should_not be_nil
71
+ phone.valid?.should be_true
72
+
73
+ phone.cell?.should be_false
74
+ phone.free?.should be_false
75
+
76
+ phone.city.should eq '495'
77
+ phone.country.should eq '7'
78
+ phone.subscriber.should eq '1111111'
79
+ phone.full.should eq '+7 (495) 111-11-11'
80
+ end
81
+
82
+ it 'should parse +7 (495) 111-11-11 number' do
83
+ phone = RussianPhone::Number.new('+7 (495) 111-11-11')
84
+ phone.should_not be_nil
85
+ phone.valid?.should be_true
86
+
87
+ phone.cell?.should be_false
88
+ phone.free?.should be_false
89
+
90
+ phone.city.should eq '495'
91
+ phone.country.should eq '7'
92
+ phone.subscriber.should eq '1111111'
93
+ phone.full.should eq '+7 (495) 111-11-11'
94
+ end
95
+
96
+ it 'should parse +7 (495) 111-11-11 number' do
97
+ phone = RussianPhone::Number.new('+7 (495) 111-11-11')
98
+ phone.should_not be_nil
99
+ phone.valid?.should be_true
100
+
101
+ phone.cell?.should be_false
102
+ phone.free?.should be_false
103
+
104
+ phone.city.should eq '495'
105
+ phone.country.should eq '7'
106
+ phone.subscriber.should eq '1111111'
107
+ phone.full.should eq '+7 (495) 111-11-11'
108
+ end
109
+
110
+ it 'should parse 8(4912)12-34-56 number' do
111
+ phone = RussianPhone::Number.new('+7 (4912) 12-34-56', default_country: 7)
112
+ phone.should_not be_nil
113
+ phone.valid?.should be_true
114
+
115
+ phone.cell?.should be_false
116
+ phone.free?.should be_false
117
+
118
+ phone.city.should eq '4912'
119
+ phone.country.should eq '7'
120
+ phone.subscriber.should eq '123456'
121
+ phone.full.should eq '+7 (4912) 12-34-56'
122
+ end
123
+
124
+ it 'should parse 2-34-56 number whith default city' do
125
+ phone = RussianPhone::Number.new('2-34-56', default_country: 7, default_city: 87252)
126
+ phone.should_not be_nil
127
+ phone.valid?.should be_true
128
+
129
+ phone.cell?.should be_false
130
+ phone.free?.should be_false
131
+
132
+ phone.city.should eq '87252'
133
+ phone.country.should eq '7'
134
+ phone.subscriber.should eq '23456'
135
+ phone.full.should eq '+7 (87252) 2-34-56'
136
+ end
137
+
138
+ it 'should parse 12-34-56 number whith default city' do
139
+ phone = RussianPhone::Number.new('12-34-56', default_country: 7, default_city: 4912)
140
+ phone.should_not be_nil
141
+ phone.valid?.should be_true
142
+
143
+ phone.cell?.should be_false
144
+ phone.free?.should be_false
145
+
146
+ phone.city.should eq '4912'
147
+ phone.country.should eq '7'
148
+ phone.subscriber.should eq '123456'
149
+ phone.full.should eq '+7 (4912) 12-34-56'
150
+ end
151
+
152
+ it 'should parse 8(34356)5-67-89 number correctly' do
153
+ # это номер в Екатеринбурге, с неправильно указанным кодом города
154
+
155
+ phone = RussianPhone::Number.new('8(34356)5-67-89', default_country: 7)
156
+ phone.should_not be_nil
157
+ phone.valid?.should be_true
158
+
159
+ phone.cell?.should be_false
160
+ phone.free?.should be_false
161
+
162
+ phone.city.should eq '343'
163
+ phone.country.should eq '7'
164
+ phone.subscriber.should eq '5656789'
165
+ phone.full.should eq '+7 (343) 565-67-89'
166
+ end
167
+
168
+ it 'should parse 7 (906) 123-45-67 number correctly' do
169
+ # это номер в Екатеринбурге, с неправильно указанным кодом города
170
+
171
+ phone = RussianPhone::Number.new('7 (906) 123-45-67', default_country: 7)
172
+ phone.should_not be_nil
173
+ phone.valid?.should be_true
174
+
175
+ phone.cell?.should be_true
176
+ phone.free?.should be_false
177
+ phone.extra.should eq ''
178
+ phone.city.should eq '906'
179
+ phone.country.should eq '7'
180
+ phone.subscriber.should eq '1234567'
181
+ phone.full.should eq '+7 (906) 123-45-67'
182
+ end
183
+
184
+ it 'should handle phones with extra stuff [8 (906) 111-11-11 доб. 123]' do
185
+ phone = RussianPhone::Number.new('8 (906) 111-11-11 доб. 123', default_country: 7)
186
+ phone.should_not be_nil
187
+ phone.valid?.should be_true
188
+
189
+ phone.cell?.should be_true
190
+ phone.free?.should be_false
191
+
192
+ phone.city.should eq '906'
193
+ phone.country.should eq '7'
194
+ phone.subscriber.should eq '1111111'
195
+ phone.full.should eq '+7 (906) 111-11-11 доб. 123'
196
+ end
197
+
198
+ it 'should handle phones with extra stuff [8 (906) 111-11-11 д. 123]' do
199
+ phone = RussianPhone::Number.new('8 (906) 111-11-11 д. 123', default_country: 7)
200
+ phone.should_not be_nil
201
+ phone.valid?.should be_true
202
+
203
+ phone.cell?.should be_true
204
+ phone.free?.should be_false
205
+
206
+ phone.city.should eq '906'
207
+ phone.country.should eq '7'
208
+ phone.subscriber.should eq '1111111'
209
+ phone.full.should eq '+7 (906) 111-11-11 д. 123'
210
+ end
211
+
212
+ it 'should handle phones with extra stuff [89061010101 д. 123]' do
213
+ phone = RussianPhone::Number.new('89061010101 д. 123', default_country: 7)
214
+ phone.should_not be_nil
215
+ phone.valid?.should be_true
216
+
217
+ phone.cell?.should be_true
218
+ phone.free?.should be_false
219
+
220
+ phone.city.should eq '906'
221
+ phone.country.should eq '7'
222
+ phone.subscriber.should eq '1010101'
223
+ phone.full.should eq '+7 (906) 101-01-01 д. 123'
224
+ end
225
+
226
+ it 'should handle phones with extra stuff [89061010101-д. 123]' do
227
+ phone = RussianPhone::Number.new('89061010101-д. 123', default_country: 7)
228
+ phone.should_not be_nil
229
+ phone.valid?.should be_true
230
+
231
+ phone.cell?.should be_true
232
+ phone.free?.should be_false
233
+
234
+ phone.city.should eq '906'
235
+ phone.country.should eq '7'
236
+ phone.subscriber.should eq '1010101'
237
+ phone.full.should eq '+7 (906) 101-01-01 -д. 123'
238
+ end
239
+
240
+ it 'should handle phones with unknown codes [8 (533) 111-11-11]' do
241
+ phone = RussianPhone::Number.new('8 (533) 111-11-11', default_country: 7)
242
+ phone.should_not be_nil
243
+ phone.valid?.should be_true
244
+
245
+ phone.cell?.should be_false
246
+ phone.free?.should be_false
247
+
248
+ phone.city.should eq '533'
249
+ phone.country.should eq '7'
250
+ phone.subscriber.should eq '1111111'
251
+ phone.full.should eq '+7 (533) 111-11-11'
252
+ end
253
+
254
+ tests = {
255
+ '+79261234567' => [7, 926, 1234567],
256
+ '89261234567' => [7, 926, 1234567],
257
+ '79261234567' => [7, 926, 1234567],
258
+ '+7 926 123 45 67' => [7, 926, 1234567],
259
+ '8(926)123-45-67' => [7, 926, 1234567],
260
+ '8 (926) 12-3-45-67' => [7, 926, 1234567],
261
+ '9261234567' => [7, 926, 1234567],
262
+ '(495)1234567' => [7, 495, 1234567],
263
+ '(495)123 45 67' => [7, 495, 1234567],
264
+ '8-926-123-45-67' => [7, 926, 1234567],
265
+ '8 927 1234 234' => [7, 927, 1234234],
266
+ '8 927 12 12 234' => [7, 927, 1212234],
267
+ '8 927 1 2 3 4 2 3 4' => [7, 927, 1234234],
268
+ '8 927 12 34 234' => [7, 927, 1234234],
269
+ '8 927 12 342 34' => [7, 927, 1234234],
270
+ '8 927 123-4-234' => [7, 927, 1234234],
271
+ '8 (927) 12 342 34' => [7, 927, 1234234],
272
+ '8-(927)-12-342-34' => [7, 927, 1234234],
273
+ '+7 927 1234 234' => [7, 927, 1234234],
274
+ '+7 927 12 12 234' => [7, 927, 1212234],
275
+ '+7 927 1 2 3 4 2 3 4' => [7, 927, 1234234],
276
+ '+7 927 12 34 234' => [7, 927, 1234234],
277
+ '+7 927 12 342 34' => [7, 927, 1234234],
278
+ '+7 927 123-4-234' => [7, 927, 1234234],
279
+ '+7 (927) 12 342 34' => [7, 927, 1234234],
280
+ '+7-(927)-12-342-34' => [7, 927, 1234234],
281
+ '7 927 1234 234' => [7, 927, 1234234],
282
+ '7 927 12 12 234' => [7, 927, 1212234],
283
+ '7 927 1 2 3 4 2 3 4' => [7, 927, 1234234],
284
+ '7 927 12 34 234' => [7, 927, 1234234],
285
+ '7 927 12 342 34' => [7, 927, 1234234],
286
+ '7 927 123-4-234' => [7, 927, 1234234],
287
+ '7 (927) 12 342 34' => [7, 927, 1234234],
288
+ '7-(927)-12-342-34' => [7, 927, 1234234],
289
+ '7 84543 123 12' => [7, 84543, 12312],
290
+ '78454312312' => [7, 84543, 12312],
291
+ '88454312312' => [7, 84543, 12312],
292
+ }
293
+
294
+ tests.each_pair do |str, result|
295
+ it "should parse #{str}" do
296
+ phone = RussianPhone::Number.new(str, default_country: 7)
297
+ phone.should_not be_nil
298
+ phone.valid?.should be_true
299
+
300
+ if %w(927 926).include? result[1].to_s
301
+ phone.cell?.should be_true
302
+ else
303
+ phone.cell?.should be_false
304
+ end
305
+
306
+ phone.free?.should be_false
307
+
308
+ phone.country.should eq result[0].to_s
309
+ phone.city.should eq result[1].to_s
310
+ phone.subscriber.should eq result[2].to_s
311
+ phone.clean.should eq "#{result[0]}#{result[1]}#{result[2]}"
312
+ end
313
+ end
314
+
315
+ bad_phones = [
316
+ '123 123',
317
+ '000000',
318
+ '123',
319
+ 'пыщ пыщ ололо я водитель НЛО',
320
+ '11 1 11 1'
321
+ ]
322
+
323
+ bad_phones.each do |phone|
324
+ it "should not parse #{phone}" do
325
+ phone = RussianPhone::Number.new(phone, default_country: 7)
326
+ phone.valid?.should be_false
327
+ end
328
+ end
329
+ end
330
+
331
+ describe 'when using ::Field' do
332
+ it 'should serialize and deserialize correctly' do
333
+ RussianPhone::Number.new('8 (906) 111-11-11 д. 123').mongoize.should eq '+7 (906) 111-11-11 д. 123'
334
+
335
+ RussianPhone::Number.new('495 111 11 11').mongoize.should eq '+7 (495) 111-11-11'
336
+
337
+ RussianPhone::Number.demongoize('+7 (495) 111-11-11').mongoize.should eq '+7 (495) 111-11-11'
338
+
339
+ RussianPhone::Field.mongoize(RussianPhone::Number.new('495 111 11 11')).should eq '+7 (495) 111-11-11'
340
+ RussianPhone::Field.evolve(RussianPhone::Number.new('495 111 11 11')).should eq '+7 (495) 111-11-11'
341
+ RussianPhone::Field.evolve(RussianPhone::Number.new('495 111 11 11 доб 123')).should eq '+7 (495) 111-11-11 доб 123'
342
+ end
343
+ end
344
+
345
+ describe 'when storing with mongoid' do
346
+ it 'should parse, store and retrieve numbers correctly' do
347
+ u = User.new(name: 'test', phone: '906 111 11 11')
348
+ u.save.should be_true
349
+ u = User.first
350
+
351
+ u.phone.should eq '+7 (906) 111-11-11'
352
+ u.phone.cell?.should be_true
353
+ u.phone.free?.should be_false
354
+
355
+ u.phone.clean.should eq '79061111111'
356
+
357
+ u.phone.country.should eq '7'
358
+ u.phone.city.should eq '906'
359
+ u.phone.subscriber.should eq '1111111'
360
+ end
361
+
362
+ it 'should respect field options' do
363
+ u = User.create(name: 'test', phone_in_495: '111 11 11', phone_in_812: '222 2222')
364
+ u.save.should be_true
365
+
366
+ u = User.first
367
+
368
+ u.phone_in_495.class.name.should eq 'RussianPhone::Number'
369
+ u.phone_in_495.should eq '+7 (495) 111-11-11'
370
+ u.phone_in_495.cell?.should be_false
371
+ u.phone_in_495.free?.should be_false
372
+
373
+ u.phone_in_495.country.should eq '7'
374
+ u.phone_in_495.city.should eq '495'
375
+ u.phone_in_495.subscriber.should eq '1111111'
376
+
377
+ u.phone_in_495.clean.should eq '74951111111'
378
+ u.phone_in_495.full.should eq '+7 (495) 111-11-11'
379
+
380
+ u.phone_in_812.class.name.should eq 'RussianPhone::Number'
381
+ u.phone_in_812.should eq '+7 (812) 222-22-22'
382
+ u.phone_in_812.cell?.should be_false
383
+ u.phone_in_812.free?.should be_false
384
+
385
+ u.phone_in_812.country.should eq '7'
386
+ u.phone_in_812.city.should eq '812'
387
+ u.phone_in_812.subscriber.should eq '2222222'
388
+
389
+ u.phone_in_812.clean.should eq '78122222222'
390
+ u.phone_in_812.full.should eq '+7 (812) 222-22-22'
391
+ end
392
+
393
+ it 'should fail validation when validate is on and phone is invalid' do
394
+ u = UserWithValidation.new(phone: '123')
395
+ u.valid?.should be_false
396
+ u.save.should be_false
397
+ u.errors.messages.should eq({:phone =>["Неверный телефонный номер"]})
398
+ end
399
+
400
+ it 'should pass validation when validate is on and phone is valid' do
401
+ u = UserWithValidation.new(phone: '495 121 11 11')
402
+ u.valid?.should be_true
403
+ u.save.should be_true
404
+ UserWithValidation.first.read_attribute(:phone).should eq '+7 (495) 121-11-11'
405
+ UserWithValidation.first.phone.should eq '+7 (495) 121-11-11'
406
+ end
407
+
408
+ it 'should pass validation when validate is on and phone is valid' do
409
+ u = UserWithValidation.new(phone: '7495 121 11 11')
410
+ u.valid?.should be_true
411
+ u.save.should be_true
412
+ UserWithValidation.first.read_attribute(:phone).should eq '+7 (495) 121-11-11'
413
+ UserWithValidation.first.phone.should eq '+7 (495) 121-11-11'
414
+ end
415
+
416
+ it 'should pass validation when validate is on and phone is valid' do
417
+ u = UserWithValidation.new(phone: '7 495 121 11 11')
418
+ u.valid?.should be_true
419
+ u.save.should be_true
420
+ UserWithValidation.first.read_attribute(:phone).should eq '+7 (495) 121-11-11'
421
+ UserWithValidation.first.phone.should eq '+7 (495) 121-11-11'
422
+ end
423
+
424
+ it 'should pass validation when validate is on and phone is valid' do
425
+ u = UserWithValidation.new(phone: '8495 121 11 11')
426
+ u.valid?.should be_true
427
+ u.save.should be_true
428
+ UserWithValidation.first.read_attribute(:phone).should eq '+7 (495) 121-11-11'
429
+ UserWithValidation.first.phone.should eq '+7 (495) 121-11-11'
430
+ end
431
+
432
+ it 'should pass validation when validate is on and phone is valid' do
433
+ u = UserWithValidation.new(phone: '7495123-12-12 доб 123')
434
+
435
+ u.valid?.should be_true
436
+ u.save.should be_true
437
+ UserWithValidation.first.read_attribute(:phone).should eq '+7 (495) 123-12-12 доб 123'
438
+ UserWithValidation.first.phone.should eq '+7 (495) 123-12-12 доб 123'
439
+ end
440
+
441
+ it 'should pass validation when phone is valid (unknown city code)' do
442
+ u = UserWithAnyCode.new(phone: '7701123-12-12 доб 123')
443
+ u.valid?.should be_true
444
+ u.save.should be_true
445
+ UserWithAnyCode.first.read_attribute(:phone).should eq '+7 (701) 123-12-12 доб 123'
446
+ UserWithAnyCode.first.phone.should eq '+7 (701) 123-12-12 доб 123'
447
+ end
448
+
449
+ it 'should pass validation when validate is on and phone is valid (unknown city code)' do
450
+ u = UserWithAnyCode.new(phone: '701123-12-12 доб 123')
451
+ u.valid?.should be_true
452
+ u.save.should be_true
453
+ UserWithAnyCode.first.read_attribute(:phone).should eq '+7 (701) 123-12-12 доб 123'
454
+ UserWithAnyCode.first.phone.should eq '+7 (701) 123-12-12 доб 123'
455
+ end
456
+
457
+ it 'should pass validation when validate is on and phone is valid' do
458
+ u = UserWithValidation.new(phone: '8 495 121 11 11')
459
+ u.valid?.should be_true
460
+ u.save.should be_true
461
+ UserWithValidation.first.read_attribute(:phone).should eq '+7 (495) 121-11-11'
462
+ UserWithValidation.first.phone.should eq '+7 (495) 121-11-11'
463
+ end
464
+
465
+ it 'should pass validation when validate is on and phone is valid' do
466
+ u = UserWithValidation.new(phone: '+7 495 121 11 11')
467
+ u.valid?.should be_true
468
+ u.save.should be_true
469
+ UserWithValidation.first.read_attribute(:phone).should eq '+7 (495) 121-11-11'
470
+ UserWithValidation.first.phone.should eq '+7 (495) 121-11-11'
471
+ end
472
+
473
+ it 'should pass validation when validate is on and phone is valid' do
474
+ u = UserWithValidation.new(phone: '495 121 11 11 доб 123')
475
+ u.valid?.should be_true
476
+ u.save.should be_true
477
+ UserWithValidation.first.read_attribute(:phone).should eq '+7 (495) 121-11-11 доб 123'
478
+ UserWithValidation.first.phone.should eq '+7 (495) 121-11-11 доб 123'
479
+ end
480
+
481
+ it 'should fail validation when validate is on and city is not in allowed_cities' do
482
+ u = UserWithValidation.new(phone: '906 121 11 11')
483
+ u.valid?.should be_false
484
+ u.save.should be_false
485
+ u.errors.messages.should eq({:phone =>["Неверный телефонный номер"]})
486
+ end
487
+
488
+ it 'should pass validation when validate is off and phone is invalid' do
489
+ u = UserWithoutValidation.new(phone: '123')
490
+
491
+ u.valid?.should be_true
492
+ u.save.should be_true
493
+ end
494
+
495
+ it 'should pass validation when validate is off and phone is valid' do
496
+ u = UserWithoutValidation.new(phone: '495 121 11 11')
497
+
498
+ u.valid?.should be_true
499
+ u.save.should be_true
500
+ end
501
+
502
+ it 'should pass validation when validate is off and city is not in allowed_cities' do
503
+ u = UserWithoutValidation.new(phone: '906 121 11 11')
504
+
505
+ u.valid?.should be_true
506
+ u.save.should be_true
507
+ end
508
+
509
+ it 'should pass validation when required but not validated' do
510
+ u = UserWithRequired.new(phone: '906 121 11 11')
511
+
512
+ u.valid?.should be_true
513
+ u.save.should be_true
514
+ end
515
+
516
+ it 'should pass validation when required but not validated' do
517
+ u = UserWithRequired.new(phone: '11 11')
518
+
519
+ u.valid?.should be_true
520
+ u.save.should be_true
521
+ end
522
+
523
+ it 'should fail validation when required and not present' do
524
+ u = UserWithRequired.new()
525
+ u.valid?.should be_false
526
+ u.save.should be_false
527
+
528
+ u = UserWithRequired.new(phone: '')
529
+ u.valid?.should be_false
530
+ u.save.should be_false
531
+ end
532
+ end
533
+
534
534
  end