phony 1.7.7 → 1.7.8

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.
@@ -10,7 +10,7 @@
10
10
  # * none: Does not have a national destination code, e.g. Denmark, Iceland.
11
11
  # * one_of: Matches one of the following numbers. Splits if it does.
12
12
  # * match: Try to match the regex, and if it matches, splits it off.
13
- # * fixed: Always splits off a fixed length ndc. (Always use last in a | chain) Offers a "zero" formatting option (default true).
13
+ # * fixed: Always splits off a fixed length ndc. (Always use last in a | chain as a catchall) Offers a "zero" formatting option (default true).
14
14
  #
15
15
  # For the national number part, there are two:
16
16
  # * split: Use this number group splitting.
@@ -139,7 +139,7 @@ Phony.define do
139
139
  country '52',
140
140
  match(/^(0\d{2})\d+$/) >> split(2,2,2,2) |
141
141
  match(/^(33|55|81)\d+$/) >> split(2,2,2,2) |
142
- match(/^(\d{3})\d+$/) >> split(3,2,2)
142
+ match(/^(\d{3})\d+$/) >> split(3,2,2) # catchall.
143
143
 
144
144
  # Cuba.
145
145
  #
@@ -66,5 +66,6 @@ service = [
66
66
  ]
67
67
 
68
68
  Phony.define do
69
- country '43', one_of(service + mobile + ndcs, :max_length => 4) >> split(10)
69
+ country '43', one_of(service + mobile + ndcs) >> split(10) |
70
+ fixed(4) >> split(10)
70
71
  end
@@ -21,7 +21,8 @@ mobile = %w{ 130 131 132 133 135 136 137 138 139 145 150 151 152 153 155 156 157
21
21
  service = %w{ 110 114 119 120 122 999 } # Probably not exhaustive. TODO Look at http://www.eoc.com.cn/?action-viewnews-itemid-11493.
22
22
 
23
23
  Phony.define do
24
- country '86', one_of(service) >> split(8) |
25
- one_of(mobile) >> split(4,4) |
26
- one_of(ndcs, :max_length => 3) >> split(4,4)
24
+ country '86', one_of(service) >> split(8) |
25
+ one_of(mobile) >> split(4,4) |
26
+ one_of(ndcs) >> split(4,4) |
27
+ fixed(3) >> split(4,4)
27
28
  end
@@ -4066,7 +4066,8 @@ service = [
4066
4066
  ]
4067
4067
 
4068
4068
  Phony.define do
4069
- country '49', one_of(service) >> split(3,10) |
4070
- match(/^(1[567]\d)\d*$/) >> split(3,10) | # Mobile
4071
- one_of(ndcs, :max_length => 5) >> split(3,10)
4069
+ country '49', one_of(service) >> split(3,10) |
4070
+ match(/^(1[567]\d)\d*$/) >> split(3,10) | # Mobile
4071
+ one_of(ndcs) >> split(3,10) |
4072
+ fixed(5) >> split(3,10)
4072
4073
  end
@@ -69,8 +69,8 @@ Phony.define do
69
69
  country '353', one_of(ndcs_with_7_subscriber_digits) >> split(3,4) |
70
70
  one_of(ndcs_with_5_subscriber_digits) >> split(5) |
71
71
  one_of(ndcs_with_6_subscriber_digits) >> split(6) |
72
- one_of(freefone) >> split(6) |
73
- match(/^(8\d).+$/) >> split(3,4) | # mobile
74
- fixed(3) >> split(6)
72
+ one_of(freefone) >> split(6) |
73
+ match(/^(8\d).+$/) >> split(3,4) | # mobile
74
+ fixed(3) >> split(6) # Fallback
75
75
 
76
76
  end
@@ -18,5 +18,5 @@ Phony.define do
18
18
  country '60', one_of(service) >> split(3,3) | # Service
19
19
  one_of(mobile) >> split(8) | # Mobile
20
20
  one_of(ndcs) >> split(8) | # 1-digit NDCs
21
- fixed(2) >> split(8) # 2-digit NDCs
21
+ fixed(2) >> split(8) # 2-digit NDCs (Also, fallback)
22
22
  end
@@ -51,7 +51,7 @@ service = [
51
51
  ]
52
52
 
53
53
  Phony.define do
54
- country '31', one_of(service) >> split(3,3) |
55
- one_of(['6'] + ndcs, :max_length => 3) >> split(8) # mobile + landline
56
- # 8 is for mobile numbers, other numbers will work as well (they use 7).
54
+ country '31', one_of(service) >> split(3,3) |
55
+ one_of(['6'] + ndcs) >> split(8) | # mobile + landline
56
+ fixed(3) >> split(8) # 8 is for mobile numbers, other numbers will work as well (they use 7).
57
57
  end
@@ -116,8 +116,9 @@ Phony.define do
116
116
  country '7', one_of(ndcs_with_5_subscriber_digits) >> split(1, 2, 2) |
117
117
  one_of(ndcs_with_6_subscriber_digits) >> split(2, 2, 2) |
118
118
  one_of(ndcs_with_7_subscriber_digits) >> split(3, 2, 2) |
119
- one_of(%w(800)) >> split(3, 2, 2) | # Russia free number
119
+ one_of(%w(800)) >> split(3, 2, 2) | # Russia free number
120
120
  one_of(%w(929 995344 9971 99744 9976 997)) >> split(2, 2, 2) | # South Osetia
121
- match(/([67]\d{2})/) >> split(2, 2, 2) | # Kazakhstan: (600..799)
122
- one_of(%w(840 940), :max_length => 3) >> split(2, 2, 2) # Abhasia
121
+ match(/([67]\d{2})/) >> split(2, 2, 2) | # Kazakhstan: (600..799)
122
+ one_of(%w(840 940)) >> split(2,2,2) # Abhasia
123
+ fixed(3) >> split(2,2,2)
123
124
  end
@@ -7,10 +7,14 @@
7
7
  #
8
8
 
9
9
  special = %w{ 100 101 105 106 107 108 109 111 112 113 114 115 116 117 118 119 120 121 122 123 125 127 128 129 131 132 134 141 182 188 }
10
- # mobile = ('10'..'19').to_a # Note: Mobile not used as it is (for now) handled by the one_of rule.
10
+ # mobile = ('10'..'19').to_a # Note: Mobile not used as it is (for now) handled by the fixed catchall.
11
11
 
12
12
  Phony.define do
13
13
  country '82', match(/^(#{special.join("|")})$/) >> split(3,3) | # Special actually don't need to be split – but better err.
14
+ one_of('2') >> split(4,4) | # Seoul, also includes "services".
15
+ fixed(2) >> split(4,4) # catchall
16
+
17
+ # See above.
18
+ #
14
19
  # one_of(*mobile) >> split(4,4) |
15
- one_of('2', :max_length => 2) >> split(4,4) # Seoul, also includes "services".
16
20
  end
@@ -54,6 +54,7 @@ service = [
54
54
  ]
55
55
 
56
56
  Phony.define do
57
- country '46', one_of(service) >> split(3,3) |
58
- one_of(ndcs + mobile, :max_length => 3) >> split(8)
57
+ country '46', one_of(service) >> split(3,3) |
58
+ one_of(ndcs + mobile) >> split(8) |
59
+ fixed(3) >> split(8) # catchall
59
60
  end
data/lib/phony/country.rb CHANGED
@@ -28,7 +28,7 @@ module Phony
28
28
  def split national_number
29
29
  @codes.each do |code|
30
30
  zero, ndc, *rest = code.split national_number
31
- return ndc ? [zero, ndc, *rest] : [zero, nil, *rest] if rest && !rest.empty?
31
+ return [zero, ndc, *rest] if rest && !rest.empty?
32
32
  end
33
33
  end
34
34
 
data/lib/phony/dsl.rb CHANGED
@@ -79,6 +79,10 @@ module Phony
79
79
  NationalSplitters::Variable.new options[:max_length], ndcs
80
80
  end
81
81
  def match regex, options = {}
82
+ # Check if regexp has a group in it.
83
+ #
84
+ raise "Regexp /#{regex.source}/ needs a group in it that defines which digits belong to the NDC." unless regex.source =~ /\(/
85
+
82
86
  on_fail_take = options.delete :on_fail_take
83
87
  NationalSplitters::Regex.instance_for regex, options[:on_fail_take], options
84
88
  end
@@ -13,6 +13,30 @@ module Phony
13
13
  @instance ||= new
14
14
  end
15
15
 
16
+ # On false:
17
+ #
18
+ # This is a hack to make phony's
19
+ # plausible method work even with
20
+ # this splitter.
21
+ #
22
+ # Promise: We will rewrite this soon
23
+ # to beautify, but making it work for
24
+ # people in production is most important
25
+ # right now.
26
+ #
27
+ # The problem is that the validation looks
28
+ # at whether there is a NDC - if it is nil,
29
+ # it is not plausible.
30
+ # (Does not work with this class, of course
31
+ # since using nil is dangerous and breaks
32
+ # abstraction)
33
+ #
34
+ # Note: Actually, it might stay in.
35
+ #
36
+ def split national_number
37
+ [nil, false, national_number]
38
+ end
39
+
16
40
  end
17
41
 
18
42
  end
@@ -48,8 +48,10 @@ module Phony
48
48
 
49
49
  # Element based checking.
50
50
  #
51
- return false unless ndc
52
- return false if ndc.empty?
51
+ # Note: ndc == false means the country has none.
52
+ #
53
+ return false if ndc.nil?
54
+ return false if ndc && ndc.empty?
53
55
 
54
56
  # A valid range for the rest is 0 or 3+ total digits.
55
57
  #
@@ -63,7 +63,7 @@ describe 'country descriptions' do
63
63
  Phony.split('5342123456').should == ['53', '42', '123456'] # Villa Clara
64
64
  end
65
65
  it 'handles danish numbers' do
66
- Phony.split('4532121212').should == ['45', nil, '32', '12', '12', '12']
66
+ Phony.split('4532121212').should == ['45', false, '32', '12', '12', '12']
67
67
  end
68
68
  it 'handles dutch numbers' do
69
69
  Phony.split('31612345678').should == ['31', '6', '12345678'] # mobile
@@ -109,10 +109,10 @@ describe 'country descriptions' do
109
109
  Phony.split('3622123456').should == ['36', '22', '123', '456']
110
110
  end
111
111
  it 'handles icelandic numbers' do
112
- Phony.split('354112').should == ['354', nil, '112'] # Emergency TODO
113
- Phony.split('3544211234').should == ['354', nil, '421', '1234'] # Keflavík
114
- Phony.split('3544621234').should == ['354', nil, '462', '1234'] # Akureyri
115
- Phony.split('3545511234').should == ['354', nil, '551', '1234'] # Reykjavík
112
+ Phony.split('354112').should == ['354', false, '112'] # Emergency TODO
113
+ Phony.split('3544211234').should == ['354', false, '421', '1234'] # Keflavík
114
+ Phony.split('3544621234').should == ['354', false, '462', '1234'] # Akureyri
115
+ Phony.split('3545511234').should == ['354', false, '551', '1234'] # Reykjavík
116
116
  end
117
117
  it 'handles irish numbers' do
118
118
  Phony.split('35311234567').should == ['353', '1', '123', '4567'] # Dublin, 7 digit subscriber #
@@ -149,14 +149,14 @@ describe 'country descriptions' do
149
149
  Phony.split('526641231212').should == ['52', '664', '123', '12', '12'] # Tijuana
150
150
  end
151
151
  it 'handles norwegian numbers' do
152
- Phony.split('4721234567').should == ['47',nil,'21','23','45','67']
153
- Phony.split('4731234567').should == ['47',nil,'31','23','45','67']
154
- Phony.split('4741234567').should == ['47',nil,'412','34','567']
155
- Phony.split('4751234567').should == ['47',nil,'51','23','45','67']
156
- Phony.split('4761234567').should == ['47',nil,'61','23','45','67']
157
- Phony.split('4771234567').should == ['47',nil,'71','23','45','67']
158
- Phony.split('4781234567').should == ['47',nil,'812','34','567']
159
- Phony.split('4791234567').should == ['47',nil,'912','34','567']
152
+ Phony.split('4721234567').should == ['47',false,'21','23','45','67']
153
+ Phony.split('4731234567').should == ['47',false,'31','23','45','67']
154
+ Phony.split('4741234567').should == ['47',false,'412','34','567']
155
+ Phony.split('4751234567').should == ['47',false,'51','23','45','67']
156
+ Phony.split('4761234567').should == ['47',false,'61','23','45','67']
157
+ Phony.split('4771234567').should == ['47',false,'71','23','45','67']
158
+ Phony.split('4781234567').should == ['47',false,'812','34','567']
159
+ Phony.split('4791234567').should == ['47',false,'912','34','567']
160
160
  end
161
161
  it 'handles peruvian numbers' do
162
162
  Phony.split('51112341234').should == ['51', '1', '1234', '1234'] # Lima
@@ -202,7 +202,7 @@ describe 'country descriptions' do
202
202
  Phony.split('21621231234').should == ['216', '2', '123', '1234'] # Bizerte
203
203
  end
204
204
  it 'handles singaporean numbers' do
205
- Phony.split('6561231234').should == ['65', nil, '6123', '1234'] # Fixed line
205
+ Phony.split('6561231234').should == ['65', false, '6123', '1234'] # Fixed line
206
206
  end
207
207
  it 'handles slovakian numbers' do
208
208
  Phony.split('421912123456').should == ['421', '912', '123456'] # Mobile
@@ -12,7 +12,7 @@ describe Phony::Country do
12
12
  @iceland = described_class.new national_code
13
13
  end
14
14
  it 'splits correctly' do
15
- @iceland.split('112').should == [nil, nil, '112']
15
+ @iceland.split('112').should == [nil, false, '112']
16
16
  end
17
17
  end
18
18
  end
@@ -3,10 +3,21 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe Phony::DSL do
6
+
7
+ let(:dsl) { described_class.new }
6
8
 
7
9
  it 'has a todo' do
8
10
  Phony.define do
9
- todo.split("123456789012345").should == [nil, nil, nil, nil, '123456789012345']
11
+ todo.split("123456789012345").should == [nil, false, nil, nil, '123456789012345']
12
+ end
13
+ end
14
+
15
+ describe 'match' do
16
+ it 'checks for ( in regex' do
17
+ expect { dsl.match(/123/) }.to raise_error("Regexp /123/ needs a group in it that defines which digits belong to the NDC.")
18
+ end
19
+ it 'checks for ( in regex' do
20
+ dsl.match(/(123)/).should be_kind_of(Phony::NationalSplitters::Regex)
10
21
  end
11
22
  end
12
23
 
@@ -12,7 +12,7 @@ describe Phony::NationalCode do
12
12
  @national = Phony::NationalCode.new national_splitter, local_splitter
13
13
  end
14
14
  it 'splits correctly' do
15
- @national.split('112').should == [nil, nil, '112']
15
+ @national.split('112').should == [nil, false, '112']
16
16
  end
17
17
  end
18
18
  end
@@ -11,7 +11,7 @@ describe Phony::NationalSplitters::None do
11
11
  describe 'split' do
12
12
  let(:splitter) { described_class.instance_for }
13
13
  it 'splits correctly into ndc and rest' do
14
- splitter.split('123456789').should == [nil, nil, '123456789']
14
+ splitter.split('123456789').should == [nil, false, '123456789']
15
15
  end
16
16
  end
17
17
 
@@ -22,6 +22,12 @@ describe 'validations' do
22
22
  # it 'is correct' do
23
23
  # Phony.plausible?('+1911').should be_false
24
24
  # end
25
+ it 'is correct' do
26
+ Phony.plausible?('45 44 11 22 33').should be_true
27
+ end
28
+ it 'is correct' do
29
+ Phony.plausible?('+4231231212').should be_true
30
+ end
25
31
  it 'is correct' do
26
32
  Phony.plausible?('010').should be_false
27
33
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phony
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.7
4
+ version: 1.7.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-05 00:00:00.000000000 Z
12
+ date: 2012-08-19 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ! 'Fast international phone number (E164 standard) normalizing, splitting
15
15
  and formatting. Lots of formatting options: International (+.., 00..), national
@@ -83,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
83
  version: '0'
84
84
  requirements: []
85
85
  rubyforge_project:
86
- rubygems_version: 1.8.11
86
+ rubygems_version: 1.8.23
87
87
  signing_key:
88
88
  specification_version: 3
89
89
  summary: Fast international phone number (E164 standard) normalizing, splitting and
@@ -103,4 +103,3 @@ test_files:
103
103
  - spec/lib/phony/validations_spec.rb
104
104
  - spec/lib/phony/vanity_spec.rb
105
105
  - spec/lib/phony_spec.rb
106
- has_rdoc: false