phony 2.8.9 → 2.9.0

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: fd8cbfadedb2800791fcac55bacbd9b2ef09ab13
4
- data.tar.gz: 202911c35b494806e41e00e5717bf4147912694e
3
+ metadata.gz: 4183dcc6d47df59a308089bef203e7946735dd6f
4
+ data.tar.gz: 395ef432f8497cc175b20b424dfabf41011a0afa
5
5
  SHA512:
6
- metadata.gz: 7804bf547d723df7106f1e10a0d3b1dbcc0ab665a0066729ba75c47944de22d8233dec5e19bb205d477a16b8792a7b0f753cae32930e8f6ee67da5cc1f314aa2
7
- data.tar.gz: 52efca5ffa5152fc6050071a0f47bea7c6d578dc91a53d5415b7ccd0eaeba1c89d53af6d00c571b509792c3f95f62b2f22d9ac048a92eec120f141667afbb868
6
+ metadata.gz: 7a0b6e84eb116e0b018ae6930af4a9833373805a7ba719d7083a235353bca9798f247e3e42061c43bd4500b26dac307fd8554fff4b31ea43c555f86d4d5e8d31
7
+ data.tar.gz: 18b922a9c4e066cc2f579ab00a4c5e0120ad9b2c280a34419ec1132f24b6888945405c89aeeecf7f6ddaac313f38a5b2294337c7a17ec0c1843430af717b1f97
@@ -10,11 +10,12 @@ module Phony
10
10
  attr_accessor :international_absolute_format, :international_relative_format, :national_format
11
11
 
12
12
  def initialize
13
- @international_absolute_format = '+%s%s%s'
14
- @international_relative_format = '00%s%s%s'
15
- @national_format = '%s%s'
13
+ @international_absolute_format = '+%s%s%s%s%s'
14
+ @international_relative_format = '00%s%s%s%s%s'
15
+ @national_format = '%s%s%s%s'
16
16
 
17
17
  @default_space = ' '
18
+ @default_local_space = ' '
18
19
  end
19
20
 
20
21
  def self.instance
@@ -84,48 +85,50 @@ module Phony
84
85
  end
85
86
 
86
87
  def format number, options = {}
87
- format_cc_ndc_local options[:format], options[:spaces] || @default_space, *split(number)
88
+ format_cc_ndc_local options[:format],
89
+ options[:spaces] || @default_space,
90
+ options[:local_spaces] || options[:spaces] || @default_local_space,
91
+ *split(number)
88
92
  end
89
93
  alias formatted format
90
94
 
91
95
  # Formats country code and national destination code.
92
96
  #
93
- def format_cc_ndc_local format, space, cc, trunk, ndc, *parts
94
- cc_ndc = format_cc_ndc format, space, cc, trunk, ndc
95
- local = if parts.empty?
96
- cc_ndc = cc_ndc.slice 0...cc_ndc.rindex(space.to_s)
97
- EMPTY_STRING
98
- else
99
- format_local(space, parts) unless parts.empty?
100
- end
101
- cc_ndc.empty?? local : "#{cc_ndc}#{space}#{local}"
102
- end
103
- #
104
- # TODO This method needs an overhaul.
105
- #
106
- def format_cc_ndc format, space, cc, trunk, ndc
97
+ def format_cc_ndc_local format, space, local_space, cc, trunk, ndc, *parts
98
+ local = if parts.empty?
99
+ EMPTY_STRING
100
+ else
101
+ format_local(local_space, parts) unless parts.empty?
102
+ end
103
+ format_cc_ndc format, space, cc, trunk, ndc, local
104
+
105
+ # cc_ndc = cc_ndc.slice 0...cc_ndc.rindex(space.to_s) if parts.empty?
106
+ end
107
+ def format_cc_ndc format, space, cc, trunk, ndc, local
107
108
  case format
109
+ when String
110
+ format % { :cc => cc, :ndc => ndc, :local => local }
108
111
  when nil, :international_absolute, :international, :+
109
112
  ndc ?
110
- @international_absolute_format % [cc, space, ndc] :
111
- @international_absolute_format % [cc, nil, nil]
113
+ @international_absolute_format % [cc, space, ndc, space, local] :
114
+ @international_absolute_format % [cc, space, local, nil, nil]
112
115
  when :international_relative
113
116
  ndc ?
114
- @international_relative_format % [cc, space, ndc] :
115
- @international_relative_format % [cc, nil, nil]
117
+ @international_relative_format % [cc, space, ndc, space, local] :
118
+ @international_relative_format % [cc, space, local, nil, nil]
116
119
  when :national
117
120
  # Replaces the %s in the trunk code with a "space".
118
121
  trunk = trunk % space if trunk && trunk.size > 1
119
122
  ndc && !ndc.empty? ?
120
- @national_format % [trunk, ndc] :
121
- @national_format % [trunk, nil]
123
+ @national_format % [trunk, ndc, space, local] :
124
+ @national_format % [trunk, nil, nil, nil]
122
125
  when :local
123
- EMPTY_STRING
126
+ local
124
127
  end
125
128
  end
126
- def format_local space, parts_ary
129
+ def format_local local_space, parts_ary
127
130
  parts_ary.compact!
128
- parts_ary.join space.to_s
131
+ parts_ary.join local_space.to_s
129
132
  end
130
133
 
131
134
  #
@@ -0,0 +1,189 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Phony#format' do
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
+ describe 'cases' do
28
+
29
+ describe 'exceptions' do
30
+ it 'raises on nil' do
31
+ expect {
32
+ Phony.format nil
33
+ }.to raise_error(ArgumentError, 'Phone number cannot be nil. Use e.g. number && Phony.format(number).')
34
+ end
35
+ end
36
+ describe 'default' do
37
+ it 'should format Swiss numbers' do
38
+ Phony.format('41443643532').should eql '+41 44 364 35 32'
39
+ end
40
+ it 'should format Swiss national numbers' do
41
+ Phony.format('41443643532', :format => :national).should eql '044 364 35 32'
42
+ end
43
+ # TODO
44
+ #
45
+ it 'should format Swiss service numbers' do
46
+ Phony.format('41800112233').should eql '+41 800 112 233'
47
+ end
48
+ it 'should format Austrian numbers' do
49
+ Phony.format('43198110').should eql '+43 1 98110'
50
+ end
51
+ it 'should format American numbers' do
52
+ Phony.format('18705551122').should eql '+1 870 555 1122'
53
+ end
54
+ it 'should format New Zealand 021 6-digit mobile numbers' do
55
+ Phony.format('6421123456').should eql '+64 21 123 456'
56
+ end
57
+ it 'should format New Zealand 021 7-digit mobile numbers' do
58
+ Phony.format('64211234567').should eql '+64 21 123 4567'
59
+ end
60
+ it 'should format New Zealand 021 8-digit mobile numbers' do
61
+ Phony.format('642112345678').should eql '+64 21 12 345 678'
62
+ end
63
+ it 'should format New Zealand landline numbers' do
64
+ Phony.format('6493791234').should eql '+64 9 379 1234'
65
+ end
66
+ it 'should format Indian numbers' do
67
+ Phony.format('914433993939').should eql '+91 44 339 93 939'
68
+ end
69
+ it 'should format Russian numbers' do
70
+ Phony.format(Phony.normalize('+370 800 12345'), :format => :international).should eql '+370 800 12 345'
71
+ end
72
+ it 'should format Russian numbers' do
73
+ Phony.format(Phony.normalize('+7 812 123 4567'), :format => :international).should eql '+7 812 123 4567'
74
+ end
75
+ it 'should format Russian numbers' do
76
+ Phony.format(Phony.normalize('+370 800 12345'), :format => :national).should eql '8800 12 345'
77
+ end
78
+ it 'should format Russian numbers' do
79
+ Phony.format(Phony.normalize('+7 812 123 4567'), :format => :national).should eql '8812 123 4567'
80
+ end
81
+ end
82
+ describe 'international' do
83
+ it 'should format north american numbers' do
84
+ Phony.format('18091231234', :format => :international).should eql '+1 809 123 1234'
85
+ end
86
+ it 'should format austrian numbers' do
87
+ Phony.format('43198110', :format => :international).should eql '+43 1 98110'
88
+ end
89
+ it 'should format austrian numbers' do
90
+ Phony.format('43198110', :format => :international_absolute).should eql '+43 1 98110'
91
+ end
92
+ it 'should format french numbers' do
93
+ Phony.format('33142278186', :format => :+).should eql '+33 1 42 27 81 86'
94
+ end
95
+ it 'should format austrian numbers' do
96
+ Phony.format('43198110', :format => :international_relative).should eql '0043 1 98110'
97
+ end
98
+ it 'should format liechtensteiner numbers' do
99
+ Phony.format('4233841148', :format => :international_relative).should eql '00423 384 11 48'
100
+ end
101
+ context 'with no spaces' do
102
+ it 'should format north american numbers' do
103
+ Phony.format('18091231234', :format => :international, :spaces => '').should eql '+18091231234'
104
+ end
105
+ it 'should format austrian numbers' do
106
+ Phony.format('43198110', :format => :international, :spaces => '').should eql '+43198110'
107
+ end
108
+ it 'should format austrian numbers' do
109
+ Phony.format('43198110', :format => :international_absolute, :spaces => '').should eql '+43198110'
110
+ end
111
+ it 'should format french numbers' do
112
+ Phony.format('33142278186', :format => :+, :spaces => '').should eql '+33142278186'
113
+ end
114
+ it 'should format austrian numbers' do
115
+ Phony.format('43198110', :format => :international_relative, :spaces => '').should eql '0043198110'
116
+ end
117
+ it 'should format liechtensteiner numbers' do
118
+ Phony.format('4233841148', :format => :international_relative, :spaces => '').should eql '004233841148'
119
+ end
120
+ end
121
+ context 'with special spaces' do
122
+ it 'should format swiss numbers' do
123
+ Phony.format('41443643532', :format => :international).should eql '+41 44 364 35 32'
124
+ end
125
+ it 'should format north american numbers' do
126
+ Phony.format('18091231234', :format => :international, :spaces => :-).should eql '+1-809-123-1234'
127
+ end
128
+ it 'should format austrian numbers' do
129
+ Phony.format('43198110', :format => :international, :spaces => :-).should eql '+43-1-98110'
130
+ end
131
+ it 'should format austrian numbers' do
132
+ Phony.format('43198110', :format => :international_absolute, :spaces => :-).should eql '+43-1-98110'
133
+ end
134
+ it 'should format french numbers' do
135
+ Phony.format('33142278186', :format => :+, :spaces => :-).should eql '+33-1-42-27-81-86'
136
+ end
137
+ it 'should format austrian numbers' do
138
+ Phony.format('43198110', :format => :international_relative, :spaces => :-).should eql '0043-1-98110'
139
+ end
140
+ it 'should format liechtensteiner numbers' do
141
+ Phony.format('4233841148', :format => :international_relative, :spaces => :-).should eql '00423-384-11-48'
142
+ end
143
+ end
144
+ describe "'unsupported' countries" do
145
+ it 'should format as a single block' do
146
+ Phony.format('88132155605220').should eql '+881 32155605220'
147
+ end
148
+ it 'should format as a single block, regardless of format' do
149
+ Phony.format('8811819372205', :format => :international).should eql '+881 1819372205'
150
+ end
151
+ it 'should format as a single block, respecting custom spaces' do
152
+ Phony.format('8811819372205', :spaces => :-).should eql '+881-1819372205'
153
+ end
154
+ it 'should format as a single block, even without spaces' do
155
+ Phony.format('8811819372205', :spaces => '').should eql '+8811819372205'
156
+ end
157
+ end
158
+ end
159
+ describe 'national' do
160
+ it 'should format swiss numbers' do
161
+ Phony.format('41443643532', :format => :national).should eql '044 364 35 32'
162
+ end
163
+ # TODO
164
+ #
165
+ it 'should format swiss service numbers' do
166
+ Phony.format('41800112233', :format => :national).should eql '0800 112 233'
167
+ end
168
+ it 'should format austrian numbers' do
169
+ Phony.format('43198110', :format => :national).should eql '01 98110'
170
+ end
171
+ it 'should format US numbers with a leading trunk 1' do
172
+ Phony.format('14159224711', :format => :national).should eql '1 415 922 4711'
173
+ end
174
+ it 'should format Dutch numbers with a trunk code' do
175
+ Phony.format('311012341234', :format => :national).should eql '010 123 41234'
176
+ end
177
+ end
178
+ describe 'local' do
179
+ it 'should format swiss numbers' do
180
+ Phony.format('41443643532', :format => :local).should eql '364 35 32'
181
+ end
182
+ it 'should format german numbers' do
183
+ Phony.format('493038625454', :format => :local).should eql '386 25454'
184
+ end
185
+ end
186
+
187
+ end
188
+
189
+ end
@@ -12,7 +12,7 @@ describe Phony::CountryCodes do
12
12
  end
13
13
  it 'formats correctly' do
14
14
  old_format = @countries.international_absolute_format
15
- @countries.international_absolute_format = '!!! %s%s%s'
15
+ @countries.international_absolute_format = '!!! %s%s%s%s%s'
16
16
 
17
17
  @countries.formatted('41443643532', :format => :international).should eql '!!! 41 44 364 35 32'
18
18
 
@@ -25,7 +25,7 @@ describe Phony::CountryCodes do
25
25
  end
26
26
  it 'formats correctly' do
27
27
  old_format = @countries.international_relative_format
28
- @countries.international_relative_format = '000 %s%s%s'
28
+ @countries.international_relative_format = '000 %s%s%s%s%s'
29
29
 
30
30
  @countries.formatted('41443643532', :format => :international_relative).should eql '000 41 44 364 35 32'
31
31
 
@@ -38,7 +38,7 @@ describe Phony::CountryCodes do
38
38
  end
39
39
  it 'formats correctly' do
40
40
  old_format = @countries.national_format
41
- @countries.national_format = '%s%s'
41
+ @countries.national_format = '%s%s%s%s'
42
42
 
43
43
  # Removes CC 1, but adds national call prefix 1.
44
44
  #
@@ -21,172 +21,6 @@ describe Phony do
21
21
  end
22
22
  end
23
23
 
24
- describe 'formatted' do
25
- it 'is an alias of format' do
26
- Phony.formatted('41443643532').should eql '+41 44 364 35 32'
27
- end
28
- end
29
-
30
- describe 'format' do
31
- describe 'exceptions' do
32
- it 'raises on nil' do
33
- expect {
34
- Phony.format nil
35
- }.to raise_error(ArgumentError, 'Phone number cannot be nil. Use e.g. number && Phony.format(number).')
36
- end
37
- end
38
- describe 'default' do
39
- it 'should format Swiss numbers' do
40
- Phony.format('41443643532').should eql '+41 44 364 35 32'
41
- end
42
- it 'should format Swiss national numbers' do
43
- Phony.format('41443643532', :format => :national).should eql '044 364 35 32'
44
- end
45
- # TODO
46
- #
47
- it 'should format Swiss service numbers' do
48
- Phony.format('41800112233').should eql '+41 800 112 233'
49
- end
50
- it 'should format Austrian numbers' do
51
- Phony.format('43198110').should eql '+43 1 98110'
52
- end
53
- it 'should format American numbers' do
54
- Phony.format('18705551122').should eql '+1 870 555 1122'
55
- end
56
- it 'should format New Zealand 021 6-digit mobile numbers' do
57
- Phony.format('6421123456').should eql '+64 21 123 456'
58
- end
59
- it 'should format New Zealand 021 7-digit mobile numbers' do
60
- Phony.format('64211234567').should eql '+64 21 123 4567'
61
- end
62
- it 'should format New Zealand 021 8-digit mobile numbers' do
63
- Phony.format('642112345678').should eql '+64 21 12 345 678'
64
- end
65
- it 'should format New Zealand landline numbers' do
66
- Phony.format('6493791234').should eql '+64 9 379 1234'
67
- end
68
- it 'should format Indian numbers' do
69
- Phony.format('914433993939').should eql '+91 44 339 93 939'
70
- end
71
- it 'should format Russian numbers' do
72
- Phony.format(Phony.normalize('+370 800 12345'), :format => :international).should eql '+370 800 12 345'
73
- end
74
- it 'should format Russian numbers' do
75
- Phony.format(Phony.normalize('+7 812 123 4567'), :format => :international).should eql '+7 812 123 4567'
76
- end
77
- it 'should format Russian numbers' do
78
- Phony.format(Phony.normalize('+370 800 12345'), :format => :national).should eql '8800 12 345'
79
- end
80
- it 'should format Russian numbers' do
81
- Phony.format(Phony.normalize('+7 812 123 4567'), :format => :national).should eql '8812 123 4567'
82
- end
83
- end
84
- describe 'international' do
85
- it 'should format north american numbers' do
86
- Phony.format('18091231234', :format => :international).should eql '+1 809 123 1234'
87
- end
88
- it 'should format austrian numbers' do
89
- Phony.format('43198110', :format => :international).should eql '+43 1 98110'
90
- end
91
- it 'should format austrian numbers' do
92
- Phony.format('43198110', :format => :international_absolute).should eql '+43 1 98110'
93
- end
94
- it 'should format french numbers' do
95
- Phony.format('33142278186', :format => :+).should eql '+33 1 42 27 81 86'
96
- end
97
- it 'should format austrian numbers' do
98
- Phony.format('43198110', :format => :international_relative).should eql '0043 1 98110'
99
- end
100
- it 'should format liechtensteiner numbers' do
101
- Phony.format('4233841148', :format => :international_relative).should eql '00423 384 11 48'
102
- end
103
- context 'with no spaces' do
104
- it 'should format north american numbers' do
105
- Phony.format('18091231234', :format => :international, :spaces => '').should eql '+18091231234'
106
- end
107
- it 'should format austrian numbers' do
108
- Phony.format('43198110', :format => :international, :spaces => '').should eql '+43198110'
109
- end
110
- it 'should format austrian numbers' do
111
- Phony.format('43198110', :format => :international_absolute, :spaces => '').should eql '+43198110'
112
- end
113
- it 'should format french numbers' do
114
- Phony.format('33142278186', :format => :+, :spaces => '').should eql '+33142278186'
115
- end
116
- it 'should format austrian numbers' do
117
- Phony.format('43198110', :format => :international_relative, :spaces => '').should eql '0043198110'
118
- end
119
- it 'should format liechtensteiner numbers' do
120
- Phony.format('4233841148', :format => :international_relative, :spaces => '').should eql '004233841148'
121
- end
122
- end
123
- context 'with special spaces' do
124
- it 'should format swiss numbers' do
125
- Phony.format('41443643532', :format => :international).should eql '+41 44 364 35 32'
126
- end
127
- it 'should format north american numbers' do
128
- Phony.format('18091231234', :format => :international, :spaces => :-).should eql '+1-809-123-1234'
129
- end
130
- it 'should format austrian numbers' do
131
- Phony.format('43198110', :format => :international, :spaces => :-).should eql '+43-1-98110'
132
- end
133
- it 'should format austrian numbers' do
134
- Phony.format('43198110', :format => :international_absolute, :spaces => :-).should eql '+43-1-98110'
135
- end
136
- it 'should format french numbers' do
137
- Phony.format('33142278186', :format => :+, :spaces => :-).should eql '+33-1-42-27-81-86'
138
- end
139
- it 'should format austrian numbers' do
140
- Phony.format('43198110', :format => :international_relative, :spaces => :-).should eql '0043-1-98110'
141
- end
142
- it 'should format liechtensteiner numbers' do
143
- Phony.format('4233841148', :format => :international_relative, :spaces => :-).should eql '00423-384-11-48'
144
- end
145
- end
146
- describe "'unsupported' countries" do
147
- it 'should format as a single block' do
148
- Phony.format('88132155605220').should eql '+881 32155605220'
149
- end
150
- it 'should format as a single block, regardless of format' do
151
- Phony.format('8811819372205', :format => :international).should eql '+881 1819372205'
152
- end
153
- it 'should format as a single block, respecting custom spaces' do
154
- Phony.format('8811819372205', :spaces => :-).should eql '+881-1819372205'
155
- end
156
- it 'should format as a single block, even without spaces' do
157
- Phony.format('8811819372205', :spaces => '').should eql '+8811819372205'
158
- end
159
- end
160
- end
161
- describe 'national' do
162
- it 'should format swiss numbers' do
163
- Phony.format('41443643532', :format => :national).should eql '044 364 35 32'
164
- end
165
- # TODO
166
- #
167
- it 'should format swiss service numbers' do
168
- Phony.format('41800112233', :format => :national).should eql '0800 112 233'
169
- end
170
- it 'should format austrian numbers' do
171
- Phony.format('43198110', :format => :national).should eql '01 98110'
172
- end
173
- it 'should format US numbers with a leading trunk 1' do
174
- Phony.format('14159224711', :format => :national).should eql '1 415 922 4711'
175
- end
176
- it 'should format Dutch numbers with a trunk code' do
177
- Phony.format('311012341234', :format => :national).should eql '010 123 41234'
178
- end
179
- end
180
- describe 'local' do
181
- it 'should format swiss numbers' do
182
- Phony.format('41443643532', :format => :local).should eql '364 35 32'
183
- end
184
- it 'should format german numbers' do
185
- Phony.format('493038625454', :format => :local).should eql '386 25454'
186
- end
187
- end
188
- end
189
-
190
24
  context 'minimal cases' do
191
25
  context 'splitting' do
192
26
  describe 'exceptions' 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.8.9
4
+ version: 2.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Hanke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-17 00:00:00.000000000 Z
11
+ date: 2014-12-23 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
@@ -76,6 +76,7 @@ files:
76
76
  - lib/phony/trunk_code.rb
77
77
  - lib/phony/vanity.rb
78
78
  - spec/functional/error_spec.rb
79
+ - spec/functional/format_spec.rb
79
80
  - spec/functional/normalize_spec.rb
80
81
  - spec/functional/plausibility_spec.rb
81
82
  - spec/lib/phony/countries_spec.rb
@@ -118,6 +119,7 @@ summary: Fast international phone number (E164 standard) normalizing, splitting
118
119
  formatting.
119
120
  test_files:
120
121
  - spec/functional/error_spec.rb
122
+ - spec/functional/format_spec.rb
121
123
  - spec/functional/normalize_spec.rb
122
124
  - spec/functional/plausibility_spec.rb
123
125
  - spec/lib/phony/countries_spec.rb