dialy 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +6 -6
- data/VERSION +1 -1
- data/dialy.gemspec +6 -2
- data/lib/dialy/config.rb +1 -1
- data/lib/dialy/data.rb +15 -384
- data/lib/dialy/data/at.rb +68 -0
- data/lib/dialy/data/ch.rb +22 -0
- data/lib/dialy/data/countries.rb +26 -0
- data/lib/dialy/data/de.rb +331 -0
- data/lib/dialy/formatter.rb +13 -6
- data/spec/dialy_spec.rb +39 -33
- metadata +8 -4
data/lib/dialy/formatter.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module Dialy
|
2
|
-
def self.
|
2
|
+
def self.format(value)
|
3
3
|
# Remove all but digits and +
|
4
4
|
plain = value.gsub(/[^+0-9]/, '')
|
5
5
|
|
@@ -12,8 +12,11 @@ module Dialy
|
|
12
12
|
if match = plain.match(/^(\+|00)(\d{1,3})/)
|
13
13
|
plain.slice!(0,match[1].length)
|
14
14
|
|
15
|
+
# Because the length of a country code is not fixed, we have to do
|
16
|
+
# multiple searches. Start with the minimum length and go to the
|
17
|
+
# maxium until an area code is found.
|
15
18
|
(1..3).each do |len|
|
16
|
-
part = match[2][0,len]
|
19
|
+
part = match[2][0,len].to_i
|
17
20
|
|
18
21
|
if COUNTRY_CODES.include?(part)
|
19
22
|
country_code = part
|
@@ -22,7 +25,7 @@ module Dialy
|
|
22
25
|
end
|
23
26
|
end
|
24
27
|
|
25
|
-
raise ArgumentError("Unknown country code: #{match[2]}") unless country_code
|
28
|
+
raise ArgumentError.new("Unknown country code: #{match[2]}") unless country_code
|
26
29
|
else
|
27
30
|
country_code = Config[:default_country_code]
|
28
31
|
end
|
@@ -33,8 +36,12 @@ module Dialy
|
|
33
36
|
# Step 2: Find area code
|
34
37
|
area_code = nil
|
35
38
|
if AREA_CODES[country_code]
|
36
|
-
|
37
|
-
|
39
|
+
|
40
|
+
# Because the length of an area code is not fixed, we have to do
|
41
|
+
# multiple searches. Start with the minimum length and go to the
|
42
|
+
# maxium until an area code is found.
|
43
|
+
AC_RANGE[country_code].each do |len|
|
44
|
+
part = plain[0,len].to_i
|
38
45
|
|
39
46
|
if AREA_CODES[country_code].include?(part)
|
40
47
|
area_code = part
|
@@ -43,7 +50,7 @@ module Dialy
|
|
43
50
|
end
|
44
51
|
end
|
45
52
|
|
46
|
-
raise ArgumentError("Area code not found") unless area_code
|
53
|
+
raise ArgumentError.new("Area code not found") unless area_code
|
47
54
|
end
|
48
55
|
|
49
56
|
# Finished. Build result
|
data/spec/dialy_spec.rb
CHANGED
@@ -2,100 +2,106 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
2
|
|
3
3
|
describe "Dialy" do
|
4
4
|
it "should find area codes" do
|
5
|
-
Dialy::AREA_CODES[
|
6
|
-
Dialy::AREA_CODES[
|
7
|
-
Dialy::AREA_CODES[
|
8
|
-
Dialy::AREA_CODES[
|
9
|
-
Dialy::AREA_CODES[
|
10
|
-
Dialy::AREA_CODES[
|
11
|
-
Dialy::AREA_CODES[
|
12
|
-
Dialy::AREA_CODES[
|
5
|
+
Dialy::AREA_CODES[49].should be_include(221)
|
6
|
+
Dialy::AREA_CODES[49].should be_include(2233)
|
7
|
+
Dialy::AREA_CODES[49].should be_include(2235)
|
8
|
+
Dialy::AREA_CODES[49].should be_include(2406)
|
9
|
+
Dialy::AREA_CODES[49].should be_include(30)
|
10
|
+
Dialy::AREA_CODES[49].should be_include(241)
|
11
|
+
Dialy::AREA_CODES[49].should be_include(39291)
|
12
|
+
Dialy::AREA_CODES[49].should be_include(163)
|
13
13
|
|
14
|
-
Dialy::AREA_CODES[
|
14
|
+
Dialy::AREA_CODES[43].should be_include(1)
|
15
15
|
|
16
|
-
Dialy::AREA_CODES[
|
16
|
+
Dialy::AREA_CODES[41].should be_include(44)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should calc min and max length" do
|
20
|
+
Dialy::AC_RANGE[41].should == (2..3)
|
21
|
+
Dialy::AC_RANGE[43].should == (1..4)
|
22
|
+
Dialy::AC_RANGE[49].should == (2..5)
|
17
23
|
end
|
18
24
|
|
19
25
|
describe "options" do
|
20
26
|
it "should use default_country_code" do
|
21
|
-
Dialy::Config[:default_country_code] =
|
22
|
-
Dialy.
|
27
|
+
Dialy::Config[:default_country_code] = 41
|
28
|
+
Dialy.format('030-12345678').should == '+41 30 12345678'
|
23
29
|
end
|
24
30
|
end
|
25
31
|
|
26
32
|
describe "Germany" do
|
27
33
|
before :each do
|
28
|
-
Dialy::Config[:default_country_code] =
|
34
|
+
Dialy::Config[:default_country_code] = 49
|
29
35
|
@expected = '+49 2406 12345678'
|
30
36
|
end
|
31
37
|
|
32
38
|
it "should format plain number" do
|
33
|
-
Dialy.
|
39
|
+
Dialy.format('02406-12345678').should == @expected
|
34
40
|
end
|
35
41
|
|
36
42
|
it "should format with +49" do
|
37
|
-
Dialy.
|
43
|
+
Dialy.format('+49240612345678').should == @expected
|
38
44
|
end
|
39
45
|
|
40
46
|
it "should format with +49(0)" do
|
41
|
-
Dialy.
|
47
|
+
Dialy.format('+49(0)2406-123456-78').should == @expected
|
42
48
|
end
|
43
49
|
|
44
50
|
it "should format with 0049" do
|
45
|
-
Dialy.
|
51
|
+
Dialy.format('0049240612345678').should == @expected
|
46
52
|
end
|
47
53
|
|
48
54
|
it "should format with missing 0" do
|
49
|
-
Dialy.
|
55
|
+
Dialy.format('240612345678').should == @expected
|
50
56
|
end
|
51
57
|
end
|
52
58
|
|
53
59
|
describe "German mobile" do
|
54
60
|
before :each do
|
55
|
-
Dialy::Config[:default_country_code] =
|
61
|
+
Dialy::Config[:default_country_code] = 49
|
56
62
|
end
|
57
63
|
|
58
64
|
it "should format" do
|
59
|
-
Dialy.
|
60
|
-
Dialy.
|
65
|
+
Dialy.format('0163-1234567').should == '+49 163 1234567'
|
66
|
+
Dialy.format('0171-1234567').should == '+49 171 1234567'
|
61
67
|
end
|
62
68
|
end
|
63
69
|
|
64
|
-
describe "
|
70
|
+
describe "obscure input" do
|
65
71
|
before :each do
|
66
|
-
Dialy::Config[:default_country_code] =
|
72
|
+
Dialy::Config[:default_country_code] = 49
|
67
73
|
end
|
68
74
|
|
69
75
|
it "should format" do
|
70
|
-
Dialy.
|
71
|
-
Dialy.
|
72
|
-
Dialy.
|
76
|
+
Dialy.format('(+49) (08541) 123456').should == '+49 8541 123456'
|
77
|
+
Dialy.format('0 08 00-1 23 45 67').should == '+800 1234567'
|
78
|
+
Dialy.format('[0351] 1 23 45 6').should == '+49 351 123456'
|
73
79
|
end
|
74
80
|
end
|
75
81
|
|
76
82
|
describe "Switzerland" do
|
77
83
|
before :each do
|
78
|
-
Dialy::Config[:default_country_code] =
|
84
|
+
Dialy::Config[:default_country_code] = 41
|
79
85
|
end
|
80
86
|
|
81
87
|
it "should format" do
|
82
|
-
Dialy.
|
83
|
-
Dialy.
|
88
|
+
Dialy.format('0041-71-123 45 67').should == '+41 71 1234567'
|
89
|
+
Dialy.format('71-123 45 67').should == '+41 71 1234567'
|
84
90
|
end
|
85
91
|
end
|
86
92
|
|
87
93
|
describe "Wrong formatting" do
|
88
94
|
it "should fail with +" do
|
89
|
-
lambda { Dialy.
|
90
|
-
lambda { Dialy.
|
95
|
+
lambda { Dialy.format('++49') }.should raise_error(ArgumentError)
|
96
|
+
lambda { Dialy.format('0+49 221') }.should raise_error(ArgumentError)
|
91
97
|
end
|
92
98
|
|
93
99
|
it "should fail for non existing area_code" do
|
94
|
-
lambda { Dialy.
|
100
|
+
lambda { Dialy.format('+49 2396 1234567') }.should raise_error(ArgumentError)
|
95
101
|
end
|
96
102
|
|
97
103
|
it "should fail for non existing country_code" do
|
98
|
-
lambda { Dialy.
|
104
|
+
lambda { Dialy.format('+429 1234 1234567') }.should raise_error(ArgumentError)
|
99
105
|
end
|
100
106
|
end
|
101
107
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dialy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Georg Ledermann
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-09-
|
18
|
+
date: 2010-09-10 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -54,6 +54,10 @@ files:
|
|
54
54
|
- lib/dialy.rb
|
55
55
|
- lib/dialy/config.rb
|
56
56
|
- lib/dialy/data.rb
|
57
|
+
- lib/dialy/data/at.rb
|
58
|
+
- lib/dialy/data/ch.rb
|
59
|
+
- lib/dialy/data/countries.rb
|
60
|
+
- lib/dialy/data/de.rb
|
57
61
|
- lib/dialy/formatter.rb
|
58
62
|
- spec/dialy_spec.rb
|
59
63
|
- spec/spec.opts
|