phony 1.6.8 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -1,7 +1,11 @@
1
+ "!{float:right}https://secure.travis-ci.org/floere/phony.png!":http://travis-ci.org/floere/phony
2
+
1
3
  h1. Phony
2
4
 
3
5
  Used in e.g.: "airbnb.com":http://airbnb.com, "restorm.com":http://restorm.com, "socialcam.com":http://socialcam.com, "zendesk.com":http://www.zendesk.com/ (and many, many others).
4
6
 
7
+ There's also a Rails wrapper: https://github.com/joost/phony_rails.
8
+
5
9
  h2. Description
6
10
 
7
11
  This gem can normalize, format and split E164 numbers.
@@ -9,7 +13,7 @@ This gem can normalize, format and split E164 numbers.
9
13
 
10
14
  The (admittedly crazy) *goal* of this Gem is to be able to format/split all phone numbers in the world.
11
15
 
12
- Currently handles Afghan, Algerian, Austrian, Australian, Belgian, Brazilian, Chilean, Chinese, Croatian, Cuban, Czech, Danish, Dutch, Egyptian, French, German, Ghanan, Greek, Hungarian, Italian, Ireland, Lithuanian, Malaysian, Mexican, (The) Netherlands, New Zealand, Norwegian, Peruvian, Polish, Russian, Romanian, Singapore, Slovakian, South African, South Korean, Spanish, Swedish, Swiss, Thailand, Tunisian, Turkish, Liechtenstein, UK, US, and Venezuelan numbers.
16
+ Currently handles Afghan, Algerian, Austrian, Australian, Belgian, Brazilian, Chilean, Chinese, Croatian, Cuban, Czech, Danish, Dutch, Egyptian, French, German, Ghanan, Greek, Hungarian, Italian, Irish, Lithuanian, Malaysian, Mexican, New Zealand, Norwegian, Peruvian, Polish, Russian, Romanian, Singapore, Slovakian, South African, South Korean, Spanish, Swedish, Swiss, Thailand, Tunisian, Turkish, Liechtenstein, UK, US, Venezuelan, and Vietnamese numbers.
13
17
  And to some extent, all others. Just try if it works for you.
14
18
 
15
19
  If it doesn't, please "enter an issue":http://github.com/floere/phony/issues.
@@ -18,9 +22,39 @@ h2. Installation
18
22
 
19
23
  <pre><code>gem install phony</code></pre>
20
24
 
21
- h2. Some examples
25
+ h2(#examples). Some examples
26
+
27
+ h3(#plausibility). Plausibility
28
+
29
+ "Plausible" means "seems reasonable or probable", not implying 100% correctness on a @true@, but implying 100% correctness on a @false@ return value. So if this returns @true@, it might still be not a plausible number. If it returns @false@, it is definitely not an E164 conform number.
30
+
31
+ Use this method in combination with @normalize@ for saving it into the database. Also see https://github.com/floere/phony/issues/35 for a discussion on the topic.
32
+
33
+ Note that you can add constraints to the plausibility check like the country code, @cc@, and the national destination code, @ndc@. Use the options to further limit the plausibility range. For example, only allow a small number of countries.
34
+
35
+ @Phony.plausible?('0000000').should be_false@
36
+
37
+ @Phony.plausible?('hello').should be_false@
38
+
39
+ @Phony.plausible?('+41 44 111 22 33').should be_true@
40
+
41
+ @Phony.plausible?('+41 44 111 22 33', cc: '41').should be_true@
42
+
43
+ @Phony.plausible?('+41 44 111 22 33', ndc: '44').should be_true@
44
+
45
+ @Phony.plausible?('+41 44 111 22 33', cc: '1').should be_false@
46
+
47
+ @Phony.plausible?('+41 44 111 22 33', ndc: '43').should be_false@
48
+
49
+ @Phony.plausible?('+41 44 111 22 33', cc: '41', ndc: '44').should be_true@
50
+
51
+ @Phony.plausible?('+41 44 111 22 33', cc: /4(0|2)/, ndc: /4(4|5)/).should be_false@
52
+
53
+ @Phony.plausible?('+41 44 111 22 33', cc: /4(0|1)/, ndc: /4(4|5)/).should be_true@
54
+
55
+ h3(#normalizing). Normalizing
22
56
 
23
- h3. Normalizing
57
+ This will often raise an error if you try normalizing a non E164-izable number (a number that does not contain enough information to be normalized into an E164 conform number). Use @Phony.plausible?@ for checking if it can be normalized first.
24
58
 
25
59
  @Phony.normalize('41443643533').should == '41443643533'@
26
60
 
@@ -40,7 +74,7 @@ h3. Normalizing
40
74
 
41
75
  @Phony.normalize('+41 (044) 364 35 33').should == '41443643533'@
42
76
 
43
- h3. Formatting
77
+ h3(#formatting). Formatting
44
78
 
45
79
  @Phony.formatted('41443643532').should == '+41 44 364 35 32'@
46
80
 
@@ -50,7 +84,7 @@ h3. Formatting
50
84
 
51
85
  @Phony.formatted('18705551122').should == '+1 870 555 1122'@
52
86
 
53
- h4. International
87
+ h4(#international). International
54
88
 
55
89
  @Phony.formatted('18091231234', :format => :international).should == '+1 809 123 1234'@
56
90
 
@@ -64,7 +98,7 @@ h4. International
64
98
 
65
99
  @Phony.formatted('4233841148', :format => :international_relative).should == '00423 384 11 48'@
66
100
 
67
- h4. With spaces
101
+ h4(#spaces). With spaces
68
102
 
69
103
  @Phony.formatted('18091231234', :format => :international, :spaces => '').should == '+18091231234'@
70
104
 
@@ -78,7 +112,7 @@ h4. With spaces
78
112
 
79
113
  @Phony.formatted('4233841148', :format => :international_relative, :spaces => '').should == '004233841148'@
80
114
 
81
- h4. With special spaces
115
+ h4(#special). With special spaces
82
116
 
83
117
  @Phony.formatted('18091231234', :format => :international, :spaces => :-).should == '+1-809-123-1234'@
84
118
 
@@ -92,7 +126,7 @@ h4. With special spaces
92
126
 
93
127
  @Phony.formatted('4233841148', :format => :international_relative, :spaces => :-).should == '00423-384-11-48'@
94
128
 
95
- h4. National
129
+ h4(#national). National
96
130
 
97
131
  @Phony.formatted('41443643532', :format => :national).should == '044 364 35 32'@
98
132
 
@@ -100,13 +134,13 @@ h4. National
100
134
 
101
135
  @Phony.formatted('43198110', :format => :national).should == '01 98110'@
102
136
 
103
- h4. Local
137
+ h4(#local). Local
104
138
 
105
139
  @Phony.formatted('41443643532', :format => :local).should == '364 35 32'@
106
140
 
107
141
  @Phony.formatted('493038625454', :format => :local).should == '386 25454'@
108
142
 
109
- h3. Splitting
143
+ h3(#splitting). Splitting
110
144
 
111
145
  @Phony.split('43198110').should == ['43', '1', '98110']@
112
146
 
data/lib/phony.rb CHANGED
@@ -7,8 +7,8 @@ require File.expand_path '../phony/national_splitters/dsl', __FILE__
7
7
  require File.expand_path '../phony/national_splitters/fixed', __FILE__
8
8
  require File.expand_path '../phony/national_splitters/variable', __FILE__
9
9
  require File.expand_path '../phony/national_splitters/regex', __FILE__
10
- require File.expand_path '../phony/national_splitters/none', __FILE__
11
10
  require File.expand_path '../phony/national_splitters/default', __FILE__
11
+ require File.expand_path '../phony/national_splitters/none', __FILE__
12
12
  require File.expand_path '../phony/national_code', __FILE__
13
13
  require File.expand_path '../phony/country', __FILE__
14
14
  require File.expand_path '../phony/country_codes', __FILE__
@@ -63,7 +63,9 @@ module Phony
63
63
  split! phone_number.dup
64
64
  end
65
65
  def split! phone_number
66
- @codes.split phone_number
66
+ parts = @codes.split phone_number
67
+ parts.delete_at 1
68
+ parts
67
69
  end
68
70
 
69
71
  # Formats a E164 number according to local customs.
@@ -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)
13
+ # * fixed: Always splits off a fixed length ndc. (Always use last in a | chain) 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.
@@ -32,7 +32,7 @@ Phony.define do
32
32
  # USA, Canada, etc.
33
33
  #
34
34
  country '1',
35
- fixed(3) >> split(3,4),
35
+ fixed(3, :zero => false) >> split(3,4),
36
36
  invalid_ndcs('911') # /911/ would also work.
37
37
 
38
38
  # Kazakhstan (Republic of) & Russian Federation.
@@ -527,7 +527,7 @@ Phony.define do
527
527
  country '878', todo # Universal Personal Telecommunication Service (UPT)
528
528
  country '879', todo # Reserved for national non-commercial purposes
529
529
 
530
- country '880', todo # Bangladesh (People's Republic of)
530
+ country '880', todo # Bangladesh (People's Republic of)
531
531
  country '881', todo # International Mobile, shared code
532
532
  country '882', todo # International Networks, shared code
533
533
  country '883', todo # -
data/lib/phony/country.rb CHANGED
@@ -27,8 +27,8 @@ module Phony
27
27
  #
28
28
  def split national_number
29
29
  @codes.each do |code|
30
- ndc, *rest = code.split national_number
31
- return ndc ? [ndc, *rest] : rest if rest && !rest.empty?
30
+ zero, ndc, *rest = code.split national_number
31
+ return ndc ? [zero, ndc, *rest] : [zero, nil, *rest] if rest && !rest.empty?
32
32
  end
33
33
  end
34
34
 
@@ -55,7 +55,7 @@ module Phony
55
55
  #
56
56
  #
57
57
  def vanity_to_number vanity_number
58
- ndc, *rest = split vanity_number
58
+ zero, ndc, *rest = split vanity_number
59
59
  "#{ndc}#{Vanity.replace(rest.join)}"
60
60
  end
61
61
 
@@ -10,9 +10,9 @@ 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%s'
14
- @international_relative_format = '00%s%s%s%s'
15
- @national_format = '0%s%s'
13
+ @international_absolute_format = '+%s%s%s'
14
+ @international_relative_format = '00%s%s%s'
15
+ @national_format = '%s%s'
16
16
 
17
17
  @normalize_format = '%s%s'
18
18
  @default_space = ' '
@@ -37,8 +37,8 @@ module Phony
37
37
  # Splits this number into cc, ndc and locally split number parts.
38
38
  #
39
39
  def split number
40
- national_handler, cc, rest = split_cc number
41
- [cc, *national_handler.split(rest)]
40
+ country_handler, cc, rest = split_cc number
41
+ [cc, *country_handler.split(rest)]
42
42
  end
43
43
 
44
44
  def formatted number, options = {}
@@ -46,67 +46,71 @@ module Phony
46
46
  end
47
47
  # Formats country code and national destination code.
48
48
  #
49
- def format_cc_ndc_local format, space, cc, ndc, *parts
50
- cc_ndc = format_cc_ndc(format, space, cc, ndc)
51
- local = \
52
- if parts.empty?
53
- cc_ndc = cc_ndc.slice 0...cc_ndc.rindex(space.to_s)
54
- EMPTY_STRING
55
- else
56
- format_local(space, parts) unless parts.empty?
57
- end
58
- "#{cc_ndc}#{local}"
59
- end
60
- def format_cc_ndc format, space, cc, ndc
49
+ def format_cc_ndc_local format, space, cc, zero, ndc, *parts
50
+ cc_ndc = format_cc_ndc format, space, cc, zero, ndc
51
+ local = if parts.empty?
52
+ cc_ndc = cc_ndc.slice 0...cc_ndc.rindex(space.to_s)
53
+ EMPTY_STRING
54
+ else
55
+ format_local(space, parts) unless parts.empty?
56
+ end
57
+ cc_ndc.empty?? local : "#{cc_ndc}#{space}#{local}"
58
+ end
59
+ def format_cc_ndc format, space, cc, zero, ndc
61
60
  case format
62
61
  when nil, :international_absolute, :international, :+
63
- @international_absolute_format % [cc, space, ndc, space]
62
+ ndc ?
63
+ @international_absolute_format % [cc, space, ndc] :
64
+ @international_absolute_format % [cc, nil, nil]
64
65
  when :international_relative
65
- @international_relative_format % [cc, space, ndc, space]
66
+ ndc ?
67
+ @international_relative_format % [cc, space, ndc] :
68
+ @international_relative_format % [cc, nil, nil]
66
69
  when :national
67
- (ndc.empty? ? EMPTY_STRING : @national_format) % [ndc, space]
70
+ (ndc.empty? ? EMPTY_STRING : @national_format) % [zero, ndc]
68
71
  when :local
69
72
  EMPTY_STRING
70
73
  end
71
74
  end
72
75
  def format_local space, parts_ary
76
+ parts_ary.compact!
73
77
  parts_ary.join space.to_s
74
78
  end
75
79
 
76
80
  #
77
81
  #
78
82
  def service? number
79
- national_handler, cc, rest = split_cc number
80
- national_handler.service? rest
83
+ country_handler, cc, rest = split_cc number
84
+ country_handler.service? rest
81
85
  end
82
86
  def mobile? number
83
- national_handler, cc, rest = split_cc number
84
- national_handler.mobile? rest
87
+ country_handler, cc, rest = split_cc number
88
+ country_handler.mobile? rest
85
89
  end
86
90
  def landline? number
87
- national_handler, cc, rest = split_cc number
88
- national_handler.landline? rest
91
+ country_handler, cc, rest = split_cc number
92
+ country_handler.landline? rest
89
93
  end
90
94
 
91
95
  # Is the given number a vanity number?
92
96
  #
93
97
  def vanity? number
94
- national_handler, _, rest = split_cc number
95
- national_handler.vanity? rest
98
+ country_handler, _, rest = split_cc number
99
+ country_handler.vanity? rest
96
100
  end
97
101
  # Converts a vanity number into a normalized E164 number.
98
102
  #
99
103
  def vanity_to_number vanity_number
100
- national_handler, cc, rest = split_cc vanity_number
101
- "#{cc}#{national_handler.vanity_to_number(rest)}"
104
+ country_handler, cc, rest = split_cc vanity_number
105
+ "#{cc}#{country_handler.vanity_to_number(rest)}"
102
106
  end
103
107
 
104
108
  def split_cc rest
105
109
  presumed_cc = ''
106
110
  1.upto(3) do |i|
107
111
  presumed_cc << rest.slice!(0..0)
108
- national_code_handler = splitter_mapping[i][presumed_cc]
109
- return [national_code_handler, presumed_cc, rest] if national_code_handler
112
+ country_handler = splitter_mapping[i][presumed_cc]
113
+ return [country_handler, presumed_cc, rest] if country_handler
110
114
  end
111
115
  # This line is never reached as CCs are in prefix code.
112
116
  end
data/lib/phony/dsl.rb CHANGED
@@ -54,10 +54,17 @@ module Phony
54
54
  # National matcher & splitters.
55
55
  #
56
56
 
57
+ # By default, a fixed length NDC
58
+ # uses a zero prefix when formatted
59
+ # with format :national.
57
60
  #
61
+ # Options:
62
+ # * length: Length of the fixed length NDC.
63
+ # * options: Set :zero to false to not add a zero on format :national.
58
64
  #
59
- def fixed size
60
- NationalSplitters::Fixed.instance_for size
65
+ def fixed length, options = {}
66
+ options[:zero] = true if options[:zero].nil?
67
+ NationalSplitters::Fixed.instance_for length, options
61
68
  end
62
69
  def none
63
70
  NationalSplitters::None.instance_for
@@ -72,7 +79,8 @@ module Phony
72
79
  NationalSplitters::Variable.new options[:max_length], ndcs
73
80
  end
74
81
  def match regex, options = {}
75
- NationalSplitters::Regex.instance_for regex, options[:on_fail_take]
82
+ on_fail_take = options.delete :on_fail_take
83
+ NationalSplitters::Regex.instance_for regex, options[:on_fail_take], options
76
84
  end
77
85
  def todo
78
86
  none >> NationalSplitters::Default.instance_for
@@ -16,9 +16,9 @@ module Phony
16
16
  # its parts.
17
17
  #
18
18
  def split national_number
19
- ndc, rest = @national_splitter.split national_number.dup
20
- return ndc unless rest
21
- [ndc, *@local_splitter.split(rest)]
19
+ zero, ndc, rest = @national_splitter.split national_number.dup
20
+ return [zero, ndc] unless rest
21
+ [zero, ndc, *@local_splitter.split(rest)]
22
22
  end
23
23
 
24
24
  # Split gets a number without country code and removes a relative zero.
@@ -8,8 +8,8 @@ module Phony
8
8
  @instance ||= new
9
9
  end
10
10
 
11
- def split number
12
- number
11
+ def split national_number
12
+ [nil, nil, national_number]
13
13
  end
14
14
 
15
15
  end
@@ -6,13 +6,19 @@ module Phony
6
6
  #
7
7
  class DSL
8
8
 
9
- # TODO normalize option!
9
+ #
10
10
  #
11
11
  def >> local_splitter
12
- national_code = Phony::NationalCode.new self, local_splitter
13
- Phony::Country.new national_code
12
+ country_for local_splitter, true
14
13
  end
15
14
 
15
+ private
16
+
17
+ def country_for local_splitter, with_zero
18
+ national_code = Phony::NationalCode.new self, local_splitter, with_zero
19
+ Phony::Country.new national_code
20
+ end
21
+
16
22
  end
17
23
 
18
24
  end
@@ -14,19 +14,20 @@ module Phony
14
14
  #
15
15
  # Caches the created splitter for the given format.
16
16
  #
17
- def self.instance_for size
18
- @mapping[size] ||= new(size)
17
+ def self.instance_for *parameters
18
+ @mapping[parameters] ||= new(*parameters)
19
19
  end
20
20
 
21
- def initialize size
21
+ def initialize size, options = {}
22
22
  @size = size
23
+ @zero = options[:zero] == false ? nil : '0'
23
24
  end
24
25
 
25
26
  # Takes a national number and splits it into ndc and rest.
26
27
  #
27
28
  def split national_number
28
- return [national_number] unless @size
29
- [national_number.slice!(0...@size), national_number]
29
+ return [@zero, national_number] unless @size
30
+ [@zero, national_number.slice!(0...@size), national_number]
30
31
  end
31
32
 
32
33
  end
@@ -5,7 +5,7 @@ module Phony
5
5
  # This is a national splitter for countries
6
6
  # which have no NDC / Area Code.
7
7
  #
8
- class None < DSL
8
+ class None < Default
9
9
 
10
10
  # Get a splitter. Caches.
11
11
  #
@@ -13,12 +13,6 @@ module Phony
13
13
  @instance ||= new
14
14
  end
15
15
 
16
- # Takes a national number and splits it into ndc and rest.
17
- #
18
- def split national_number
19
- [nil, national_number]
20
- end
21
-
22
16
  end
23
17
 
24
18
  end
@@ -17,12 +17,12 @@ module Phony
17
17
  #
18
18
  # Note: Not cached.
19
19
  #
20
- def self.instance_for regex, on_fail_take = nil
21
- new regex, on_fail_take
20
+ def self.instance_for regex, on_fail_take = nil, options = {}
21
+ new regex, on_fail_take, options
22
22
  end
23
23
 
24
- def initialize regex, on_fail_take = nil
25
- super on_fail_take
24
+ def initialize regex, on_fail_take = nil, options = {}
25
+ super on_fail_take, options
26
26
 
27
27
  @regex = regex
28
28
  end
@@ -32,14 +32,14 @@ module Phony
32
32
  # Examples
33
33
  # * split '3643533' # => ['364', '35', '33'] # (Switzerland)
34
34
  #
35
- def split number
35
+ def split national_number
36
36
  # Improve matching.
37
37
  #
38
- return [number.slice!(0..$1.size-1), number] if number =~ regex
38
+ return [@zero, national_number.slice!(0..$1.size-1), national_number] if national_number =~ regex
39
39
 
40
40
  # Not found.
41
41
  #
42
- super number
42
+ super national_number
43
43
  end
44
44
 
45
45
  end
@@ -6,8 +6,8 @@ module Phony
6
6
 
7
7
  #
8
8
  #
9
- def initialize fallback, ndcs
10
- super fallback
9
+ def initialize fallback, ndcs, options = {}
10
+ super fallback, options
11
11
  @ndcs = optimize ndcs
12
12
  end
13
13
 
@@ -28,7 +28,7 @@ module Phony
28
28
  #
29
29
  @mapped_ndc_min_length.upto(@mapped_ndc_max_length) do |i|
30
30
  sized_ndcs = @ndcs[i]
31
- return [presumed_code, national_number] unless sized_ndcs && !sized_ndcs.include?(presumed_code)
31
+ return [@zero, presumed_code, national_number] unless sized_ndcs && !sized_ndcs.include?(presumed_code)
32
32
  presumed_code << national_number.slice!(0..0)
33
33
  end
34
34
 
@@ -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', '32', '12', '12', '12']
66
+ Phony.split('4532121212').should == ['45', nil, '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', '112'] # Emergency TODO
113
- Phony.split('3544211234').should == ['354', '421', '1234'] # Keflavík
114
- Phony.split('3544621234').should == ['354', '462', '1234'] # Akureyri
115
- Phony.split('3545511234').should == ['354', '551', '1234'] # Reykjavík
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
116
116
  end
117
117
  it 'handles irish numbers' do
118
118
  Phony.split('35311234567').should == ['353', '1', '123', '4567'] # Dublin, 7 digit subscriber #
@@ -121,7 +121,7 @@ describe 'country descriptions' do
121
121
  Phony.split('353801234567').should == ['353', '80', '123', '4567'] # Mobile
122
122
  Phony.split('353761234567').should == ['353', '76', '123', '4567'] # VoIP
123
123
  Phony.split('353800123456').should == ['353', '800', '123456'] # Freefone
124
- Phony.split('353000123456').should == ['353', '000123456'] # No NDC/local split for unrecognized
124
+ Phony.split('353000123456').should == ['353', nil, '000123456'] # No NDC/local split for unrecognized
125
125
  end
126
126
  it 'handles italian numbers' do
127
127
  Phony.split('3934869528').should == ['39', '3486', '952', '8'] # Mobile
@@ -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','21','23','45','67']
153
- Phony.split('4731234567').should == ['47','31','23','45','67']
154
- Phony.split('4741234567').should == ['47','412','34','567']
155
- Phony.split('4751234567').should == ['47','51','23','45','67']
156
- Phony.split('4761234567').should == ['47','61','23','45','67']
157
- Phony.split('4771234567').should == ['47','71','23','45','67']
158
- Phony.split('4781234567').should == ['47','812','34','567']
159
- Phony.split('4791234567').should == ['47','912','34','567']
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']
160
160
  end
161
161
  it 'handles peruvian numbers' do
162
162
  Phony.split('51112341234').should == ['51', '1', '1234', '1234'] # Lima
@@ -194,7 +194,7 @@ describe 'country descriptions' do
194
194
  Phony.split('21621231234').should == ['216', '2', '123', '1234'] # Bizerte
195
195
  end
196
196
  it 'handles singaporean numbers' do
197
- Phony.split('6561231234').should == ['65', '6123', '1234'] # Fixed line
197
+ Phony.split('6561231234').should == ['65', nil, '6123', '1234'] # Fixed line
198
198
  end
199
199
  it 'handles slovakian numbers' do
200
200
  Phony.split('421912123456').should == ['421', '912', '123456'] # Mobile
@@ -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%s'
15
+ @countries.international_absolute_format = '!!! %s%s%s'
16
16
 
17
17
  @countries.formatted('41443643532', :format => :international).should == '!!! 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%s'
28
+ @countries.international_relative_format = '000 %s%s%s'
29
29
 
30
30
  @countries.formatted('41443643532', :format => :international_relative).should == '000 41 44 364 35 32'
31
31
 
@@ -48,7 +48,7 @@ describe Phony::CountryCodes do
48
48
 
49
49
  describe 'split' do
50
50
  it 'splits correctly' do
51
- @countries.split('41443643532').should == ['41', '44', '364', '35', '32']
51
+ @countries.split('41443643532').should == ['41', '0', '44', '364', '35', '32']
52
52
  end
53
53
  end
54
54
 
@@ -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 == ['112']
15
+ @iceland.split('112').should == [nil, nil, '112']
16
16
  end
17
17
  end
18
18
  end
@@ -28,7 +28,7 @@ describe Phony::Country do
28
28
 
29
29
  describe "split" do
30
30
  it "should handle ZH" do
31
- @switzerland.split('443643532').should == ['44', '364', '35', '32']
31
+ @switzerland.split('443643532').should == ['0', '44', '364', '35', '32']
32
32
  end
33
33
  end
34
34
  describe 'normalize' do
@@ -53,10 +53,10 @@ describe Phony::Country do
53
53
 
54
54
  describe "split" do
55
55
  it "should handle ZH" do
56
- @switzerland.split('443643532').should == ['44', '364', '35', '32']
56
+ @switzerland.split('443643532').should == ['0', '44', '364', '35', '32']
57
57
  end
58
58
  it "should handle 800" do
59
- @switzerland.split('800333666').should == ['800', '333', '666']
59
+ @switzerland.split('800333666').should == ['0', '800', '333', '666']
60
60
  end
61
61
  end
62
62
  end
@@ -6,7 +6,7 @@ describe Phony::DSL do
6
6
 
7
7
  it 'has a todo' do
8
8
  Phony.define do
9
- todo.split("123456789012345").should == ["123456789012345"]
9
+ todo.split("123456789012345").should == [nil, nil, nil, nil, '123456789012345']
10
10
  end
11
11
  end
12
12
 
@@ -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, '112']
15
+ @national.split('112').should == [nil, nil, '112']
16
16
  end
17
17
  end
18
18
  end
@@ -24,10 +24,10 @@ describe Phony::NationalCode do
24
24
  @national = Phony::NationalCode.new national_splitter, local_splitter
25
25
  end
26
26
  it 'splits correctly' do
27
- @national.split('443643532').should == ['44', '364', '35', '32']
27
+ @national.split('443643532').should == ['0', '44', '364', '35', '32']
28
28
  end
29
29
  it 'splits correctly' do
30
- @national.split('44364353').should == ['44', '364', '35', '3']
30
+ @national.split('44364353').should == ['0', '44', '364', '35', '3']
31
31
  end
32
32
  it 'normalizes correctly' do
33
33
  @national.normalize('044364353').should == '44364353'
@@ -44,10 +44,10 @@ describe Phony::NationalCode do
44
44
  @national = Phony::NationalCode.new national_splitter, local_splitter
45
45
  end
46
46
  it 'splits correctly' do
47
- @national.split('142278186').should == ['1', '42', '27', '81', '86']
47
+ @national.split('142278186').should == ['0', '1', '42', '27', '81', '86']
48
48
  end
49
49
  it 'splits correctly' do
50
- @national.split('14227818').should == ['1', '42', '27', '81', '8']
50
+ @national.split('14227818').should == ['0', '1', '42', '27', '81', '8']
51
51
  end
52
52
  it 'normalizes correctly' do
53
53
  @national.normalize('0142278186').should == '142278186'
@@ -16,13 +16,13 @@ describe Phony::NationalSplitters::Fixed do
16
16
  @splitter = Phony::NationalSplitters::Fixed.new 2
17
17
  end
18
18
  it 'splits correctly' do
19
- @splitter.split('443643532').should == ['44', '3643532']
19
+ @splitter.split('443643532').should == ['0', '44', '3643532']
20
20
  end
21
21
  it 'splits correctly even when the number is too long' do
22
- @splitter.split('44364353211').should == ['44', '364353211']
22
+ @splitter.split('44364353211').should == ['0', '44', '364353211']
23
23
  end
24
24
  it 'splits correctly even when the number is too short' do
25
- @splitter.split('443').should == ['44','3']
25
+ @splitter.split('443').should == ['0', '44','3']
26
26
  end
27
27
  end
28
28
  describe 'split' do
@@ -30,13 +30,13 @@ describe Phony::NationalSplitters::Fixed do
30
30
  @splitter = Phony::NationalSplitters::Fixed.new nil
31
31
  end
32
32
  it 'splits correctly' do
33
- @splitter.split('443643532').should == ['443643532']
33
+ @splitter.split('443643532').should == ['0', '443643532']
34
34
  end
35
35
  it 'splits correctly even when the number is too long' do
36
- @splitter.split('44364353211').should == ['44364353211']
36
+ @splitter.split('44364353211').should == ['0', '44364353211']
37
37
  end
38
38
  it 'splits correctly even when the number is too short' do
39
- @splitter.split('443').should == ['443']
39
+ @splitter.split('443').should == ['0', '443']
40
40
  end
41
41
  end
42
42
 
@@ -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, '123456789']
14
+ splitter.split('123456789').should == [nil, nil, '123456789']
15
15
  end
16
16
  end
17
17
 
@@ -15,7 +15,7 @@ describe Phony::NationalSplitters::Regex do
15
15
  context 'with on_fail 2' do
16
16
  let(:splitter) { described_class.instance_for(/^(123)\d+$/, 2) }
17
17
  it 'uses the on_fail_take' do
18
- splitter.split('23456789').should == ['23', '456789']
18
+ splitter.split('23456789').should == ['0', '23', '456789']
19
19
  end
20
20
  end
21
21
  end
@@ -8,16 +8,16 @@ describe Phony::NationalSplitters::Variable do
8
8
  @splitter = Phony::NationalSplitters::Variable.new 4, ['1', '316', '67', '68', '669', '711']
9
9
  end
10
10
  it "handles Vienna" do
11
- @splitter.split('198110').should == ['1', '98110']
11
+ @splitter.split('198110').should == ['0', '1', '98110']
12
12
  end
13
13
  it "handles some mobile services" do
14
- @splitter.split('66914093902').should == ['669', '14093902']
14
+ @splitter.split('66914093902').should == ['0', '669', '14093902']
15
15
  end
16
16
  it "handles Graz" do
17
- @splitter.split('3161234567891').should == ['316', '1234567891']
17
+ @splitter.split('3161234567891').should == ['0', '316', '1234567891']
18
18
  end
19
19
  it "handles Rohrau" do
20
- @splitter.split('2164123456789').should == ['2164', '123456789']
20
+ @splitter.split('2164123456789').should == ['0', '2164', '123456789']
21
21
  end
22
22
  end
23
23
  context 'special handling for using the variable size splitter for Swiss service numbers' do
@@ -25,7 +25,7 @@ describe Phony::NationalSplitters::Variable do
25
25
  @splitter = Phony::NationalSplitters::Variable.new 2, ['800']
26
26
  end
27
27
  it "should handle swiss service numbers" do
28
- @splitter.split('800223344').should == ['800', '223344']
28
+ @splitter.split('800223344').should == ['0', '800', '223344']
29
29
  end
30
30
  end
31
31
  end
@@ -172,6 +172,9 @@ describe Phony do
172
172
  it "should format austrian numbers" do
173
173
  Phony.format('43198110', :format => :national).should == '01 98110'
174
174
  end
175
+ it "should format US numbers without a leading zero" do
176
+ Phony.format('14159224711', :format => :national).should == '415 922 4711'
177
+ end
175
178
  end
176
179
  describe "local" do
177
180
  it "should format swiss numbers" do
@@ -212,8 +215,11 @@ describe Phony do
212
215
  end
213
216
  end
214
217
  context '"unsupported" countries' do
215
- it 'handles normalizing, then formatting' do
216
- Phony.format(Phony.normalize('+880-321 5560 5220')).should == '+880 32155605220'
218
+ it 'handles formatting' do
219
+ Phony.format('88032155605220').should == '+880 32155605220'
220
+ end
221
+ it 'handles normalizing' do
222
+ Phony.normalize('+880-321 5560 5220').should == '88032155605220'
217
223
  end
218
224
  end
219
225
  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.6.8
4
+ version: 1.7.0
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-05-13 00:00:00.000000000 Z
12
+ date: 2012-05-17 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