phony 2.12.8 → 2.12.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4fa0e706a618d0a9b1a60f0256ad496743c52235
4
- data.tar.gz: a483882673bc99b4d88b82ffadad0cc7465557b7
3
+ metadata.gz: 6e3d86e7298e2ebf5c621f7cabbafdb706863231
4
+ data.tar.gz: 90e5956221c59daa70fad43fef10c300f9525f8d
5
5
  SHA512:
6
- metadata.gz: fda7a75e5097c61a0df6a60fc254be719e0f2157dbd57c3ce45d96972fd13835773ba2b79e2a52f073a01bd81e4196ae3c8a4b607fe98144f621d615841ac585
7
- data.tar.gz: ed85fd7b00cd8a7ae12185e272fb483380ece8e52fc32f7c6a7e6432973f0df94e75bafde1d6533cc3546a8e19ff218db332a0bb0ad8a45c4d4f68da7a92ec4c
6
+ metadata.gz: 05c9832e7d1de23aa9114cbc1998859dc1c29e8a69b8281b890dd134aa7b2a7efafe5e888548a4034c477c3e3240ffde87e67c6ccfcf401feca97fb3046db692
7
+ data.tar.gz: 14e74fd73e52ef533f39443d2a4d02fbe20ceae43076a28203c6d35c0d8356e7f61f27c32590c484ea361b995549b12b369e8cfabe34d9ea312e5cbd2e5d2dad
@@ -56,7 +56,7 @@ module Phony
56
56
 
57
57
  # Cleans all non-numeric characters.
58
58
  #
59
- @@basic_cleaning_pattern = /\(0\)|\D/
59
+ @@basic_cleaning_pattern = /\(0|\D/
60
60
  # Clean number of all non-numeric characters and return a copy.
61
61
  #
62
62
  def clean number
@@ -28,15 +28,15 @@ module Phony
28
28
  countries[cc.size][cc]
29
29
  end
30
30
 
31
- # Clean number of all non-numeric characters, initial zeros or (0).
31
+ # Clean number of all non-numeric characters, initial zeros or (0.
32
32
  #
33
- @@basic_cleaning_pattern = /\A00?|\(0\)|\D/
34
- # Clean number of all non-numeric characters, initial zeros or (0) and return it.
33
+ @@basic_cleaning_pattern = /\A00?|\(0|\D/
34
+ # Clean number of all non-numeric characters, initial zeros or (0 and return it.
35
35
  #
36
36
  def clean number
37
37
  clean! number && number.dup
38
38
  end
39
- # Clean number of all non-numeric characters, initial zeros or (0) and return a copy.
39
+ # Clean number of all non-numeric characters, initial zeros or (0 and return a copy.
40
40
  #
41
41
  def clean! number
42
42
  number.gsub!(@@basic_cleaning_pattern, EMPTY_STRING) || number
@@ -2,203 +2,140 @@ require 'spec_helper'
2
2
 
3
3
  describe 'Phony#format' do
4
4
 
5
- describe 'formatted' do
6
- it 'is an alias of format' do
7
- Phony.formatted('41443643532').should eql '+41 44 364 35 32'
8
- end
9
- end
10
-
11
- describe 'with templates' do
12
- it 'handles a basic template correctly' do
13
- Phony.format('41443643532', :format => 'A%{cc}B%{ndc}C%{local}').should eql 'A41B44C364 35 32'
14
- end
15
- it 'handles a funky template correctly' do
16
- Phony.format('41443643532', :format => '%{local}%{ndc}%{cc}', :local_spaces => '').should eql '36435324441'
17
- end
18
- it 'handles local_spaces correctly' do
19
- Phony.format('41443643532', :format => 'A%{cc}B%{ndc}C%{local}', :local_spaces => '/').should eql 'A41B44C364/35/32'
20
- end
21
- it 'handles a russian example correctly' do
22
- # https://github.com/floere/phony/issues/214
23
- Phony.format('71234567890', :format => '(+%{cc} %{ndc}) %{local}', :local_spaces => '-').should eql '(+7 123) 45-67890'
24
- end
25
- end
26
-
27
5
  describe 'cases' do
28
-
29
- describe 'exceptions' do
6
+ describe 'Exceptions' do
30
7
  it 'raises on nil' do
31
8
  expect {
32
9
  Phony.format nil
33
10
  }.to raise_error(ArgumentError, 'Phone number cannot be nil. Use e.g. number && Phony.format(number).')
34
11
  end
35
12
  end
36
- describe 'default' do
37
- it 'formats Swiss numbers' do
38
- Phony.format('41443643532').should eql '+41 44 364 35 32'
39
- end
40
- it 'formats Swiss national numbers' do
41
- Phony.format('41443643532', :format => :national).should eql '044 364 35 32'
42
- end
43
- it 'formats Swiss service numbers' do
44
- Phony.format('41800112233').should eql '+41 800 112 233'
45
- end
46
- it 'formats Austrian numbers' do
47
- Phony.format('43198110').should eql '+43 1 98110'
48
- end
49
- it 'formats Danish national numbers' do
50
- Phony.format('4540506070', format: :national).should eql '40 50 60 70'
51
- end
52
- it 'formats American numbers' do
53
- Phony.format('18705551122').should eql '+1 870 555 1122'
54
- end
55
- it 'formats New Zealand 021 6-digit mobile numbers' do
56
- Phony.format('6421123456').should eql '+64 21 123 456'
13
+
14
+ describe 'Templates' do
15
+ it 'handles a basic template correctly' do
16
+ Phony.format('41443643532', :format => 'A%{cc}B%{ndc}C%{local}').should eql 'A41B44C364 35 32'
57
17
  end
58
- it 'formats New Zealand 021 7-digit mobile numbers' do
59
- Phony.format('64211234567').should eql '+64 21 123 4567'
18
+ it 'handles a funky template correctly' do
19
+ Phony.format('41443643532', :format => '%{local}%{ndc}%{cc}', :local_spaces => '').should eql '36435324441'
60
20
  end
61
- it 'formats New Zealand 021 8-digit mobile numbers' do
62
- Phony.format('642112345678').should eql '+64 21 12 345 678'
21
+ it 'handles local_spaces correctly' do
22
+ Phony.format('41443643532', :format => 'A%{cc}B%{ndc}C%{local}', :local_spaces => '/').should eql 'A41B44C364/35/32'
63
23
  end
64
- it 'formats New Zealand landline numbers' do
65
- Phony.format('6493791234').should eql '+64 9 379 1234'
66
- end
67
- it 'formats Indian numbers' do
68
- Phony.format('914433993939').should eql '+91 44 339 93 939'
69
- end
70
- it 'formats Russian numbers' do
71
- Phony.format(Phony.normalize('+370 800 12345'), :format => :international).should eql '+370 800 12 345'
72
- end
73
- it 'formats Russian numbers' do
74
- Phony.format(Phony.normalize('+7 812 123 4567'), :format => :international).should eql '+7 812 123-45-67'
75
- end
76
- it 'formats Russian numbers' do
77
- Phony.format(Phony.normalize('+370 800 12345'), :format => :national).should eql '8800 12 345'
78
- end
79
- it 'formats Russian numbers' do
80
- Phony.format(Phony.normalize('+7 812 123 4567'), :format => :national).should eql '8812 123-45-67'
24
+ it 'handles a russian example correctly' do
25
+ # https://github.com/floere/phony/issues/214
26
+ Phony.format('71234567890', :format => '(+%{cc} %{ndc}) %{local}', :local_spaces => '-').should eql '(+7 123) 45-67890'
81
27
  end
82
28
  end
83
- describe 'international' do
84
- it 'formats north american numbers' do
85
- Phony.format('18091231234', :format => :international).should eql '+1 809 123 1234'
86
- end
87
- it 'formats north american numbers' do
88
- # Gets a trunk code.
89
- Phony.format('14152223333', :format => :national).should eql '1 415 222 3333'
90
- # Does not show a trunk code.
91
- Phony.format('14152223333', :format => :national, :trunk => false).should eql '415 222 3333'
92
- end
93
- it 'formats austrian numbers' do
94
- Phony.format('43198110', :format => :international).should eql '+43 1 98110'
95
- end
96
- it 'formats austrian numbers' do
97
- Phony.format('43198110', :format => :international_absolute).should eql '+43 1 98110'
98
- end
99
- it 'formats french numbers' do
100
- Phony.format('33142278186', :format => :+).should eql '+33 1 42 27 81 86'
101
- end
102
- it 'formats austrian numbers' do
103
- Phony.format('43198110', :format => :international_relative).should eql '0043 1 98110'
104
- end
105
- it 'formats liechtensteiner numbers' do
106
- Phony.format('4233841148', :format => :international_relative).should eql '00423 384 11 48'
107
- end
108
- context 'with no spaces' do
109
- it 'formats north american numbers' do
110
- Phony.format('18091231234', :format => :international, :spaces => '').should eql '+18091231234'
111
- end
112
- it 'formats austrian numbers' do
113
- Phony.format('43198110', :format => :international, :spaces => '').should eql '+43198110'
114
- end
115
- it 'formats austrian numbers' do
116
- Phony.format('43198110', :format => :international_absolute, :spaces => '').should eql '+43198110'
117
- end
118
- it 'formats french numbers' do
119
- Phony.format('33142278186', :format => :+, :spaces => '').should eql '+33142278186'
120
- end
121
- it 'formats italian numbers' do
122
- Phony.format('393333337647', :format => :international).should eql '+39 333 333 7647'
123
- Phony.format('390108480161', :format => :international).should eql '+39 010 8480161'
124
- end
125
- it 'formats austrian numbers' do
126
- Phony.format('43198110', :format => :international_relative, :spaces => '').should eql '0043198110'
127
- end
128
- it 'formats liechtensteiner numbers' do
129
- Phony.format('4233841148', :format => :international_relative, :spaces => '').should eql '004233841148'
130
- end
131
- end
132
- context 'with special spaces' do
133
- it 'formats swiss numbers' do
134
- Phony.format('41443643532', :format => :international).should eql '+41 44 364 35 32'
135
- end
136
- it 'formats north american numbers' do
137
- Phony.format('18091231234', :format => :international, :spaces => :-).should eql '+1-809-123-1234'
138
- end
139
- it 'formats austrian numbers' do
140
- Phony.format('43198110', :format => :international, :spaces => :-).should eql '+43-1-98110'
141
- end
142
- it 'formats austrian numbers' do
143
- Phony.format('43198110', :format => :international_absolute, :spaces => :-).should eql '+43-1-98110'
144
- end
145
- it 'formats french numbers' do
146
- Phony.format('33142278186', :format => :+, :spaces => :-).should eql '+33-1-42-27-81-86'
147
- end
148
- it 'formats austrian numbers' do
149
- Phony.format('43198110', :format => :international_relative, :spaces => :-).should eql '0043-1-98110'
150
- end
151
- it 'formats liechtensteiner numbers' do
152
- Phony.format('4233841148', :format => :international_relative, :spaces => :-).should eql '00423-384-11-48'
153
- end
154
- end
155
- describe "'unsupported' countries" do
156
- it 'formats as a single block' do
157
- Phony.format('88132155605220').should eql '+881 32155605220'
158
- end
159
- it 'formats as a single block, regardless of format' do
160
- Phony.format('8811819372205', :format => :international).should eql '+881 1819372205'
161
- end
162
- it 'formats as a single block, respecting custom spaces' do
163
- Phony.format('8811819372205', :spaces => :-).should eql '+881-1819372205'
164
- end
165
- it 'formats as a single block, even without spaces' do
166
- Phony.format('8811819372205', :spaces => '').should eql '+8811819372205'
29
+
30
+ def self.for_each_from tsv_file_name
31
+ require 'csv'
32
+ describe "from file #{tsv_file_name}" do
33
+ CSV.open(tsv_file_name, headers: true, col_sep: "\t").each.with_index do |row, index|
34
+ country, expected, original, format, spaces, local_spaces = *row.
35
+ values_at('Country', 'Expected', 'Original', 'Format', 'Spaces', 'Local Spaces')
36
+ options = {}
37
+ options[:format] = format.to_sym if format
38
+ options[:spaces] = spaces.to_sym if spaces
39
+ it "(row #{index + 2}) #{country}: #{expected} from #{original} with #{format}, #{spaces}, #{local_spaces}" do
40
+ Phony.format(original, options).should eql expected
41
+ end
167
42
  end
168
43
  end
169
44
  end
170
- describe 'national' do
171
- it 'formats italian numbers' do
172
- Phony.format('393333337647', :format => :national).should eql '333 333 7647'
173
- Phony.format('390108480161', :format => :national).should eql '010 8480161'
174
- end
175
- it 'formats swiss numbers' do
176
- Phony.format('41443643532', :format => :national).should eql '044 364 35 32'
177
- end
178
- # TODO
179
- #
180
- it 'formats swiss service numbers' do
181
- Phony.format('41800112233', :format => :national).should eql '0800 112 233'
182
- end
183
- it 'formats austrian numbers' do
184
- Phony.format('43198110', :format => :national).should eql '01 98110'
185
- end
186
- it 'formats US numbers with a leading trunk 1' do
187
- Phony.format('14159224711', :format => :national).should eql '1 415 922 4711'
188
- end
189
- it 'formats Dutch numbers with a trunk code' do
190
- Phony.format('311012341234', :format => :national).should eql '010 123 41234'
191
- end
45
+
46
+ # Countries.
47
+ #
48
+ for_each_from 'spec/functional/format.tab'
49
+
50
+ describe 'Austria' do
51
+ it { Phony.format('43198110').should eql '+43 1 98110' }
52
+ it { Phony.format('43198110', :format => :international).should eql '+43 1 98110' }
53
+ it { Phony.format('43198110', :format => :international_absolute).should eql '+43 1 98110' }
54
+ it { Phony.format('43198110', :format => :international_relative).should eql '0043 1 98110' }
55
+ it { Phony.format('43198110', :format => :international, :spaces => '').should eql '+43198110' }
56
+ it { Phony.format('43198110', :format => :international_absolute, :spaces => '').should eql '+43198110' }
57
+ it { Phony.format('43198110', :format => :international_relative, :spaces => '').should eql '0043198110' }
58
+ it { Phony.format('43198110', :format => :international, :spaces => :-).should eql '+43-1-98110' }
59
+ it { Phony.format('43198110', :format => :international_absolute, :spaces => :-).should eql '+43-1-98110' }
60
+ it { Phony.format('43198110', :format => :international_relative, :spaces => :-).should eql '0043-1-98110' }
61
+ it { Phony.format('43198110', :format => :national).should eql '01 98110' }
192
62
  end
193
- describe 'local' do
194
- it 'formats swiss numbers' do
195
- Phony.format('41443643532', :format => :local).should eql '364 35 32'
196
- end
197
- it 'formats german numbers' do
198
- Phony.format('493038625454', :format => :local).should eql '386 25454'
199
- end
63
+ describe 'Denmark' do
64
+ it { Phony.format('4540506070', format: :national).should eql '40 50 60 70' }
65
+ end
66
+ describe 'France' do
67
+ it { Phony.format('33142278186', :format => :+).should eql '+33 1 42 27 81 86' }
68
+ it { Phony.format('33142278186', :format => :+, :spaces => '').should eql '+33142278186' }
69
+ it { Phony.format('33142278186', :format => :+, :spaces => :-).should eql '+33-1-42-27-81-86' }
70
+ end
71
+ describe 'India' do
72
+ it { Phony.format('914433993939').should eql '+91 44 339 93 939' }
73
+ end
74
+ describe 'Italy' do
75
+ it { Phony.format('393333337647', :format => :international).should eql '+39 333 333 7647' }
76
+ it { Phony.format('390108480161', :format => :international).should eql '+39 010 8480161' }
77
+ it { Phony.format('393333337647', :format => :national).should eql '333 333 7647' }
78
+ it { Phony.format('390108480161', :format => :national).should eql '010 8480161' }
79
+ end
80
+ describe 'Liechtenstein' do
81
+ it { Phony.format('4233841148', :format => :international_relative).should eql '00423 384 11 48' }
82
+ it { Phony.format('4233841148', :format => :international_relative, :spaces => '').should eql '004233841148' }
83
+ it { Phony.format('4233841148', :format => :international_relative, :spaces => :-).should eql '00423-384-11-48' }
84
+ end
85
+ describe 'NANP' do
86
+ it { Phony.format('18705551122').should eql '+1 870 555 1122' }
87
+ it { Phony.format('18091231234', :format => :international).should eql '+1 809 123 1234' }
88
+ it { Phony.format('18091231234', :format => :international, :spaces => '').should eql '+18091231234' }
89
+ # Gets a trunk code.
90
+ it { Phony.format('14152223333', :format => :national).should eql '1 415 222 3333' }
91
+ # Does not show a trunk code.
92
+ it { Phony.format('14152223333', :format => :national, :trunk => false).should eql '415 222 3333' }
93
+ it { Phony.format('18091231234', :format => :international, :spaces => :-).should eql '+1-809-123-1234' }
94
+ # leading trunk
95
+ it { Phony.format('14159224711', :format => :national).should eql '1 415 922 4711' }
96
+ end
97
+ describe 'Netherlands' do
98
+ it { Phony.format('311012341234', :format => :national).should eql '010 123 41234' }
99
+ end
100
+ describe 'New Zealand' do
101
+ it { Phony.format('6421123456').should eql '+64 21 123 456' }
102
+ it { Phony.format('64211234567').should eql '+64 21 123 4567' }
103
+ it { Phony.format('642112345678').should eql '+64 21 12 345 678' }
104
+ it { Phony.format('6493791234').should eql '+64 9 379 1234' }
105
+ end
106
+ describe 'Russia' do
107
+ it { Phony.format(Phony.normalize('+370 800 12345'), :format => :international).should eql '+370 800 12 345' }
108
+ it { Phony.format(Phony.normalize('+7 812 123 4567'), :format => :international).should eql '+7 812 123-45-67' }
109
+ it { Phony.format(Phony.normalize('+370 800 12345'), :format => :national).should eql '8800 12 345' }
110
+ it { Phony.format(Phony.normalize('+7 812 123 4567'), :format => :national).should eql '8812 123-45-67' }
111
+ end
112
+ describe 'Switzerland' do
113
+ it { Phony.format('41443643532').should eql '+41 44 364 35 32' }
114
+ it { Phony.format('41443643532', :format => :national).should eql '044 364 35 32' }
115
+ it { Phony.format('41800112233').should eql '+41 800 112 233' }
116
+ it { Phony.format('41443643532', :format => :international).should eql '+41 44 364 35 32' }
117
+ it { Phony.format('41443643532', :format => :national).should eql '044 364 35 32' }
118
+ it { Phony.format('41800112233', :format => :national).should eql '0800 112 233' }
119
+ it { Phony.format('41443643532', :format => :local).should eql '364 35 32' }
120
+ it { Phony.format('493038625454', :format => :local).should eql '386 25454' }
121
+ end
122
+ describe 'yet unsupported' do
123
+ # formats as a single block
124
+ it { Phony.format('88132155605220').should eql '+881 32155605220' }
125
+ # formats as a single block, regardless of format
126
+ it { Phony.format('8811819372205', :format => :international).should eql '+881 1819372205' }
127
+ # formats as a single block, respecting custom spaces
128
+ it { Phony.format('8811819372205', :spaces => :-).should eql '+881-1819372205' }
129
+ # formats as a single block, even without spaces
130
+ it { Phony.format('8811819372205', :spaces => '').should eql '+8811819372205' }
200
131
  end
201
132
 
202
133
  end
134
+
135
+ describe 'formatted' do
136
+ it 'is an alias of format' do
137
+ Phony.formatted('41443643532').should eql '+41 44 364 35 32'
138
+ end
139
+ end
203
140
 
204
141
  end
@@ -491,12 +491,11 @@ describe 'plausibility' do
491
491
  Phony.plausible?('+386 41 123 4567').should be_false
492
492
  end
493
493
 
494
- # TODO: more needs to be done here
495
- #
496
494
  it "is correct for Swiss numbers" do
497
495
  Phony.plausible?('+41 44 111 22 33').should be_true
498
496
  Phony.plausible?('+41 44 111 22 334').should be_false
499
497
  Phony.plausible?('+41 44 111 22').should be_false
498
+ Phony.plausible?('+41 (044) 364 35 33').should be_true
500
499
  end
501
500
 
502
501
  it "is correct for Swedish numbers" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phony
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.12.8
4
+ version: 2.12.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Hanke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-07 00:00:00.000000000 Z
11
+ date: 2015-04-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: 'Fast international phone number (E164 standard) normalizing, splitting
14
14
  and formatting. Lots of formatting options: International (+.., 00..), national