phony 1.0.1 → 1.1.0
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/README.textile +14 -2
- data/lib/phony.rb +58 -463
- data/lib/phony/countries/all_other.rb +417 -0
- data/lib/phony/countries/austria.rb +69 -0
- data/lib/phony/countries/egypt.rb +40 -0
- data/lib/phony/countries/germany.rb +156 -0
- data/lib/phony/countries/greece.rb +30 -0
- data/lib/phony/countries/hungary.rb +23 -0
- data/lib/phony/countries/italy.rb +105 -0
- data/lib/phony/country.rb +102 -0
- data/lib/phony/country_codes.rb +102 -0
- data/lib/phony/local_splitter.rb +46 -0
- data/lib/phony/national_code.rb +27 -0
- data/lib/phony/national_splitters/fixed.rb +36 -0
- data/lib/phony/national_splitters/variable.rb +64 -0
- data/lib/phony/vanity.rb +35 -0
- data/spec/lib/phony/countries/austria_spec.rb +24 -0
- data/spec/lib/phony/countries/egypt_spec.rb +18 -0
- data/spec/lib/phony/countries/germany_spec.rb +24 -0
- data/spec/lib/phony/countries/greece_spec.rb +18 -0
- data/spec/lib/phony/countries/hungary_spec.rb +18 -0
- data/spec/lib/phony/countries/switzerland_spec.rb +18 -0
- data/spec/lib/phony/country_codes_spec.rb +110 -0
- data/spec/lib/phony/country_spec.rb +91 -0
- data/spec/lib/phony/local_splitter_spec.rb +48 -0
- data/spec/lib/phony/national_code_spec.rb +36 -0
- data/spec/lib/phony/national_splitters/fixed_spec.rb +43 -0
- data/spec/lib/phony/national_splitters/variable_spec.rb +35 -0
- data/spec/lib/phony/vanity_spec.rb +34 -0
- data/spec/lib/phony_spec.rb +117 -238
- metadata +45 -21
- data/lib/ndc/austria.rb +0 -69
- data/lib/ndc/fixed_size.rb +0 -113
- data/lib/ndc/germany.rb +0 -157
- data/lib/ndc/prefix.rb +0 -67
- data/lib/ndc/splitter.rb +0 -81
- data/spec/lib/ndc/austria_spec.rb +0 -40
- data/spec/lib/ndc/fixed_size_spec.rb +0 -65
- data/spec/lib/ndc/germany_spec.rb +0 -40
- data/spec/lib/ndc/prefix_spec.rb +0 -25
- data/spec/lib/ndc/splitter_spec.rb +0 -59
data/lib/ndc/splitter.rb
DELETED
@@ -1,81 +0,0 @@
|
|
1
|
-
# Splits a national number into a fixed size NDC and rest.
|
2
|
-
#
|
3
|
-
module Phony
|
4
|
-
module NDC
|
5
|
-
class Splitter
|
6
|
-
|
7
|
-
# Sets the local grouping format.
|
8
|
-
#
|
9
|
-
# Examples
|
10
|
-
# * local 3, 2, 2 # Switzerland, 364 35 33, thus: 3-2-2.
|
11
|
-
# * local 2, 2, 2, 2 # France, 12 34 56 78, thus: 2-2-2-2.
|
12
|
-
#
|
13
|
-
def self.local *split_sizes
|
14
|
-
@split_sizes = split_sizes.flatten
|
15
|
-
format((['%s']*@split_sizes.size).join('%s'))
|
16
|
-
end
|
17
|
-
|
18
|
-
# Define a format for the country's local format.
|
19
|
-
#
|
20
|
-
# Examples
|
21
|
-
# * format '%s%s%s%s%s' # Switzerland, spaces between the groups, e.g. 364˽35˽32
|
22
|
-
#
|
23
|
-
def self.format format
|
24
|
-
@format = format
|
25
|
-
@format_with_ndc = ndc_format + @format
|
26
|
-
end
|
27
|
-
|
28
|
-
# Define a format for the country's national format.
|
29
|
-
#
|
30
|
-
# Default is NN followed by a space character.
|
31
|
-
#
|
32
|
-
def self.ndc_format
|
33
|
-
'%s%s'
|
34
|
-
end
|
35
|
-
|
36
|
-
# Split an E164 number without country code into its NDC-Local parts.
|
37
|
-
#
|
38
|
-
# Examples
|
39
|
-
# * split '443643533' # => ['44', '364', '35', '33'] # (Switzerland)
|
40
|
-
#
|
41
|
-
def self.split number
|
42
|
-
ndc_part = split_ndc(number)
|
43
|
-
[ndc_part, split_local(ndc_part.pop)].flatten
|
44
|
-
end
|
45
|
-
|
46
|
-
# Split a local number according to an assumed country specific format.
|
47
|
-
#
|
48
|
-
# Examples
|
49
|
-
# * split '3643533' # => ['364', '35', '33'] # (Switzerland)
|
50
|
-
#
|
51
|
-
def self.split_local number
|
52
|
-
local = []
|
53
|
-
@split_sizes.each do |size|
|
54
|
-
local << number.slice!(0..size-1)
|
55
|
-
break if number.empty?
|
56
|
-
end
|
57
|
-
local
|
58
|
-
end
|
59
|
-
|
60
|
-
# Formats the given E164 Number (NDC-Local without CC) according to the country specific format / ndcs splitting.
|
61
|
-
#
|
62
|
-
def self.formatted number, space = ' '
|
63
|
-
formatted_with_spaces @format_with_ndc, split(number), space
|
64
|
-
end
|
65
|
-
|
66
|
-
# Formats the given local number according to the country specific format.
|
67
|
-
#
|
68
|
-
def self.locally_formatted number, space = ' '
|
69
|
-
formatted_with_spaces @format, split_local(number), space
|
70
|
-
end
|
71
|
-
|
72
|
-
def self.formatted_with_spaces format, split_number, space
|
73
|
-
space ||= ' '
|
74
|
-
number_and_spaces = (split_number).zip([space]*split_number.size).flatten
|
75
|
-
format % number_and_spaces
|
76
|
-
end
|
77
|
-
|
78
|
-
end
|
79
|
-
|
80
|
-
end
|
81
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Phony::NDC::Austria do
|
4
|
-
|
5
|
-
attr_reader :splitter
|
6
|
-
before(:each) do
|
7
|
-
@splitter = Phony::NDC::Austria
|
8
|
-
end
|
9
|
-
|
10
|
-
describe "split" do
|
11
|
-
it "should handle Vienna" do
|
12
|
-
splitter.split('198110').should == ['1', '98110']
|
13
|
-
end
|
14
|
-
it "should handle some mobile services" do
|
15
|
-
splitter.split('66914093902').should == ['669', '14093902']
|
16
|
-
end
|
17
|
-
it "should handle Graz" do
|
18
|
-
splitter.split('3161234567891').should == ['316', '1234567891']
|
19
|
-
end
|
20
|
-
it "should handle Rohrau" do
|
21
|
-
splitter.split('2164123456789').should == ['2164', '123456789']
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
describe "formatted" do
|
26
|
-
it "should format Vienna" do
|
27
|
-
splitter.formatted('198110').should == '1 98110'
|
28
|
-
end
|
29
|
-
it "should format some mobile services" do
|
30
|
-
splitter.formatted('671234567891').should == '67 1234567891'
|
31
|
-
end
|
32
|
-
it "should format Graz" do
|
33
|
-
splitter.formatted('316123456789').should == '316 123456789'
|
34
|
-
end
|
35
|
-
it "should format Rohrau" do
|
36
|
-
splitter.formatted('216412345678').should == '2164 12345678'
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
end
|
@@ -1,65 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Phony::NDC::FixedSize do
|
4
|
-
|
5
|
-
attr_reader :splitter
|
6
|
-
|
7
|
-
# describe 'size 0' do
|
8
|
-
# before(:each) do
|
9
|
-
# @splitter = Phony::NDC.fixed 0
|
10
|
-
# end
|
11
|
-
#
|
12
|
-
# describe "formatted" do
|
13
|
-
# it "should format correctly" do
|
14
|
-
# # TODO not perfect, but does this case make sense?
|
15
|
-
# splitter.formatted('1234567').should == '123 45 67 '
|
16
|
-
# end
|
17
|
-
# end
|
18
|
-
# end
|
19
|
-
|
20
|
-
describe "size 1" do
|
21
|
-
before(:each) do
|
22
|
-
@splitter = Phony::NDC.fixed 1
|
23
|
-
end
|
24
|
-
|
25
|
-
describe "formatted" do
|
26
|
-
it "should format correctly" do
|
27
|
-
splitter.formatted('12345678').should == '1 234 56 78'
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
describe "size 2" do
|
32
|
-
before(:each) do
|
33
|
-
@splitter = Phony::NDC.fixed 2
|
34
|
-
end
|
35
|
-
|
36
|
-
describe "formatted" do
|
37
|
-
it "should format correctly" do
|
38
|
-
splitter.formatted('123456789').should == '12 345 67 89'
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
describe "size 3" do
|
43
|
-
before(:each) do
|
44
|
-
@splitter = Phony::NDC.fixed 3
|
45
|
-
end
|
46
|
-
|
47
|
-
describe "formatted" do
|
48
|
-
it "should format correctly" do
|
49
|
-
splitter.formatted('1234567890').should == '123 456 78 90'
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
describe "size 4" do
|
54
|
-
before(:each) do
|
55
|
-
@splitter = Phony::NDC.fixed 4
|
56
|
-
end
|
57
|
-
|
58
|
-
describe "formatted" do
|
59
|
-
it "should format correctly" do
|
60
|
-
splitter.formatted('12345678901').should == '1234 567 89 01'
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Phony::NDC::Germany do
|
4
|
-
|
5
|
-
attr_reader :splitter
|
6
|
-
before(:each) do
|
7
|
-
@splitter = Phony::NDC::Germany
|
8
|
-
end
|
9
|
-
|
10
|
-
describe "split" do
|
11
|
-
it "should handle Berlin" do
|
12
|
-
splitter.split('3038625454').should == ['30', '386', '25454']
|
13
|
-
end
|
14
|
-
it "should handle Cologne" do
|
15
|
-
splitter.split('22137683323').should == ['221', '376', '83323']
|
16
|
-
end
|
17
|
-
it "should handle Freiburg im Breisgau" do
|
18
|
-
splitter.split('7614767676').should == ['761', '476', '7676']
|
19
|
-
end
|
20
|
-
it "should handle Nettetal-Lobberich" do
|
21
|
-
splitter.split('21535100').should == ['2153', '510', '0']
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
describe "formatted" do
|
26
|
-
it "should format Berlin" do
|
27
|
-
splitter.formatted('3038625454').should == '30 386 25454'
|
28
|
-
end
|
29
|
-
it "should format Cologne" do
|
30
|
-
splitter.formatted('22137683323').should == '221 376 83323'
|
31
|
-
end
|
32
|
-
it "should format Freiburg im Breisgau" do
|
33
|
-
splitter.formatted('7614767676').should == '761 476 7676'
|
34
|
-
end
|
35
|
-
it "should format Nettetal-Lobberich" do
|
36
|
-
splitter.formatted('21535100').should == '2153 510 0'
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
end
|
data/spec/lib/ndc/prefix_spec.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Phony::NDC::Prefix do
|
4
|
-
|
5
|
-
attr_reader :prefix
|
6
|
-
before(:each) do
|
7
|
-
@prefix = Phony::NDC::Prefix
|
8
|
-
end
|
9
|
-
|
10
|
-
describe "optimize" do
|
11
|
-
it "should convert an array into a hash" do
|
12
|
-
prefix.optimize(Array.new).should be_kind_of(Hash)
|
13
|
-
end
|
14
|
-
it "should convert an empty array into an empty thing" do
|
15
|
-
prefix.optimize([]).should be_empty
|
16
|
-
end
|
17
|
-
it "should convert an empty array into an empty hash" do
|
18
|
-
prefix.optimize([]).should == {}
|
19
|
-
end
|
20
|
-
it "should convert a various string length array into a hash(size=>array(entries))" do
|
21
|
-
prefix.optimize(['1', '22', '333', '4444']).should == { 1 => ['1'], 2 => ['22'], 3 => ['333'], 4 => ['4444'] }
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
@@ -1,59 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Phony::NDC::Splitter do
|
4
|
-
|
5
|
-
attr_reader :splitter
|
6
|
-
|
7
|
-
describe "DSL" do
|
8
|
-
before(:each) do
|
9
|
-
@splitter = Phony::NDC::Splitter
|
10
|
-
end
|
11
|
-
describe "local" do
|
12
|
-
it "should set @split_sizes" do
|
13
|
-
splitter.local 3,2,2
|
14
|
-
splitter.instance_variable_get(:@split_sizes).should == [3,2,2]
|
15
|
-
end
|
16
|
-
it "should call format" do
|
17
|
-
splitter.should_receive(:format).once.with('%s%s%s%s%s')
|
18
|
-
splitter.local 3,2,2
|
19
|
-
end
|
20
|
-
end
|
21
|
-
describe "format" do
|
22
|
-
it "should set @format" do
|
23
|
-
splitter.format 'any format'
|
24
|
-
splitter.instance_variable_get(:@format).should == 'any format'
|
25
|
-
end
|
26
|
-
it "should set @format_with_ndc" do
|
27
|
-
splitter.format 'any format'
|
28
|
-
splitter.instance_variable_get(:@format_with_ndc).should == '%s%sany format'
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
describe "split_local" do
|
34
|
-
describe "with local" do
|
35
|
-
before(:each) do
|
36
|
-
@splitter = Phony::NDC.fixed(2, :local => [3, 2, 2])
|
37
|
-
end
|
38
|
-
it "should split it according to the local format" do
|
39
|
-
splitter.split_local('3643533').should == ['364', '35', '33']
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
describe 'formatted_with_spaces' do
|
45
|
-
before(:each) do
|
46
|
-
@splitter = Phony::NDC.fixed(2, :local => [3, 2, 2])
|
47
|
-
end
|
48
|
-
it 'should format with spaces' do
|
49
|
-
splitter.formatted_with_spaces('%s%s%s%s%s', ['364', '35', '33'], ' ').should == '364 35 33'
|
50
|
-
end
|
51
|
-
it 'should format with spaces' do
|
52
|
-
splitter.formatted_with_spaces('%s%s%s%s%s', ['364', '35', '33'], '').should == '3643533'
|
53
|
-
end
|
54
|
-
it 'should format with dashes' do
|
55
|
-
splitter.formatted_with_spaces('%s%s%s%s%s', ['364', '35', '33'], :-).should == '364-35-33'
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
end
|