cogi_phony 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.MD +92 -0
- data/Rakefile +5 -0
- data/lib/cogi_phony.rb +268 -0
- data/lib/cogi_phony/error.rb +7 -0
- data/lib/cogi_phony/version.rb +3 -0
- data/lib/data/country_codes.yaml +970 -0
- data/lib/data/mobile_networks.yaml +16 -0
- data/spec/cogi_phony_spec.rb +535 -0
- data/spec/spec_helper.rb +1 -0
- metadata +118 -0
@@ -0,0 +1,16 @@
|
|
1
|
+
#
|
2
|
+
# List of mobile networks in Vietnam.
|
3
|
+
#
|
4
|
+
# Reference from:
|
5
|
+
# - http://netviettel.com/dau-so-dien-thoai-co-dinh-di-dong-cua-viettel/
|
6
|
+
# - http://www.3gvinaphone.pro.vn/2016/03/cac-dau-so-cua-vinaphone.html
|
7
|
+
# - http://3gmobifone.vn/cac-dau-so-mang-mobifone.html
|
8
|
+
# - http://www.simphongthuy.com.vn/tin-tuc-moi-Danh-sach-cac-dau-so-nha-mang-Gmobile-cid459.html
|
9
|
+
|
10
|
+
---
|
11
|
+
vi:
|
12
|
+
viettel: '96|97|98|162|163|164|165|166|167|168|169'
|
13
|
+
vinaphone: '88|91|94|123|124|125|127|129'
|
14
|
+
mobifone: '89|90|93|121|122|124|126|128'
|
15
|
+
vietnamobile: '92|186|188'
|
16
|
+
gmobile: '99|199'
|
@@ -0,0 +1,535 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CogiPhony do
|
4
|
+
subject { CogiPhony.new }
|
5
|
+
|
6
|
+
describe '#hi' do
|
7
|
+
it 'return nil' do
|
8
|
+
expect(CogiPhony.hi).to eq(nil)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#country_codes_hash' do
|
13
|
+
let(:country_codes) { CogiPhony.country_codes_hash }
|
14
|
+
|
15
|
+
it 'return a hash' do
|
16
|
+
expect(country_codes).to be_a(Hash)
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'has countries key' do
|
20
|
+
expect(country_codes['countries']).not_to be_nil
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'return country code 84' do
|
24
|
+
expect(country_codes['countries']['VN']['country_code']).to eq('84')
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'return country name Vietnam' do
|
28
|
+
expect(country_codes['countries']['VN']['name']).to eq('Viet Nam')
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'return nil if invalid iso code' do
|
32
|
+
expect(country_codes['countries']['ZZ']).to be_nil
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#mobile_networks' do
|
37
|
+
let(:mobile_networks) { CogiPhony.mobile_networks_hash }
|
38
|
+
|
39
|
+
it 'return a hash' do
|
40
|
+
expect(mobile_networks).to be_a(Hash)
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'return Viettel network' do
|
44
|
+
expect(mobile_networks['vi']['viettel']).to be_a(String)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe '#gmobile?' do
|
49
|
+
context 'mobile phone begin with 99' do
|
50
|
+
it 'length equal 10 is valid' do
|
51
|
+
expect(CogiPhony.gmobile?('0994567890')).not_to be_nil
|
52
|
+
expect(CogiPhony.gmobile?('84994567890')).not_to be_nil
|
53
|
+
expect(CogiPhony.gmobile?('+84994567890')).not_to be_nil
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'length less than 10 is invalid' do
|
57
|
+
expect(CogiPhony.gmobile?('099456789')).to be_nil
|
58
|
+
expect(CogiPhony.gmobile?('8499456789')).to be_nil
|
59
|
+
expect(CogiPhony.gmobile?('+8499456789')).to be_nil
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'length more than 10 is invalid' do
|
63
|
+
expect(CogiPhony.gmobile?('09945678901')).to be_nil
|
64
|
+
expect(CogiPhony.gmobile?('849945678901')).to be_nil
|
65
|
+
expect(CogiPhony.gmobile?('+849945678901')).to be_nil
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context 'mobile phone begin with 199' do
|
70
|
+
it 'length equal 11 is valid' do
|
71
|
+
expect(CogiPhony.gmobile?('01994567890')).not_to be_nil
|
72
|
+
expect(CogiPhony.gmobile?('841994567890')).not_to be_nil
|
73
|
+
expect(CogiPhony.gmobile?('+841994567890')).not_to be_nil
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'length less than 11 is invalid' do
|
77
|
+
expect(CogiPhony.gmobile?('0199456789')).to be_nil
|
78
|
+
expect(CogiPhony.gmobile?('84199456789')).to be_nil
|
79
|
+
expect(CogiPhony.gmobile?('+84199456789')).to be_nil
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'length more than 11 is invalid' do
|
83
|
+
expect(CogiPhony.gmobile?('019945678901')).to be_nil
|
84
|
+
expect(CogiPhony.gmobile?('8419945678901')).to be_nil
|
85
|
+
expect(CogiPhony.gmobile?('+8419945678901')).to be_nil
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe '#vietnamobile?' do
|
91
|
+
context 'mobile phone begin with 92' do
|
92
|
+
it 'length equal 10 is valid' do
|
93
|
+
expect(CogiPhony.vietnamobile?('0924567890')).not_to be_nil
|
94
|
+
expect(CogiPhony.vietnamobile?('84924567890')).not_to be_nil
|
95
|
+
expect(CogiPhony.vietnamobile?('+84924567890')).not_to be_nil
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'length less than 10 is invalid' do
|
99
|
+
expect(CogiPhony.vietnamobile?('092456789')).to be_nil
|
100
|
+
expect(CogiPhony.vietnamobile?('8492456789')).to be_nil
|
101
|
+
expect(CogiPhony.vietnamobile?('+8492456789')).to be_nil
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'length more than 10 is invalid' do
|
105
|
+
expect(CogiPhony.vietnamobile?('09245678901')).to be_nil
|
106
|
+
expect(CogiPhony.vietnamobile?('849245678901')).to be_nil
|
107
|
+
expect(CogiPhony.vietnamobile?('+849245678901')).to be_nil
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
context 'mobile phone begin with 186' do
|
112
|
+
it 'length equal 11 is valid' do
|
113
|
+
expect(CogiPhony.vietnamobile?('01864567890')).not_to be_nil
|
114
|
+
expect(CogiPhony.vietnamobile?('841864567890')).not_to be_nil
|
115
|
+
expect(CogiPhony.vietnamobile?('+841864567890')).not_to be_nil
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'length less than 11 is invalid' do
|
119
|
+
expect(CogiPhony.vietnamobile?('0186456789')).to be_nil
|
120
|
+
expect(CogiPhony.vietnamobile?('84186456789')).to be_nil
|
121
|
+
expect(CogiPhony.vietnamobile?('+84186456789')).to be_nil
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'length more than 11 is invalid' do
|
125
|
+
expect(CogiPhony.vietnamobile?('018645678901')).to be_nil
|
126
|
+
expect(CogiPhony.vietnamobile?('8418645678901')).to be_nil
|
127
|
+
expect(CogiPhony.vietnamobile?('+8418645678901')).to be_nil
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
context 'mobile phone begin with 188' do
|
132
|
+
it 'length equal 11 is valid' do
|
133
|
+
expect(CogiPhony.vietnamobile?('01884567890')).not_to be_nil
|
134
|
+
expect(CogiPhony.vietnamobile?('841884567890')).not_to be_nil
|
135
|
+
expect(CogiPhony.vietnamobile?('+841884567890')).not_to be_nil
|
136
|
+
end
|
137
|
+
|
138
|
+
it 'length less than 11 is invalid' do
|
139
|
+
expect(CogiPhony.vietnamobile?('0188456789')).to be_nil
|
140
|
+
expect(CogiPhony.vietnamobile?('84188456789')).to be_nil
|
141
|
+
expect(CogiPhony.vietnamobile?('+84188456789')).to be_nil
|
142
|
+
end
|
143
|
+
|
144
|
+
it 'length more than 11 is invalid' do
|
145
|
+
expect(CogiPhony.vietnamobile?('018845678901')).to be_nil
|
146
|
+
expect(CogiPhony.vietnamobile?('8418845678901')).to be_nil
|
147
|
+
expect(CogiPhony.vietnamobile?('+8418845678901')).to be_nil
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
describe '#mobifone?' do
|
153
|
+
context 'mobile phone begin with 90' do
|
154
|
+
it 'length equal 10 is valid' do
|
155
|
+
expect(CogiPhony.mobifone?('0904567890')).not_to be_nil
|
156
|
+
expect(CogiPhony.mobifone?('84904567890')).not_to be_nil
|
157
|
+
expect(CogiPhony.mobifone?('+84904567890')).not_to be_nil
|
158
|
+
end
|
159
|
+
|
160
|
+
it 'length less than 10 is invalid' do
|
161
|
+
expect(CogiPhony.mobifone?('090456789')).to be_nil
|
162
|
+
expect(CogiPhony.mobifone?('8490456789')).to be_nil
|
163
|
+
expect(CogiPhony.mobifone?('+8490456789')).to be_nil
|
164
|
+
end
|
165
|
+
|
166
|
+
it 'length more than 10 is invalid' do
|
167
|
+
expect(CogiPhony.mobifone?('09045678901')).to be_nil
|
168
|
+
expect(CogiPhony.mobifone?('849045678901')).to be_nil
|
169
|
+
expect(CogiPhony.mobifone?('+849045678901')).to be_nil
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
context 'mobile phone begin with 121' do
|
174
|
+
it 'length equal 11 is valid' do
|
175
|
+
expect(CogiPhony.mobifone?('01214567890')).not_to be_nil
|
176
|
+
expect(CogiPhony.mobifone?('841214567890')).not_to be_nil
|
177
|
+
expect(CogiPhony.mobifone?('+841214567890')).not_to be_nil
|
178
|
+
end
|
179
|
+
|
180
|
+
it 'length less than 11 is invalid' do
|
181
|
+
expect(CogiPhony.mobifone?('0121456789')).to be_nil
|
182
|
+
expect(CogiPhony.mobifone?('84121456789')).to be_nil
|
183
|
+
expect(CogiPhony.mobifone?('+84121456789')).to be_nil
|
184
|
+
end
|
185
|
+
|
186
|
+
it 'length more than 11 is invalid' do
|
187
|
+
expect(CogiPhony.mobifone?('012145678901')).to be_nil
|
188
|
+
expect(CogiPhony.mobifone?('8412145678901')).to be_nil
|
189
|
+
expect(CogiPhony.mobifone?('+8412145678901')).to be_nil
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
describe '#vinaphone?' do
|
195
|
+
context 'mobile phone begin with 91' do
|
196
|
+
it 'length equal 10 is valid' do
|
197
|
+
expect(CogiPhony.vinaphone?('0914567890')).not_to be_nil
|
198
|
+
expect(CogiPhony.vinaphone?('84914567890')).not_to be_nil
|
199
|
+
expect(CogiPhony.vinaphone?('+84914567890')).not_to be_nil
|
200
|
+
end
|
201
|
+
|
202
|
+
it 'length less than 10 is invalid' do
|
203
|
+
expect(CogiPhony.vinaphone?('091456789')).to be_nil
|
204
|
+
expect(CogiPhony.vinaphone?('8491456789')).to be_nil
|
205
|
+
expect(CogiPhony.vinaphone?('+8491456789')).to be_nil
|
206
|
+
end
|
207
|
+
|
208
|
+
it 'length more than 10 is invalid' do
|
209
|
+
expect(CogiPhony.vinaphone?('09145678901')).to be_nil
|
210
|
+
expect(CogiPhony.vinaphone?('849145678901')).to be_nil
|
211
|
+
expect(CogiPhony.vinaphone?('+849145678901')).to be_nil
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
context 'mobile phone begin with 123' do
|
216
|
+
it 'length equal 11 is valid' do
|
217
|
+
expect(CogiPhony.vinaphone?('01234567890')).not_to be_nil
|
218
|
+
expect(CogiPhony.vinaphone?('841234567890')).not_to be_nil
|
219
|
+
expect(CogiPhony.vinaphone?('+841234567890')).not_to be_nil
|
220
|
+
end
|
221
|
+
|
222
|
+
it 'length less than 11 is invalid' do
|
223
|
+
expect(CogiPhony.vinaphone?('0123456789')).to be_nil
|
224
|
+
expect(CogiPhony.vinaphone?('84123456789')).to be_nil
|
225
|
+
expect(CogiPhony.vinaphone?('+84123456789')).to be_nil
|
226
|
+
end
|
227
|
+
|
228
|
+
it 'length more than 11 is invalid' do
|
229
|
+
expect(CogiPhony.vinaphone?('012345678901')).to be_nil
|
230
|
+
expect(CogiPhony.vinaphone?('8412345678901')).to be_nil
|
231
|
+
expect(CogiPhony.vinaphone?('+8412345678901')).to be_nil
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
describe '#viettel?' do
|
237
|
+
context 'mobile phone begin with 98' do
|
238
|
+
it 'length equal 10 is valid' do
|
239
|
+
expect(CogiPhony.viettel?('0984567890')).not_to be_nil
|
240
|
+
expect(CogiPhony.viettel?('84984567890')).not_to be_nil
|
241
|
+
expect(CogiPhony.viettel?('+84984567890')).not_to be_nil
|
242
|
+
end
|
243
|
+
|
244
|
+
it 'length less than 10 is invalid' do
|
245
|
+
expect(CogiPhony.viettel?('098456789')).to be_nil
|
246
|
+
expect(CogiPhony.viettel?('8498456789')).to be_nil
|
247
|
+
expect(CogiPhony.viettel?('+8498456789')).to be_nil
|
248
|
+
end
|
249
|
+
|
250
|
+
it 'length more than 10 is invalid' do
|
251
|
+
expect(CogiPhony.viettel?('09845678901')).to be_nil
|
252
|
+
expect(CogiPhony.viettel?('849845678901')).to be_nil
|
253
|
+
expect(CogiPhony.viettel?('+849845678901')).to be_nil
|
254
|
+
end
|
255
|
+
end
|
256
|
+
|
257
|
+
context 'mobile phone begin with 168' do
|
258
|
+
it 'length equal 11 is valid' do
|
259
|
+
expect(CogiPhony.viettel?('01684567890')).not_to be_nil
|
260
|
+
expect(CogiPhony.viettel?('841684567890')).not_to be_nil
|
261
|
+
expect(CogiPhony.viettel?('+841684567890')).not_to be_nil
|
262
|
+
end
|
263
|
+
|
264
|
+
it 'length less than 11 is invalid' do
|
265
|
+
expect(CogiPhony.viettel?('0168456789')).to be_nil
|
266
|
+
expect(CogiPhony.viettel?('84168456789')).to be_nil
|
267
|
+
expect(CogiPhony.viettel?('+84168456789')).to be_nil
|
268
|
+
end
|
269
|
+
|
270
|
+
it 'length more than 11 is invalid' do
|
271
|
+
expect(CogiPhony.viettel?('016845678901')).to be_nil
|
272
|
+
expect(CogiPhony.viettel?('8416845678901')).to be_nil
|
273
|
+
expect(CogiPhony.viettel?('+8416845678901')).to be_nil
|
274
|
+
end
|
275
|
+
end
|
276
|
+
end
|
277
|
+
|
278
|
+
describe '#phone_to_provider' do
|
279
|
+
it 'return Viettel' do
|
280
|
+
expect(CogiPhony.phone_to_provider('0988091097')).to eq('Viettel')
|
281
|
+
expect(CogiPhony.phone_to_provider('84988091097')).to eq('Viettel')
|
282
|
+
expect(CogiPhony.phone_to_provider('+84988091097')).to eq('Viettel')
|
283
|
+
end
|
284
|
+
|
285
|
+
it 'return Mobifone' do
|
286
|
+
expect(CogiPhony.phone_to_provider('0938091097')).to eq('Mobifone')
|
287
|
+
expect(CogiPhony.phone_to_provider('84938091097')).to eq('Mobifone')
|
288
|
+
expect(CogiPhony.phone_to_provider('+84938091097')).to eq('Mobifone')
|
289
|
+
end
|
290
|
+
|
291
|
+
it 'return Vinaphone' do
|
292
|
+
expect(CogiPhony.phone_to_provider('0918091097')).to eq('Vinaphone')
|
293
|
+
expect(CogiPhony.phone_to_provider('84918091097')).to eq('Vinaphone')
|
294
|
+
expect(CogiPhony.phone_to_provider('+84918091097')).to eq('Vinaphone')
|
295
|
+
end
|
296
|
+
|
297
|
+
it 'return Vietnamobile' do
|
298
|
+
expect(CogiPhony.phone_to_provider('0928091097')).to eq('Vietnamobile')
|
299
|
+
expect(CogiPhony.phone_to_provider('84928091097')).to eq('Vietnamobile')
|
300
|
+
expect(CogiPhony.phone_to_provider('+84928091097')).to eq('Vietnamobile')
|
301
|
+
end
|
302
|
+
|
303
|
+
it 'return Gmobile (Beeline)' do
|
304
|
+
expect(CogiPhony.phone_to_provider('0998091097')).to eq('Gmobile (Beeline)')
|
305
|
+
expect(CogiPhony.phone_to_provider('84998091097')).to eq('Gmobile (Beeline)')
|
306
|
+
expect(CogiPhony.phone_to_provider('+84998091097')).to eq('Gmobile (Beeline)')
|
307
|
+
end
|
308
|
+
|
309
|
+
it 'return Others' do
|
310
|
+
expect(CogiPhony.phone_to_provider('0837621351')).to eq('Others')
|
311
|
+
expect(CogiPhony.phone_to_provider('0643676907')).to eq('Others')
|
312
|
+
expect(CogiPhony.phone_to_provider('+14037089189')).to eq('Others')
|
313
|
+
end
|
314
|
+
end
|
315
|
+
|
316
|
+
describe '#vn_mobile_phone?' do
|
317
|
+
context 'Mobile phone in Vietnam' do
|
318
|
+
it 'begin with 88 and length is 10 return true' do
|
319
|
+
expect(CogiPhony.vn_mobile_phone?('0883081090')).to eq(true)
|
320
|
+
expect(CogiPhony.vn_mobile_phone?('84883081090')).to eq(true)
|
321
|
+
expect(CogiPhony.vn_mobile_phone?('+84883081090')).to eq(true)
|
322
|
+
end
|
323
|
+
|
324
|
+
it 'begin with 8 but is not mobile phone' do
|
325
|
+
expect(CogiPhony.vn_mobile_phone?('84837621350')).to eq(false)
|
326
|
+
end
|
327
|
+
|
328
|
+
it 'begin with 9 and length is 10 return true' do
|
329
|
+
expect(CogiPhony.vn_mobile_phone?('0933081090')).to eq(true)
|
330
|
+
expect(CogiPhony.vn_mobile_phone?('84933081090')).to eq(true)
|
331
|
+
expect(CogiPhony.vn_mobile_phone?('+84933081090')).to eq(true)
|
332
|
+
end
|
333
|
+
|
334
|
+
it 'begin with 1 and length is 11 return true' do
|
335
|
+
expect(CogiPhony.vn_mobile_phone?('01214468866')).to eq(true)
|
336
|
+
expect(CogiPhony.vn_mobile_phone?('841214468866')).to eq(true)
|
337
|
+
expect(CogiPhony.vn_mobile_phone?('+841214468866')).to eq(true)
|
338
|
+
end
|
339
|
+
|
340
|
+
it 'does not a valid mobile phone format' do
|
341
|
+
expect(CogiPhony.vn_mobile_phone?('09330810900')).to eq(false)
|
342
|
+
expect(CogiPhony.vn_mobile_phone?('012144688666')).to eq(false)
|
343
|
+
expect(CogiPhony.vn_mobile_phone?('0933081090a')).to eq(false)
|
344
|
+
expect(CogiPhony.vn_mobile_phone?('093308109-0')).to eq(false)
|
345
|
+
expect(CogiPhony.vn_mobile_phone?('+14037089189')).to eq(false)
|
346
|
+
end
|
347
|
+
end
|
348
|
+
|
349
|
+
context 'Land line in Vietnam' do
|
350
|
+
it 'return false' do
|
351
|
+
expect(CogiPhony.vn_mobile_phone?('0643893907')).to eq(false)
|
352
|
+
end
|
353
|
+
end
|
354
|
+
|
355
|
+
context 'Foreign mobile phone' do
|
356
|
+
it 'return false' do
|
357
|
+
expect(CogiPhony.vn_mobile_phone?('+14037089189')).to eq(false)
|
358
|
+
end
|
359
|
+
end
|
360
|
+
end
|
361
|
+
|
362
|
+
describe '#global_format' do
|
363
|
+
context 'with country code' do
|
364
|
+
it 'format vietnam mobile phone number without plus sign' do
|
365
|
+
expect(CogiPhony.global_format('84933081090')).to eq('+84 93 3081090')
|
366
|
+
end
|
367
|
+
|
368
|
+
it 'format vietnam phone number without plus sign' do
|
369
|
+
expect(CogiPhony.global_format('84837621350')).to eq('+84 8 3762 1350')
|
370
|
+
end
|
371
|
+
|
372
|
+
it 'format international mobile phone number without plus sign' do
|
373
|
+
expect(CogiPhony.global_format('14037089189')).to eq('+1 (403) 708-9189')
|
374
|
+
end
|
375
|
+
|
376
|
+
it 'format vietnam mobile phone number with plus sign' do
|
377
|
+
expect(CogiPhony.global_format('+84933081090')).to eq('+84933081090')
|
378
|
+
end
|
379
|
+
end
|
380
|
+
|
381
|
+
context 'without country code' do
|
382
|
+
it 'return raw phone number' do
|
383
|
+
expect(CogiPhony.global_format('0933081090')).to eq('0933081090')
|
384
|
+
end
|
385
|
+
end
|
386
|
+
end
|
387
|
+
|
388
|
+
describe '#national_format' do
|
389
|
+
it 'format mobile phone number' do
|
390
|
+
expect(CogiPhony.national_format('84933081090')).to eq('093 3081090')
|
391
|
+
end
|
392
|
+
|
393
|
+
it 'format phone number' do
|
394
|
+
expect(CogiPhony.national_format('84837621350')).to eq('08 3762 1350')
|
395
|
+
end
|
396
|
+
end
|
397
|
+
|
398
|
+
describe '#format' do
|
399
|
+
context 'global format' do
|
400
|
+
it 'format vietnam mobile phone number without specify format' do
|
401
|
+
expect(CogiPhony.format('84933081090')).to eq('+84 93 3081090')
|
402
|
+
end
|
403
|
+
|
404
|
+
it 'format vietnam mobile phone number' do
|
405
|
+
expect(CogiPhony.format('84933081090', format: 'global')).to eq('+84 93 3081090')
|
406
|
+
end
|
407
|
+
|
408
|
+
it 'format vietnam phone number' do
|
409
|
+
expect(CogiPhony.format('84837621350', format: 'global')).to eq('+84 8 3762 1350')
|
410
|
+
end
|
411
|
+
end
|
412
|
+
|
413
|
+
context 'vietnam format' do
|
414
|
+
it 'format vietnam mobile phone number' do
|
415
|
+
expect(CogiPhony.format('84933081090', format: 'vietnam')).to eq('093 3081090')
|
416
|
+
end
|
417
|
+
|
418
|
+
it 'format vietnam phone number' do
|
419
|
+
expect(CogiPhony.format('84837621350', format: 'vietnam')).to eq('08 3762 1350')
|
420
|
+
end
|
421
|
+
end
|
422
|
+
end
|
423
|
+
|
424
|
+
describe '#country_code_from_number' do
|
425
|
+
it 'return country code' do
|
426
|
+
expect(CogiPhony.country_code_from_number('+84933081090')).to eq('84')
|
427
|
+
expect(CogiPhony.country_code_from_number('84933081090')).to eq('84')
|
428
|
+
end
|
429
|
+
|
430
|
+
it 'return nil' do
|
431
|
+
expect(CogiPhony.country_code_from_number('0933081090')).to be_nil
|
432
|
+
end
|
433
|
+
end
|
434
|
+
|
435
|
+
describe '#normalize' do
|
436
|
+
context 'Phone with country code' do
|
437
|
+
it 'remove all non-digit characters' do
|
438
|
+
expect(CogiPhony.normalize('+84 933081090 ')).to eq('+84933081090')
|
439
|
+
expect(CogiPhony.normalize('(+84)933081090 ')).to eq('+84933081090')
|
440
|
+
expect(CogiPhony.normalize('+8493-308-1090')).to eq('+84933081090')
|
441
|
+
expect(CogiPhony.normalize('+1 (403) 708-9189')).to eq('+14037089189')
|
442
|
+
end
|
443
|
+
|
444
|
+
it 'add plus sign before' do
|
445
|
+
expect(CogiPhony.normalize('84933081090 ')).to eq('+84933081090')
|
446
|
+
end
|
447
|
+
end
|
448
|
+
|
449
|
+
context 'Phone without country code' do
|
450
|
+
context 'vietnam format' do
|
451
|
+
it 'replace 0 with 84' do
|
452
|
+
expect(CogiPhony.normalize('0933081090', format: 'vietnam')).to eq('+84933081090')
|
453
|
+
expect(CogiPhony.normalize('0837621351', format: 'vietnam')).to eq('+84837621351')
|
454
|
+
end
|
455
|
+
|
456
|
+
it 'insert 84 before mobile phone number' do
|
457
|
+
expect(CogiPhony.normalize('933081090', format: 'vietnam')).to eq('+84933081090')
|
458
|
+
expect(CogiPhony.normalize('988091097', format: 'vietnam')).to eq('+84988091097')
|
459
|
+
expect(CogiPhony.normalize('1214468866', format: 'vietnam')).to eq('+841214468866')
|
460
|
+
end
|
461
|
+
end
|
462
|
+
|
463
|
+
context 'global format' do
|
464
|
+
context 'with default country code' do
|
465
|
+
it 'add country code before phone number' do
|
466
|
+
expect(CogiPhony.normalize('0933081090', default_country_code: '84')).to eq('+84933081090')
|
467
|
+
expect(CogiPhony.normalize('933081090', default_country_code: '84')).to eq('+84933081090')
|
468
|
+
expect(CogiPhony.normalize('093-308-1090', default_country_code: '84')).to eq('+84933081090')
|
469
|
+
expect(CogiPhony.normalize('(403) 708-9189', default_country_code: '1')).to eq('+14037089189')
|
470
|
+
end
|
471
|
+
|
472
|
+
it 'raise error if default country code is not valid' do
|
473
|
+
# expect(CogiPhony.normalize('0933081090', default_country_code: '111111')).to raise_error(CogiPhony::NormalizationError)
|
474
|
+
# expect(CogiPhony.normalize('0933081090', default_country_code: 'abcd')).to raise_error(CogiPhony::NormalizationError)
|
475
|
+
end
|
476
|
+
end
|
477
|
+
|
478
|
+
context 'without default country code' do
|
479
|
+
it 'raise error' do
|
480
|
+
# expect(CogiPhony.normalize('0933081090', format: 'global')).to raise_error(CogiPhony::NormalizationError)
|
481
|
+
# expect(CogiPhony.normalize('0933081090')).to raise_error(CogiPhony::NormalizationError)
|
482
|
+
# expect(CogiPhony.normalize('37621351', format: 'global')).to raise_error(CogiPhony::NormalizationError)
|
483
|
+
end
|
484
|
+
end
|
485
|
+
end
|
486
|
+
end
|
487
|
+
end
|
488
|
+
|
489
|
+
describe '#validate?' do
|
490
|
+
context 'Phone with country code' do
|
491
|
+
it 'vietnam mobile phone with plus sign' do
|
492
|
+
expect(CogiPhony.validate?('+84933081090')).to be_truthy
|
493
|
+
end
|
494
|
+
|
495
|
+
it 'vietnam mobile phone without plus sign' do
|
496
|
+
expect(CogiPhony.validate?('84933081090')).to be_truthy
|
497
|
+
end
|
498
|
+
|
499
|
+
it 'vietnam phone with plus sign' do
|
500
|
+
expect(CogiPhony.validate?('+84837621350')).to be_truthy
|
501
|
+
end
|
502
|
+
|
503
|
+
it 'other country mobile phone with plus sign' do
|
504
|
+
expect(CogiPhony.validate?('+14037089189')).to be_truthy
|
505
|
+
end
|
506
|
+
|
507
|
+
it 'other country mobile phone without plus sign' do
|
508
|
+
expect(CogiPhony.validate?('14037089189')).to be_truthy
|
509
|
+
end
|
510
|
+
|
511
|
+
it 'phone formatted in international format' do
|
512
|
+
expect(CogiPhony.validate?('+1 (403) 708-9189')).to be_truthy
|
513
|
+
expect(CogiPhony.validate?('(408) 974-2042')).to be_truthy
|
514
|
+
end
|
515
|
+
|
516
|
+
it 'phone formatted in national format' do
|
517
|
+
expect(CogiPhony.validate?('093 3081090')).to be_truthy
|
518
|
+
end
|
519
|
+
end
|
520
|
+
|
521
|
+
context 'Phone without country code' do
|
522
|
+
it 'vietnam mobile phone' do
|
523
|
+
expect(CogiPhony.validate?('0933081090')).to be_truthy
|
524
|
+
end
|
525
|
+
|
526
|
+
it 'vietnam phone' do
|
527
|
+
expect(CogiPhony.validate?('0837621350')).to be_truthy
|
528
|
+
end
|
529
|
+
|
530
|
+
it 'vietnam phone without region code' do
|
531
|
+
expect(CogiPhony.validate?('37621350')).to be_truthy
|
532
|
+
end
|
533
|
+
end
|
534
|
+
end
|
535
|
+
end
|