phony 1.2.10 → 1.2.11
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.
- data/lib/phony.rb +5 -1
- data/lib/phony/countries/all_other.rb +34 -8
- data/lib/phony/countries/norway.rb +11 -0
- data/lib/phony/countries/united_kingdom.rb +5 -3
- data/lib/phony/country.rb +5 -5
- data/lib/phony/local_splitters/fixed.rb +50 -0
- data/lib/phony/local_splitters/regex.rb +59 -0
- data/lib/phony/national_code.rb +2 -1
- data/lib/phony/national_splitters/experimental.rb +17 -0
- data/lib/phony/national_splitters/fixed.rb +1 -1
- data/lib/phony/national_splitters/none.rb +26 -0
- data/spec/lib/phony/country_spec.rb +3 -3
- data/spec/lib/phony/{local_splitter_spec.rb → local_splitters/fixed_spec.rb} +6 -6
- data/spec/lib/phony/local_splitters/regex_spec.rb +49 -0
- data/spec/lib/phony/national_code_spec.rb +4 -4
- data/spec/lib/phony/national_splitters/none_spec.rb +18 -0
- data/spec/lib/phony_spec.rb +10 -0
- metadata +13 -5
- data/lib/phony/local_splitter.rb +0 -46
data/lib/phony.rb
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
# Framework.
|
|
2
2
|
#
|
|
3
3
|
require File.expand_path '../phony/vanity', __FILE__
|
|
4
|
-
require File.expand_path '../phony/
|
|
4
|
+
require File.expand_path '../phony/local_splitters/fixed', __FILE__
|
|
5
|
+
require File.expand_path '../phony/local_splitters/regex', __FILE__
|
|
6
|
+
require File.expand_path '../phony/national_splitters/experimental', __FILE__
|
|
5
7
|
require File.expand_path '../phony/national_splitters/fixed', __FILE__
|
|
6
8
|
require File.expand_path '../phony/national_splitters/variable', __FILE__
|
|
9
|
+
require File.expand_path '../phony/national_splitters/none', __FILE__
|
|
7
10
|
require File.expand_path '../phony/national_code', __FILE__
|
|
8
11
|
require File.expand_path '../phony/country', __FILE__
|
|
9
12
|
|
|
@@ -22,6 +25,7 @@ require File.expand_path '../phony/countries/greece', __FILE__
|
|
|
22
25
|
require File.expand_path '../phony/countries/hungary', __FILE__
|
|
23
26
|
require File.expand_path '../phony/countries/italy', __FILE__
|
|
24
27
|
require File.expand_path '../phony/countries/malaysia', __FILE__
|
|
28
|
+
require File.expand_path '../phony/countries/norway', __FILE__
|
|
25
29
|
require File.expand_path '../phony/countries/netherlands', __FILE__
|
|
26
30
|
require File.expand_path '../phony/countries/peru', __FILE__
|
|
27
31
|
require File.expand_path '../phony/countries/portugal', __FILE__
|
|
@@ -3,6 +3,12 @@
|
|
|
3
3
|
module Phony
|
|
4
4
|
module Countries
|
|
5
5
|
|
|
6
|
+
# Ideas for a DSL:
|
|
7
|
+
#
|
|
8
|
+
# country(fixed(2) >> local([3,2,2]))
|
|
9
|
+
# country(none >> local([2,2,2,2]))
|
|
10
|
+
#
|
|
11
|
+
|
|
6
12
|
# Returns an anonymous fixed size ndc / format country handler.
|
|
7
13
|
#
|
|
8
14
|
def self.fixed size, options = {}
|
|
@@ -10,6 +16,31 @@ module Phony
|
|
|
10
16
|
Phony::Country.fixed options
|
|
11
17
|
end
|
|
12
18
|
|
|
19
|
+
#
|
|
20
|
+
#
|
|
21
|
+
def self.custom *splitters
|
|
22
|
+
codes = []
|
|
23
|
+
splitters.flatten!
|
|
24
|
+
until splitters.empty?
|
|
25
|
+
codes << Phony::NationalCode.new(splitters.shift, splitters.shift)
|
|
26
|
+
end
|
|
27
|
+
Phony::Country.new *codes
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Experimental.
|
|
31
|
+
#
|
|
32
|
+
def self.fix size
|
|
33
|
+
NationalSplitters::Fixed.instance_for size
|
|
34
|
+
end
|
|
35
|
+
def self.none
|
|
36
|
+
NationalSplitters::None.instance_for
|
|
37
|
+
end
|
|
38
|
+
#
|
|
39
|
+
#
|
|
40
|
+
def self.format local
|
|
41
|
+
LocalSplitters::Fixed.instance_for local
|
|
42
|
+
end
|
|
43
|
+
|
|
13
44
|
# TODO
|
|
14
45
|
#
|
|
15
46
|
def self.with_cc cc
|
|
@@ -73,14 +104,9 @@ module Phony
|
|
|
73
104
|
),
|
|
74
105
|
'43' => Countries::Austria,
|
|
75
106
|
'44' => Countries::UnitedKingdom, # TODO United Kingdom of Great Britain and Northern Ireland
|
|
76
|
-
'45' =>
|
|
77
|
-
:local_format => [2, 2, 2],
|
|
78
|
-
:service_ndcs => %w{112 114}
|
|
79
|
-
), # Denmark has no NDC, but 4 groups of 2 digits. I'm faking it here.
|
|
107
|
+
'45' => custom(none >> format([2,2,2,2])), # Denmark
|
|
80
108
|
'46' => Countries::Sweden,
|
|
81
|
-
'47' =>
|
|
82
|
-
:local_format => [4]
|
|
83
|
-
),
|
|
109
|
+
'47' => Countries::Norway, # custom(none >> regexp(/^[1].*$/ => [3], /^[489].*$/ => [3,2,3], :fallback => [2,2,2,2]))
|
|
84
110
|
'48' => fixed(3, # Poland (Republic of)
|
|
85
111
|
:local_format => [3, 3] # Although the NDCs are 2 digits, the representation is 3 digits.
|
|
86
112
|
), # Note: http://wapedia.mobi/en/Telephone_numbers_in_Poland, mobile not yet correct
|
|
@@ -223,7 +249,7 @@ module Phony
|
|
|
223
249
|
'350' => fixed(2), # Gibraltar
|
|
224
250
|
'351' => Countries::Portugal, # Portugal
|
|
225
251
|
'352' => fixed(2), # Luxembourg
|
|
226
|
-
'353' => fixed(2), # Ireland
|
|
252
|
+
'353' => fixed(2), # Ireland (0-3-4)
|
|
227
253
|
'354' => fixed(2), # Iceland
|
|
228
254
|
'355' => fixed(2), # Albania
|
|
229
255
|
'356' => fixed(2), # Malta
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Norway. Complete plan available here:
|
|
2
|
+
# http://www.npt.no/pt_internet/e_164/nummerplaner.html
|
|
3
|
+
#
|
|
4
|
+
# (5-digit special numbers and 01 is missing)
|
|
5
|
+
#
|
|
6
|
+
handler = Phony::NationalCode.new(
|
|
7
|
+
Phony::NationalSplitters::None.instance_for,
|
|
8
|
+
Phony::LocalSplitters::Regex.instance_for(/^[1].*$/ => [3], /^[489].*$/ => [3,2,3], :fallback => [2,2,2,2])
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
Phony::Countries::Norway = Phony::Country.new handler
|
|
@@ -8,6 +8,8 @@
|
|
|
8
8
|
# To reflect this different formatting, we need to install all handlers in a row.
|
|
9
9
|
# First, the area codes of length 2, without a fallback length (since this captures all), but with a nil fallback length.
|
|
10
10
|
#
|
|
11
|
+
# TODO Implement and use length-based splitter.
|
|
12
|
+
#
|
|
11
13
|
handlers = []
|
|
12
14
|
|
|
13
15
|
area_code_2_national = Phony::NationalSplitters::Variable.new nil, :landline => [
|
|
@@ -17,7 +19,7 @@ area_code_2_national = Phony::NationalSplitters::Variable.new nil, :landline =>
|
|
|
17
19
|
'28', # Northern Ireland
|
|
18
20
|
'29', # Cardiff
|
|
19
21
|
]
|
|
20
|
-
area_code_2_local = Phony::
|
|
22
|
+
area_code_2_local = Phony::LocalSplitters::Fixed.instance_for [4, 4]
|
|
21
23
|
handlers << Phony::NationalCode.new(area_code_2_national, area_code_2_local)
|
|
22
24
|
|
|
23
25
|
area_code_3_national = Phony::NationalSplitters::Variable.new nil, :landline => [
|
|
@@ -54,7 +56,7 @@ area_code_3_national = Phony::NationalSplitters::Variable.new nil, :landline =>
|
|
|
54
56
|
'909', # Sexual entertainment services
|
|
55
57
|
'982', # Sexual entertainment services
|
|
56
58
|
]
|
|
57
|
-
area_code_3_local = Phony::
|
|
59
|
+
area_code_3_local = Phony::LocalSplitters::Fixed.instance_for [3, 4]
|
|
58
60
|
handlers << Phony::NationalCode.new(area_code_3_national, area_code_3_local)
|
|
59
61
|
|
|
60
62
|
# 6 is the fallback length.
|
|
@@ -99,7 +101,7 @@ area_code_45_national = Phony::NationalSplitters::Variable.new 6, :landline => [
|
|
|
99
101
|
'17687', # Keswick
|
|
100
102
|
'19467', # Gosforth
|
|
101
103
|
]
|
|
102
|
-
area_code_45_local = Phony::
|
|
104
|
+
area_code_45_local = Phony::LocalSplitters::Fixed.instance_for [6]
|
|
103
105
|
handlers << Phony::NationalCode.new(area_code_45_national, area_code_45_local)
|
|
104
106
|
|
|
105
107
|
Phony::Countries::UnitedKingdom = Phony::Country.new *handlers
|
data/lib/phony/country.rb
CHANGED
|
@@ -75,18 +75,18 @@ module Phony
|
|
|
75
75
|
|
|
76
76
|
if ndc_mapping[:service]
|
|
77
77
|
service_national_splitter = Phony::NationalSplitters::Variable.new nil, :service => ndc_mapping[:service]
|
|
78
|
-
service_local_splitter = Phony::
|
|
78
|
+
service_local_splitter = Phony::LocalSplitters::Fixed.instance_for options[:service_local_format] || [3, 6]
|
|
79
79
|
codes << Phony::NationalCode.new(service_national_splitter, service_local_splitter, normalize)
|
|
80
80
|
end
|
|
81
81
|
if ndc_mapping[:mobile]
|
|
82
82
|
mobile_local_format = options[:mobile_local_format] || options[:local_format] || [9]
|
|
83
83
|
mobile_national_splitter = Phony::NationalSplitters::Variable.new nil, :mobile => ndc_mapping[:mobile]
|
|
84
|
-
mobile_local_splitter = Phony::
|
|
84
|
+
mobile_local_splitter = Phony::LocalSplitters::Fixed.instance_for mobile_local_format
|
|
85
85
|
codes << Phony::NationalCode.new(mobile_national_splitter, mobile_local_splitter, normalize)
|
|
86
86
|
end
|
|
87
87
|
|
|
88
88
|
national_splitter = Phony::NationalSplitters::Variable.new ndc_fallback_length, ndc_mapping
|
|
89
|
-
local_splitter = Phony::
|
|
89
|
+
local_splitter = Phony::LocalSplitters::Fixed.instance_for options[:local_format] || [3, 2, 2]
|
|
90
90
|
codes << Phony::NationalCode.new(national_splitter, local_splitter, normalize)
|
|
91
91
|
|
|
92
92
|
new *codes
|
|
@@ -114,12 +114,12 @@ module Phony
|
|
|
114
114
|
if service_ndcs
|
|
115
115
|
service_local_format = options[:service_local_format] || local_format || [3, 3]
|
|
116
116
|
service_national_splitter = Phony::NationalSplitters::Variable.new nil, :service => service_ndcs
|
|
117
|
-
service_local_splitter = Phony::
|
|
117
|
+
service_local_splitter = Phony::LocalSplitters::Fixed.instance_for service_local_format
|
|
118
118
|
codes << Phony::NationalCode.new(service_national_splitter, service_local_splitter, normalize)
|
|
119
119
|
end
|
|
120
120
|
|
|
121
121
|
national_splitter = Phony::NationalSplitters::Fixed.new ndc_length
|
|
122
|
-
local_splitter = Phony::
|
|
122
|
+
local_splitter = Phony::LocalSplitters::Fixed.instance_for local_format || [3, 2, 2]
|
|
123
123
|
codes << Phony::NationalCode.new(national_splitter, local_splitter, normalize)
|
|
124
124
|
|
|
125
125
|
new *codes
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
module Phony
|
|
2
|
+
|
|
3
|
+
module LocalSplitters
|
|
4
|
+
|
|
5
|
+
# Local splitter class to split the last part of
|
|
6
|
+
# a number, i.e. minus cc or ndc.
|
|
7
|
+
#
|
|
8
|
+
# Countries can create new instances according to their needs.
|
|
9
|
+
#
|
|
10
|
+
# Note: Countries should use instance_for
|
|
11
|
+
# to avoid getting new local splitter instances.
|
|
12
|
+
#
|
|
13
|
+
class Fixed
|
|
14
|
+
|
|
15
|
+
@mapping = {}
|
|
16
|
+
|
|
17
|
+
# Get a splitter for the given format.
|
|
18
|
+
#
|
|
19
|
+
# Caches the created splitter for the given format.
|
|
20
|
+
#
|
|
21
|
+
def self.instance_for format = nil
|
|
22
|
+
@mapping[format] ||= new(format)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Initialize with a local format, like [3, 2, 2] (also the default).
|
|
26
|
+
#
|
|
27
|
+
# The format [3, 2, 2] splits a number like '3332222' into ['333', '22', '22'].
|
|
28
|
+
#
|
|
29
|
+
def initialize format = nil
|
|
30
|
+
@format = format || [3, 2, 2]
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Split a local number according to an assumed country specific format.
|
|
34
|
+
#
|
|
35
|
+
# Examples
|
|
36
|
+
# * split '3643533' # => ['364', '35', '33'] # (Switzerland)
|
|
37
|
+
#
|
|
38
|
+
def split number
|
|
39
|
+
@format.inject([]) do |result, size|
|
|
40
|
+
result << number.slice!(0..size-1)
|
|
41
|
+
return result if number.empty?
|
|
42
|
+
result
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
module Phony
|
|
2
|
+
|
|
3
|
+
module LocalSplitters
|
|
4
|
+
|
|
5
|
+
# Local splitter class to split the last part of
|
|
6
|
+
# a number, i.e. minus cc or ndc.
|
|
7
|
+
#
|
|
8
|
+
# Countries can create new instances according to their needs.
|
|
9
|
+
#
|
|
10
|
+
# Note: Countries should use instance_for
|
|
11
|
+
# to avoid getting new local splitter instances.
|
|
12
|
+
#
|
|
13
|
+
class Regex
|
|
14
|
+
|
|
15
|
+
attr_reader :fallback, :mapping
|
|
16
|
+
|
|
17
|
+
# Get a splitter for the given format.
|
|
18
|
+
#
|
|
19
|
+
# Note: Not cached.
|
|
20
|
+
#
|
|
21
|
+
def self.instance_for mapping
|
|
22
|
+
new mapping
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Initialize with a regex => format mapping.
|
|
26
|
+
#
|
|
27
|
+
def initialize mapping
|
|
28
|
+
@fallback = mapping.delete(:fallback) || [12]
|
|
29
|
+
@mapping = mapping
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Split a local number according to an assumed country specific format.
|
|
33
|
+
#
|
|
34
|
+
# Examples
|
|
35
|
+
# * split '3643533' # => ['364', '35', '33'] # (Switzerland)
|
|
36
|
+
#
|
|
37
|
+
def split number
|
|
38
|
+
mapping.each do |regex, format|
|
|
39
|
+
next unless number =~ regex
|
|
40
|
+
return split_with(number, format)
|
|
41
|
+
end
|
|
42
|
+
split_with number, fallback
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
def split_with number, format
|
|
48
|
+
format.inject([]) do |result, size|
|
|
49
|
+
result << number.slice!(0..size-1)
|
|
50
|
+
return result if number.empty?
|
|
51
|
+
result
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
end
|
data/lib/phony/national_code.rb
CHANGED
|
@@ -20,7 +20,8 @@ module Phony
|
|
|
20
20
|
def split national_number
|
|
21
21
|
ndc, rest = @national_splitter.split national_number.dup
|
|
22
22
|
return ndc unless rest
|
|
23
|
-
|
|
23
|
+
local = @local_splitter.split rest
|
|
24
|
+
ndc ? [ndc, *local] : local
|
|
24
25
|
end
|
|
25
26
|
|
|
26
27
|
# Split gets a number without country code and removes a relative zero.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
module Phony
|
|
2
|
+
|
|
3
|
+
module NationalSplitters
|
|
4
|
+
|
|
5
|
+
# This is a national splitter for countries
|
|
6
|
+
# which have no NDC / Area Code.
|
|
7
|
+
#
|
|
8
|
+
class None < Experimental
|
|
9
|
+
|
|
10
|
+
# Get a splitter. Caches.
|
|
11
|
+
#
|
|
12
|
+
def self.instance_for(*)
|
|
13
|
+
@instance ||= new
|
|
14
|
+
end
|
|
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
|
+
end
|
|
23
|
+
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
end
|
|
@@ -53,7 +53,7 @@ describe Phony::Country do
|
|
|
53
53
|
context "without special cases" do
|
|
54
54
|
before(:each) do
|
|
55
55
|
national_splitter = Phony::NationalSplitters::Variable.new 4, { :normal => ['44'] }
|
|
56
|
-
local_splitter = Phony::
|
|
56
|
+
local_splitter = Phony::LocalSplitters::Fixed.instance_for [3, 2, 2]
|
|
57
57
|
national_code = Phony::NationalCode.new national_splitter, local_splitter
|
|
58
58
|
|
|
59
59
|
@switzerland = Phony::Country.new national_code
|
|
@@ -74,11 +74,11 @@ describe Phony::Country do
|
|
|
74
74
|
context "without special cases" do
|
|
75
75
|
before(:each) do
|
|
76
76
|
special_national_splitter = Phony::NationalSplitters::Variable.new nil, { :service => ['800'] }
|
|
77
|
-
special_local_splitter = Phony::
|
|
77
|
+
special_local_splitter = Phony::LocalSplitters::Fixed.instance_for [3, 3]
|
|
78
78
|
special_code = Phony::NationalCode.new special_national_splitter, special_local_splitter
|
|
79
79
|
|
|
80
80
|
national_splitter = Phony::NationalSplitters::Variable.new 4, { :normal => ['44'] }
|
|
81
|
-
local_splitter = Phony::
|
|
81
|
+
local_splitter = Phony::LocalSplitters::Fixed.instance_for [3, 2, 2]
|
|
82
82
|
national_code = Phony::NationalCode.new national_splitter, local_splitter
|
|
83
83
|
|
|
84
84
|
@switzerland = Phony::Country.new special_code, national_code
|
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
2
|
|
|
3
|
-
describe Phony::
|
|
3
|
+
describe Phony::LocalSplitters::Fixed do
|
|
4
4
|
|
|
5
5
|
describe 'instance_for' do
|
|
6
6
|
it 'caches' do
|
|
7
|
-
|
|
7
|
+
described_class.instance_for([3,2,2]).should equal(described_class.instance_for([3,2,2]))
|
|
8
8
|
end
|
|
9
9
|
it 'caches correctly' do
|
|
10
|
-
|
|
10
|
+
described_class.instance_for([1,2,3]).should_not equal(described_class.instance_for([9,9,9]))
|
|
11
11
|
end
|
|
12
12
|
it 'caches correctly' do
|
|
13
|
-
|
|
13
|
+
described_class.instance_for.should equal(described_class.instance_for)
|
|
14
14
|
end
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
describe 'split' do
|
|
18
18
|
context "without format" do
|
|
19
19
|
before(:each) do
|
|
20
|
-
@splitter =
|
|
20
|
+
@splitter = described_class.new
|
|
21
21
|
end
|
|
22
22
|
it 'splits correctly' do
|
|
23
23
|
@splitter.split('3643532').should == ['364','35','32']
|
|
@@ -31,7 +31,7 @@ describe Phony::LocalSplitter do
|
|
|
31
31
|
end
|
|
32
32
|
context "with format" do
|
|
33
33
|
before(:each) do
|
|
34
|
-
@splitter =
|
|
34
|
+
@splitter = described_class.new [3, 2, 2]
|
|
35
35
|
end
|
|
36
36
|
it 'splits correctly' do
|
|
37
37
|
@splitter.split('3643532').should == ['364','35','32']
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Phony::LocalSplitters::Regex do
|
|
4
|
+
|
|
5
|
+
describe 'instance_for' do
|
|
6
|
+
it 'does not cache' do
|
|
7
|
+
described_class.instance_for({}).should_not equal(described_class.instance_for({}))
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
describe 'split' do
|
|
12
|
+
before(:each) do
|
|
13
|
+
# Norway as example.
|
|
14
|
+
#
|
|
15
|
+
@splitter = described_class.instance_for /^[489].*$/ => [3,2,3], :fallback => [2,2,2,2]
|
|
16
|
+
end
|
|
17
|
+
it 'splits a number correctly' do
|
|
18
|
+
@splitter.split('21234567').should == ['21','23','45','67']
|
|
19
|
+
end
|
|
20
|
+
it 'splits a number correctly' do
|
|
21
|
+
@splitter.split('31234567').should == ['31','23','45','67']
|
|
22
|
+
end
|
|
23
|
+
it 'splits a number correctly' do
|
|
24
|
+
@splitter.split('41234567').should == ['412','34','567']
|
|
25
|
+
end
|
|
26
|
+
it 'splits a number correctly' do
|
|
27
|
+
@splitter.split('51234567').should == ['51','23','45','67']
|
|
28
|
+
end
|
|
29
|
+
it 'splits a number correctly' do
|
|
30
|
+
@splitter.split('61234567').should == ['61','23','45','67']
|
|
31
|
+
end
|
|
32
|
+
it 'splits a number correctly' do
|
|
33
|
+
@splitter.split('71234567').should == ['71','23','45','67']
|
|
34
|
+
end
|
|
35
|
+
it 'splits a number correctly' do
|
|
36
|
+
@splitter.split('81234567').should == ['812','34','567']
|
|
37
|
+
end
|
|
38
|
+
it 'splits a number correctly' do
|
|
39
|
+
@splitter.split('91234567').should == ['912','34','567']
|
|
40
|
+
end
|
|
41
|
+
it 'splits it fast' do
|
|
42
|
+
performance_of { @splitter.split('21234567').should == ['21','23','45','67'] }.should < 0.00005
|
|
43
|
+
end
|
|
44
|
+
it 'splits it fast' do
|
|
45
|
+
performance_of { @splitter.split('91234567').should == ['912','34','567'] }.should < 0.00004
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
end
|
|
@@ -6,7 +6,7 @@ describe Phony::NationalCode do
|
|
|
6
6
|
context 'with fixed ndc (Swiss)' do
|
|
7
7
|
before(:each) do
|
|
8
8
|
national_splitter = Phony::NationalSplitters::Fixed.instance_for 2
|
|
9
|
-
local_splitter = Phony::
|
|
9
|
+
local_splitter = Phony::LocalSplitters::Fixed.instance_for [3, 2, 2]
|
|
10
10
|
|
|
11
11
|
@national = Phony::NationalCode.new national_splitter, local_splitter
|
|
12
12
|
end
|
|
@@ -26,7 +26,7 @@ describe Phony::NationalCode do
|
|
|
26
26
|
context 'with fixed ndc (French)' do
|
|
27
27
|
before(:each) do
|
|
28
28
|
national_splitter = Phony::NationalSplitters::Fixed.instance_for 1
|
|
29
|
-
local_splitter = Phony::
|
|
29
|
+
local_splitter = Phony::LocalSplitters::Fixed.instance_for [2, 2, 2, 2]
|
|
30
30
|
|
|
31
31
|
@national = Phony::NationalCode.new national_splitter, local_splitter
|
|
32
32
|
end
|
|
@@ -55,7 +55,7 @@ describe Phony::NationalCode do
|
|
|
55
55
|
context 'true' do
|
|
56
56
|
before(:each) do
|
|
57
57
|
national_splitter = Phony::NationalSplitters::Fixed.instance_for 2
|
|
58
|
-
local_splitter = Phony::
|
|
58
|
+
local_splitter = Phony::LocalSplitters::Fixed.instance_for [3, 2, 2]
|
|
59
59
|
@national = Phony::NationalCode.new national_splitter, local_splitter, true
|
|
60
60
|
end
|
|
61
61
|
it 'normalizes a swiss case correctly' do
|
|
@@ -65,7 +65,7 @@ describe Phony::NationalCode do
|
|
|
65
65
|
context 'nil (true)' do
|
|
66
66
|
before(:each) do
|
|
67
67
|
national_splitter = Phony::NationalSplitters::Fixed.instance_for 2
|
|
68
|
-
local_splitter = Phony::
|
|
68
|
+
local_splitter = Phony::LocalSplitters::Fixed.instance_for [3, 2, 2]
|
|
69
69
|
@national = Phony::NationalCode.new national_splitter, local_splitter, nil
|
|
70
70
|
end
|
|
71
71
|
it 'normalizes a swiss case correctly' do
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Phony::NationalSplitters::None do
|
|
4
|
+
|
|
5
|
+
describe 'instance_for' do
|
|
6
|
+
it 'caches' do
|
|
7
|
+
described_class.instance_for.should equal(described_class.instance_for)
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
describe 'split' do
|
|
12
|
+
let(:splitter) { described_class.instance_for }
|
|
13
|
+
it 'splits correctly into ndc and rest' do
|
|
14
|
+
splitter.split('123456789').should == [nil, '123456789']
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
end
|
data/spec/lib/phony_spec.rb
CHANGED
|
@@ -68,6 +68,16 @@ describe Phony do
|
|
|
68
68
|
Phony.split('60312345678').should == ['60', '3', '12345678'] # Kuala Lumpur
|
|
69
69
|
Phony.split('60212345678').should == ['60', '2', '12345678'] # Singapore
|
|
70
70
|
end
|
|
71
|
+
it 'handles norwegian numbers' do
|
|
72
|
+
Phony.split('4721234567').should == ['47','21','23','45','67']
|
|
73
|
+
Phony.split('4731234567').should == ['47','31','23','45','67']
|
|
74
|
+
Phony.split('4741234567').should == ['47','412','34','567']
|
|
75
|
+
Phony.split('4751234567').should == ['47','51','23','45','67']
|
|
76
|
+
Phony.split('4761234567').should == ['47','61','23','45','67']
|
|
77
|
+
Phony.split('4771234567').should == ['47','71','23','45','67']
|
|
78
|
+
Phony.split('4781234567').should == ['47','812','34','567']
|
|
79
|
+
Phony.split('4791234567').should == ['47','912','34','567']
|
|
80
|
+
end
|
|
71
81
|
it 'handles peruvian numbers' do
|
|
72
82
|
Phony.split('51112341234').should == ['51', '1', '1234', '1234'] # Lima
|
|
73
83
|
Phony.split('51912341234').should == ['51', '9', '1234', '1234'] # mobile
|
metadata
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
name: phony
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease:
|
|
5
|
-
version: 1.2.
|
|
5
|
+
version: 1.2.11
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
8
8
|
- Florian Hanke
|
|
@@ -10,7 +10,7 @@ autorequire:
|
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
12
|
|
|
13
|
-
date: 2011-02-
|
|
13
|
+
date: 2011-02-22 00:00:00 +01:00
|
|
14
14
|
default_executable:
|
|
15
15
|
dependencies: []
|
|
16
16
|
|
|
@@ -35,6 +35,7 @@ files:
|
|
|
35
35
|
- lib/phony/countries/italy.rb
|
|
36
36
|
- lib/phony/countries/malaysia.rb
|
|
37
37
|
- lib/phony/countries/netherlands.rb
|
|
38
|
+
- lib/phony/countries/norway.rb
|
|
38
39
|
- lib/phony/countries/peru.rb
|
|
39
40
|
- lib/phony/countries/portugal.rb
|
|
40
41
|
- lib/phony/countries/romania.rb
|
|
@@ -43,9 +44,12 @@ files:
|
|
|
43
44
|
- lib/phony/countries/united_kingdom.rb
|
|
44
45
|
- lib/phony/country.rb
|
|
45
46
|
- lib/phony/country_codes.rb
|
|
46
|
-
- lib/phony/
|
|
47
|
+
- lib/phony/local_splitters/fixed.rb
|
|
48
|
+
- lib/phony/local_splitters/regex.rb
|
|
47
49
|
- lib/phony/national_code.rb
|
|
50
|
+
- lib/phony/national_splitters/experimental.rb
|
|
48
51
|
- lib/phony/national_splitters/fixed.rb
|
|
52
|
+
- lib/phony/national_splitters/none.rb
|
|
49
53
|
- lib/phony/national_splitters/variable.rb
|
|
50
54
|
- lib/phony/vanity.rb
|
|
51
55
|
- lib/phony.rb
|
|
@@ -61,9 +65,11 @@ files:
|
|
|
61
65
|
- spec/lib/phony/countries/united_kingdom_spec.rb
|
|
62
66
|
- spec/lib/phony/country_codes_spec.rb
|
|
63
67
|
- spec/lib/phony/country_spec.rb
|
|
64
|
-
- spec/lib/phony/
|
|
68
|
+
- spec/lib/phony/local_splitters/fixed_spec.rb
|
|
69
|
+
- spec/lib/phony/local_splitters/regex_spec.rb
|
|
65
70
|
- spec/lib/phony/national_code_spec.rb
|
|
66
71
|
- spec/lib/phony/national_splitters/fixed_spec.rb
|
|
72
|
+
- spec/lib/phony/national_splitters/none_spec.rb
|
|
67
73
|
- spec/lib/phony/national_splitters/variable_spec.rb
|
|
68
74
|
- spec/lib/phony/vanity_spec.rb
|
|
69
75
|
- spec/lib/phony_spec.rb
|
|
@@ -107,9 +113,11 @@ test_files:
|
|
|
107
113
|
- spec/lib/phony/countries/united_kingdom_spec.rb
|
|
108
114
|
- spec/lib/phony/country_codes_spec.rb
|
|
109
115
|
- spec/lib/phony/country_spec.rb
|
|
110
|
-
- spec/lib/phony/
|
|
116
|
+
- spec/lib/phony/local_splitters/fixed_spec.rb
|
|
117
|
+
- spec/lib/phony/local_splitters/regex_spec.rb
|
|
111
118
|
- spec/lib/phony/national_code_spec.rb
|
|
112
119
|
- spec/lib/phony/national_splitters/fixed_spec.rb
|
|
120
|
+
- spec/lib/phony/national_splitters/none_spec.rb
|
|
113
121
|
- spec/lib/phony/national_splitters/variable_spec.rb
|
|
114
122
|
- spec/lib/phony/vanity_spec.rb
|
|
115
123
|
- spec/lib/phony_spec.rb
|
data/lib/phony/local_splitter.rb
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
module Phony
|
|
2
|
-
|
|
3
|
-
# Local splitter class to split the last part of
|
|
4
|
-
# a number, i.e. minus cc or ndc.
|
|
5
|
-
#
|
|
6
|
-
# Countries can create new instances according to their needs.
|
|
7
|
-
#
|
|
8
|
-
# Note: Countries should use instance_for
|
|
9
|
-
# to avoid getting new local splitter instances.
|
|
10
|
-
#
|
|
11
|
-
class LocalSplitter
|
|
12
|
-
|
|
13
|
-
@mapping = {}
|
|
14
|
-
|
|
15
|
-
# Get a splitter for the given format.
|
|
16
|
-
#
|
|
17
|
-
# Caches the created splitter for the given format.
|
|
18
|
-
#
|
|
19
|
-
def self.instance_for format = nil
|
|
20
|
-
@mapping[format] ||= new(format)
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
# Initialize with a local format, like [3, 2, 2] (also the default).
|
|
24
|
-
#
|
|
25
|
-
# The format [3, 2, 2] splits a number like '3332222' into ['333', '22', '22'].
|
|
26
|
-
#
|
|
27
|
-
def initialize format = nil
|
|
28
|
-
@format = format || [3, 2, 2]
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
# Split a local number according to an assumed country specific format.
|
|
32
|
-
#
|
|
33
|
-
# Examples
|
|
34
|
-
# * split '3643533' # => ['364', '35', '33'] # (Switzerland)
|
|
35
|
-
#
|
|
36
|
-
def split number
|
|
37
|
-
@format.inject([]) do |result, size|
|
|
38
|
-
result << number.slice!(0..size-1)
|
|
39
|
-
return result if number.empty?
|
|
40
|
-
result
|
|
41
|
-
end
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
end
|