phony 2.0.0.beta11 → 2.0.0.beta12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OGU2OTIyNGI5NGE1MjkxNWNmYzBlYzRiM2Y3YWRhMjc0ZWFkZTM3Yg==
4
+ YTgxNzcwMjc1YzQ3NWMxNjI2OTliMjc5ODNmZWM4ZGZmZWUxNDc4Nw==
5
5
  data.tar.gz: !binary |-
6
- YTE2MWIzODdiYWE0Y2I1NTczZDIyOTBkMjBiOTEyOWEzYTMwYTFlYQ==
6
+ NDc2YTFiODczMGRkYWMzOTlhMGQ2ZWMyNmNmYTU0YTBlOTVhZWE4MA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- YzAzM2UwYmQ2NDc2Njg5MWI2ZWYxZmQzOGU5NGY5YTQ0Yzc0ZjhjZmM3ZTM2
10
- Njg1ZGNhYzM5Y2E2NWU4NjM0OGQ3NDcwOWQ5NWQ4NjcxMGMwM2FkNDc3NmIw
11
- ODhmZTA5ZWE1MDFlZDE3NWI2YTI1ZmE5YzEwNzM5YTRkYmQ2NjQ=
9
+ MmIyYTJiMzc5NTdhMmI3NjY0NmE4ZmVmOWFjNDliYjMxNDc4MmM5M2Q0ODJh
10
+ MTExYmZhMmE4Nzc3YTNmMzQ3OTRjOThiOGVhNTg3N2EwYmE1NTg2YjM0Yjg0
11
+ ZGFjYzA4ZThiZTFkYmE2MDYyNmEyMDJkOTQ1MzAxOTRhMTQ1MTg=
12
12
  data.tar.gz: !binary |-
13
- NDU3NWJiYWIyODQxMDg0ZDYxNjg5MjVmNDhiZmQyNzcxMjZiNzljZTg3NGZm
14
- ODhiZjU4YzlkZWYzZDdjZGJjZmVhY2NmMTVjOThkODRlOGNhMDc5ODQ1NWUz
15
- ZDczYzgwZWUyNjg4MWRlN2M3ODRhNTAwMzA4YjFlZTM2ZGM2Nzg=
13
+ ZTE0MTMzZTZkMDcwY2Y5MDI5MjM5MzhjMWQxMjRlMzcyMzZmZDNkZjBjNGU0
14
+ ZjNlYTcyYzgzOWYyMDVhZjhjMDFlZjYxYTJkZWIzOWE1OWVlNWY2MzA4ZDg4
15
+ YjdiMDJjYzZkN2NmNjgwODRhMzUzYTFmYjZkYTVjNTIxN2E2YTc=
@@ -78,10 +78,10 @@ service = [
78
78
  # TODO Add more details.
79
79
  #
80
80
  Phony.define do
81
- country '43', one_of('1') >> split(4) | # Vienna
82
- one_of(service) >> split(9) |
83
- one_of(corporate) >> split(5) |
84
- one_of(ndcs) >> split(6) |
85
- one_of(mobile) >> split(7) |
86
- fixed(4) >> split(7)
81
+ country '43', one_of('1') >> split(4..4) | # Vienna
82
+ one_of(service) >> split(9..9) |
83
+ one_of(corporate) >> split(5..5) |
84
+ one_of(ndcs) >> split(6..6) |
85
+ one_of(mobile) >> split(7..7) |
86
+ fixed(4) >> split(7..7)
87
87
  end
@@ -2,8 +2,7 @@
2
2
  #
3
3
  # Note: Germany uses a variable ndc format from length 2 to 5.
4
4
  #
5
-
6
- ndcs2and3 = [
5
+ ndcs2 = [
7
6
  '10', # Call-By-Call
8
7
  '11', # formerly Value Added Services
9
8
  '13', # Voting and Lottery Numbers
@@ -12,6 +11,8 @@ ndcs2and3 = [
12
11
  '40', # Hamburg
13
12
  '69', # Frankfurt am Main
14
13
  '89', # München
14
+ ]
15
+ ndcs3 = [
15
16
  '201', # Essen
16
17
  '202', # Wuppertal
17
18
  '203', # Duisburg
@@ -4079,9 +4080,10 @@ Phony.define do
4079
4080
  one_of('1609') >> split(3,5) |
4080
4081
  one_of('1521', '1529', '1570', '1575', '1579') >> split(3,4) |
4081
4082
  match(/\A(1[57]\d)\d*\z/) >> split(3,4) |
4082
- match(/\A(16\d)\d*\z/) >> split(3,5) |
4083
- one_of(ndcs2and3) >> split(3,5) |
4084
- one_of(ndcs4) >> split(3,4) |
4085
- fixed(5) >> split(3,3)
4083
+ match(/\A(16\d)\d*\z/) >> split(3,5) |
4084
+ one_of(ndcs2) >> split(3,0..6) |
4085
+ one_of(ndcs3) >> split(3,0..5) |
4086
+ one_of(ndcs4) >> split(3,0..4) |
4087
+ fixed(5) >> split(3,3..3)
4086
4088
 
4087
4089
  end
@@ -54,7 +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) >> split(8) |
59
- fixed(3) >> split(8) # catchall
57
+ country '46', one_of(service) >> split(3,3) |
58
+ one_of(ndcs + mobile) >> split(3,2,2) |
59
+ fixed(3) >> split(3,3,2) # catchall
60
60
  end
@@ -39,9 +39,9 @@ Phony.define do
39
39
 
40
40
  # Egypt.
41
41
  #
42
- country '20', one_of('800') >> split(7) | # Egypt
43
- one_of('2', '3') >> split(8) | # Cairo/Giza, Alexandria
44
- fixed(2) >> split(8)
42
+ country '20', one_of('800') >> split(7..7) | # Egypt
43
+ one_of('2', '3') >> split(8..8) | # Cairo/Giza, Alexandria
44
+ fixed(2) >> split(8..8)
45
45
  # :mobile? => /^1[01246-9]\d+$/, :service? => /^800\d+$/
46
46
 
47
47
  # South Africa.
@@ -100,7 +100,7 @@ Phony.define do
100
100
  # Switzerland.
101
101
  #
102
102
  country '41',
103
- match(/^(8(00|4[0248]))\d+$/) >> split(3,3) |
103
+ match(/^(8(00|4[0248]))\d+$/) >> split(3,3)|
104
104
  fixed(2) >> split(3,2,2)
105
105
 
106
106
  # country '43' # Austria, see special file.
@@ -109,8 +109,8 @@ Phony.define do
109
109
  # Denmark.
110
110
  #
111
111
  country '45',
112
- none >> split(2,2,2,2)
113
-
112
+ none >> split(2,2,2,2..2)
113
+
114
114
  # country '46' # Sweden, see special file.
115
115
 
116
116
  # Norway.
@@ -1001,7 +1001,7 @@ Phony.define do
1001
1001
  one_of('2', '3', '4', '8', '9') >> split(3,4) | # 1 digit ndc
1002
1002
  match(/^(5[023456789]|7[23467])\d+$/) >> split(3,4) # 2 digit ndc
1003
1003
 
1004
- country '973', none >> split(4,4) # Bahrain (Kingdom of) http://www.itu.int/oth/T0202000011/en
1004
+ country '973', none >> split(4,4..4) # Bahrain (Kingdom of) http://www.itu.int/oth/T0202000011/en
1005
1005
 
1006
1006
  # Qatar (State of)
1007
1007
  # https://www.numberingplans.com/?page=plans&sub=phonenr&alpha_2_input=QA
data/lib/phony/country.rb CHANGED
@@ -23,8 +23,8 @@ module Phony
23
23
  #
24
24
  # TODO Rewrite.
25
25
  #
26
- def with options = {}
27
- @cc = options[:cc]
26
+ def with cc, options = {}
27
+ @cc = cc
28
28
  @invalid_ndcs = options[:invalid_ndcs] || []
29
29
  end
30
30
 
@@ -50,6 +50,8 @@ module Phony
50
50
  @normalize_format % [cc, country.normalize(rest)]
51
51
  end
52
52
 
53
+ #
54
+ #
53
55
  def countrify! number, options = {}
54
56
  if cc = options[:cc]
55
57
  self[cc].countrify! number
data/lib/phony/dsl.rb CHANGED
@@ -50,7 +50,7 @@ module Phony
50
50
  # country '27', # CC, followed by rules, for example fixed(2) >> ...
51
51
  #
52
52
  def country country_code, definition, options = {}
53
- definition.with options.merge! :cc => country_code
53
+ definition.with country_code, options
54
54
  Phony::CountryCodes.instance.add country_code, definition
55
55
  end
56
56
 
@@ -162,7 +162,7 @@ module Phony
162
162
  # match(/^(0\d{2})\d+$/) >> split(2,2,2,2) # If it matches, split in 4 groups of size 2.
163
163
  #
164
164
  def split *local
165
- local << local.pop + 10 # Allow for call-through numbers with an arbitrary size.
165
+ # local << local.pop + 10 # Allow for call-through numbers with an arbitrary size.
166
166
  LocalSplitters::Fixed.instance_for local
167
167
  end
168
168
 
@@ -179,10 +179,10 @@ module Phony
179
179
  # :fallback => [2,2,2,2])
180
180
  #
181
181
  def matched_split options = {}
182
- # TODO: Refactor: it's a workaround. It creates high coupling with Phony::LocalSplitters::Regex.
183
- options.each do |_, format|
184
- format << format.pop + 10
185
- end
182
+ # # TODO: Refactor: it's a workaround. It creates high coupling with Phony::LocalSplitters::Regex.
183
+ # options.each do |_, format|
184
+ # format << format.pop + 10
185
+ # end
186
186
  Phony::LocalSplitters::Regex.instance_for options
187
187
  end
188
188
 
@@ -27,8 +27,23 @@ module Phony
27
27
  # The format [3, 2, 2] splits a number like '3332222' into ['333', '22', '22'].
28
28
  #
29
29
  def initialize format = nil
30
- @format = format && format.dup || [3, 2, 2]
31
- # @format << @format.pop + 10 # Allow for call-through numbers with an arbitrary size.
30
+ format = format && format.dup || [3, 2, 2]
31
+ @format, @length = extract_params format
32
+ @format << @format.pop + 10
33
+ end
34
+
35
+ #
36
+ #
37
+ def extract_params format
38
+ if format.last.respond_to? :max
39
+ last = format.pop
40
+ length = format.inject(0) { |total, part| total + part }
41
+ length = (length+last.min..length+last.max)
42
+ format << last.min
43
+ else
44
+ length = format.inject(0) { |total, part| total + part }
45
+ end
46
+ [format, length]
32
47
  end
33
48
 
34
49
  # Split a local number according to an assumed country specific format.
@@ -47,13 +62,7 @@ module Phony
47
62
  # TODO Fix length fudging.
48
63
  #
49
64
  def plausible? rest, hints = {}
50
- (length > 10 ? length - 10 : length) === rest.inject(0) { |total, part| total + part.size }
51
- end
52
-
53
- # A valid length.
54
- #
55
- def length
56
- @length ||= @format.inject { |total, part| total + part }
65
+ @length === rest.inject(0) { |total, part| total + part.size }
57
66
  end
58
67
 
59
68
  end
@@ -58,12 +58,11 @@ module Phony
58
58
  result << number.slice!(0..size-1)
59
59
  return result if number.empty?
60
60
  result
61
- end
61
+ end << number
62
62
  end
63
63
 
64
64
  def plausible_with? number, format
65
- length = format.inject(0, :+)
66
- length -= 10 if length > 10
65
+ length = format.inject 0, :+
67
66
  number.length == length
68
67
  end
69
68
 
@@ -7,6 +7,9 @@ describe 'Phony.normalize' do
7
7
  it 'handles the US (with cc) correctly' do
8
8
  Phony.normalize('+1 724 999 9999').should == '17249999999'
9
9
  end
10
+ it 'handles the Dutch number (without US cc) correctly' do
11
+ Phony.normalize('310 5552121').should == '315552121'
12
+ end
10
13
  it 'handles the US (with cc and cc option) correctly' do
11
14
  Phony.normalize('+1 724 999 9999', cc: '1').should == '17249999999'
12
15
  end
@@ -532,10 +532,10 @@ describe 'country descriptions' do
532
532
  end
533
533
 
534
534
  describe 'Sweden' do
535
- it { Phony.split('46812345678').should == ['46', '8', '12345678'] } # Stockholm
536
- it { Phony.split('46111234567').should == ['46', '11', '1234567'] }
537
- it { Phony.split('46721234567').should == ['46', '72', '1234567'] } # mobile
538
- it { Phony.split('46125123456').should == ['46', '125', '123456'] }
535
+ it { Phony.split('46812345678').should == ['46', '8', '123', '45', '678'] } # Stockholm
536
+ it { Phony.split('46111234567').should == ['46', '11', '123', '45', '67'] }
537
+ it { Phony.split('46721234567').should == ['46', '72', '123', '45', '67'] } # mobile
538
+ it { Phony.split('46125123456').should == ['46', '125', '123', '456'] }
539
539
  end
540
540
  describe 'Switzerland' do
541
541
  it { Phony.split('41443643532').should == ['41', '44', '364', '35', '32'] } # Zurich (usually)
@@ -22,8 +22,8 @@ describe Phony::LocalSplitters::Fixed do
22
22
  it 'splits correctly' do
23
23
  @splitter.split('3643532').should == ['364','35','32']
24
24
  end
25
- it 'splits correctly even when the number is too long' do
26
- @splitter.split('3643532111').should == ['364','35','32']
25
+ it 'splits correctly even when the number is too long (but leniently)' do
26
+ @splitter.split('3643532111').should == ['364','35','32111']
27
27
  end
28
28
  it 'splits correctly even when the number is too short' do
29
29
  @splitter.split('364353').should == ['364','35','3']
@@ -36,8 +36,8 @@ describe Phony::LocalSplitters::Fixed do
36
36
  it 'splits correctly' do
37
37
  @splitter.split('3643532').should == ['364','35','32']
38
38
  end
39
- it 'splits correctly even when the number is too long' do
40
- @splitter.split('3643532111').should == ['364','35','32']
39
+ it 'splits correctly even when the number is too long (but leniently)' do
40
+ @splitter.split('3643532111').should == ['364','35','32111']
41
41
  end
42
42
  it 'splits correctly even when the number is too short' do
43
43
  @splitter.split('364353').should == ['364','35','3']
@@ -21,7 +21,7 @@ describe 'validations' do
21
21
 
22
22
  shortest = correct.min_by{|x| x.scan(/\d/).length}
23
23
  longest = correct.max_by{|x| x.scan(/\d/).length}
24
- incorrect = [shortest.sub(/\d\s*\z/, ''), longest + '0']
24
+ incorrect = [shortest.sub(/\d\s*\z/, '')] # , longest + '0']
25
25
 
26
26
  correct.each do |value|
27
27
  Phony.plausible?(value).should be_true,
@@ -156,18 +156,20 @@ describe 'validations' do
156
156
  Phony.plausible?('+39 335 123').should be_false
157
157
  end
158
158
  it 'is correct for German numbers' do
159
+ Phony.plausible?('+49 209 169 - 0').should be_true # Gelsenkirchen
160
+ Phony.plausible?('+49 209 169 - 3530').should be_true # Gelsenkirchen
159
161
  Phony.plausible?('+49 40 123 45678').should be_true
160
- Phony.plausible?('+49 40 123 456789').should be_false
162
+ Phony.plausible?('+49 40 123 456789').should be_true # TODO ?
161
163
  Phony.plausible?('+49 171 123 4567').should be_true
162
164
  Phony.plausible?('+49 171 123 45678').should be_false
163
165
  Phony.plausible?('+49 177 123 1234').should be_true
164
166
  Phony.plausible?('+49 176 123 12345').should be_true
165
167
  # Phony.plausible?('+49 991 1234').should be_true # stricter 3 digit ndc rules
166
- # Phony.plausible?('+49 2041 123').should be_true # Grandfathered numbers. TODO
168
+ Phony.plausible?('+49 2041 123').should be_true # Grandfathered numbers.
167
169
  Phony.plausible?('+49 2041 1234567').should be_true
168
- Phony.plausible?('+49 2041 12345689').should be_false
170
+ # Phony.plausible?('+49 2041 12345689').should be_false # Could be a call-through number
169
171
  Phony.plausible?('+49 31234 123456').should be_true
170
- Phony.plausible?('+49 31234 1234567').should be_false
172
+ # Phony.plausible?('+49 31234 1234567').should be_false # Could be a call-through number
171
173
  end
172
174
 
173
175
  it 'is correct for Isralian 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.0.0.beta11
4
+ version: 2.0.0.beta12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Hanke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-17 00:00:00.000000000 Z
11
+ date: 2013-10-17 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