dialy 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  module Dialy
2
- def self.format_number(value)
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
- (2..5).each do |len|
37
- part = plain[0,len]
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
@@ -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['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')
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['43'].should be_include('1')
14
+ Dialy::AREA_CODES[43].should be_include(1)
15
15
 
16
- Dialy::AREA_CODES['41'].should be_include('44')
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] = '41'
22
- Dialy.format_number('030-12345678').should == '+41 30 12345678'
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] = '49'
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.format_number('02406-12345678').should == @expected
39
+ Dialy.format('02406-12345678').should == @expected
34
40
  end
35
41
 
36
42
  it "should format with +49" do
37
- Dialy.format_number('+49240612345678').should == @expected
43
+ Dialy.format('+49240612345678').should == @expected
38
44
  end
39
45
 
40
46
  it "should format with +49(0)" do
41
- Dialy.format_number('+49(0)2406-123456-78').should == @expected
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.format_number('0049240612345678').should == @expected
51
+ Dialy.format('0049240612345678').should == @expected
46
52
  end
47
53
 
48
54
  it "should format with missing 0" do
49
- Dialy.format_number('240612345678').should == @expected
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] = '49'
61
+ Dialy::Config[:default_country_code] = 49
56
62
  end
57
63
 
58
64
  it "should format" do
59
- Dialy.format_number('0163-1234567').should == '+49 163 1234567'
60
- Dialy.format_number('0171-1234567').should == '+49 171 1234567'
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 "obsure input" do
70
+ describe "obscure input" do
65
71
  before :each do
66
- Dialy::Config[:default_country_code] = '49'
72
+ Dialy::Config[:default_country_code] = 49
67
73
  end
68
74
 
69
75
  it "should format" do
70
- Dialy.format_number('(+49) (08541) 123456').should == '+49 8541 123456'
71
- Dialy.format_number('0 08 00-1 23 45 67').should == '+800 1234567'
72
- Dialy.format_number('[0351] 1 23 45 6').should == '+49 351 123456'
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] = '41'
84
+ Dialy::Config[:default_country_code] = 41
79
85
  end
80
86
 
81
87
  it "should format" do
82
- Dialy.format_number('0041-71-123 45 67').should == '+41 71 1234567'
83
- Dialy.format_number('71-123 45 67').should == '+41 71 1234567'
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.format_number('++49') }.should raise_error(ArgumentError)
90
- lambda { Dialy.format_number('0+49 221') }.should raise_error(ArgumentError)
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.format_number('+49 2396 1234567') }.should raise_error
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.format_number('+429 1234 1234567') }.should raise_error
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: 27
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.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-09 00:00:00 +02:00
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