dialy 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,17 +9,17 @@ Because the area codes in Germany, Austria and Switzerland have different length
9
9
 
10
10
  == Examples
11
11
 
12
- Dialy.format('+49 (0221) 12 34 56')
12
+ Dialy::Number.new('+49 (0221) 12 34 56').to_s
13
13
  # => '+49 221 1234567'
14
14
 
15
- Dialy.format('+49 (0)221 12 34 56')
15
+ Dialy::Number.new('+49 (0)221 12 34 56').to_s
16
16
  # => '+49 221 1234567'
17
17
 
18
- Dialy.format('0049221123456')
18
+ Dialy::Number.new('0049221123456').to_s
19
19
  # => '+49 221 1234567'
20
20
 
21
21
  Dialy::Config[:default_country_code] = 49
22
- Dialy.format('0221/123456')
22
+ Dialy::Number.new('0221/123456').to_s
23
23
  # => '+49 221 1234567'
24
24
 
25
25
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{dialy}
8
- s.version = "0.2.1"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Georg Ledermann"]
12
- s.date = %q{2010-09-10}
12
+ s.date = %q{2010-10-05}
13
13
  s.description = %q{Knows all area codes from Germany, Austria and Switzerland}
14
14
  s.email = %q{mail@georg-ledermann.de}
15
15
  s.extra_rdoc_files = [
@@ -1,59 +1,74 @@
1
1
  module Dialy
2
- def self.format(value)
3
- # Remove all but digits and +
4
- plain = value.gsub(/[^+0-9]/, '')
2
+ class UnknownCountryCode < ArgumentError; end
3
+ class UnknownAreaCode < ArgumentError; end
4
+ class WrongFormatting < ArgumentError; end
5
5
 
6
- # Error check: Plus (+) is only allowed as first character
7
- raise ArgumentError if plain.count('+') > 1
8
- raise ArgumentError if plain.index('+').to_i > 0
9
-
10
- # Step 1: Find country code
11
- country_code = nil
12
- if match = plain.match(/^(\+|00)(\d{1,3})/)
13
- plain.slice!(0,match[1].length)
6
+ class Number
7
+ def initialize(value)
8
+ raise ArgumentError unless value.is_a?(String)
14
9
 
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.
18
- CC_RANGE.each do |len|
19
- part = match[2][0,len].to_i
10
+ # Remove all but digits and +
11
+ @number = value.gsub(/[^+0-9]/, '')
12
+
13
+ # Plus (+) is only allowed as first character
14
+ raise WrongFormatting if @number.count('+') > 1
15
+ raise WrongFormatting if @number.index('+').to_i > 0
16
+
17
+ # Main work
18
+ @country_code = extract_country_code! || Config[:default_country_code]
19
+ @area_code = extract_area_code!
20
+ end
21
+
22
+ # String representation in E.123 format
23
+ def to_s
24
+ "+#{@country_code} #{[ @area_code, @number ].compact.join(' ')}"
25
+ end
26
+
27
+ private
28
+ def extract_country_code!
29
+ if match = @number.match(/^(\+|00)(\d{1,3})/)
30
+ # Because the length of a country code is not fixed, we have to do
31
+ # multiple searches. Start with the minimum length and go to the
32
+ # maxium until an area code is found.
33
+ CC_RANGE.each do |len|
34
+ part = match[2][0,len].to_i
20
35
 
21
- if COUNTRY_CODES.include?(part)
22
- country_code = part
23
- plain.slice!(0,len)
24
- break
36
+ if COUNTRY_CODES.include?(part)
37
+ # Remove "+" or leading "00"
38
+ @number.slice!(0,match[1].length)
39
+
40
+ # Strip country code
41
+ @number.slice!(0,part.to_s.length)
42
+
43
+ return part
44
+ end
25
45
  end
26
- end
27
46
 
28
- raise ArgumentError.new("Unknown country code: #{match[2]}") unless country_code
29
- else
30
- country_code = Config[:default_country_code]
47
+ raise UnknownCountryCode.new("Unknown country code: #{match[2]}")
48
+ end
31
49
  end
50
+
51
+ def extract_area_code!
52
+ # Delete leading "0"
53
+ @number.slice!(0,1) if @number.match /^0/
32
54
 
33
- # Delete leading "0"
34
- plain.slice!(0,1) if plain.match /^0/
35
-
36
- # Step 2: Find area code
37
- area_code = nil
38
- if AREA_CODES[country_code]
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
55
+ if AREA_CODES[@country_code]
56
+ # Because the length of an area code is not fixed, we have to do
57
+ # multiple searches. Start with the minimum length and go to the
58
+ # maxium until an area code is found.
59
+ AC_RANGE[@country_code].each do |len|
60
+ part = @number[0,len].to_i
45
61
 
46
- if AREA_CODES[country_code].include?(part)
47
- area_code = part
48
- plain.slice!(0,len)
49
- break
62
+ if AREA_CODES[@country_code].include?(part)
63
+ # Strip area_code
64
+ @number.slice!(0,part.to_s.length)
65
+
66
+ return part
67
+ end
50
68
  end
51
- end
52
69
 
53
- raise ArgumentError.new("Area code not found") unless area_code
70
+ raise UnknownAreaCode.new('Unknown area code')
71
+ end
54
72
  end
55
-
56
- # Finished. Build result
57
- "+#{country_code} #{[ area_code, plain ].compact.join(' ')}"
58
73
  end
59
74
  end
@@ -26,7 +26,7 @@ describe "Dialy" do
26
26
  describe "options" do
27
27
  it "should use default_country_code" do
28
28
  Dialy::Config[:default_country_code] = 41
29
- Dialy.format('030-12345678').should == '+41 30 12345678'
29
+ Dialy::Number.new('030-12345678').to_s.should == '+41 30 12345678'
30
30
  end
31
31
  end
32
32
 
@@ -37,23 +37,23 @@ describe "Dialy" do
37
37
  end
38
38
 
39
39
  it "should format plain number" do
40
- Dialy.format('02406-12345678').should == @expected
40
+ Dialy::Number.new('02406-12345678').to_s.should == @expected
41
41
  end
42
42
 
43
43
  it "should format with +49" do
44
- Dialy.format('+49240612345678').should == @expected
44
+ Dialy::Number.new('+49240612345678').to_s.should == @expected
45
45
  end
46
46
 
47
47
  it "should format with +49(0)" do
48
- Dialy.format('+49(0)2406-123456-78').should == @expected
48
+ Dialy::Number.new('+49(0)2406-123456-78').to_s.should == @expected
49
49
  end
50
50
 
51
51
  it "should format with 0049" do
52
- Dialy.format('0049240612345678').should == @expected
52
+ Dialy::Number.new('0049240612345678').to_s.should == @expected
53
53
  end
54
54
 
55
55
  it "should format with missing 0" do
56
- Dialy.format('240612345678').should == @expected
56
+ Dialy::Number.new('240612345678').to_s.should == @expected
57
57
  end
58
58
  end
59
59
 
@@ -63,8 +63,8 @@ describe "Dialy" do
63
63
  end
64
64
 
65
65
  it "should format" do
66
- Dialy.format('0163-1234567').should == '+49 163 1234567'
67
- Dialy.format('0171-1234567').should == '+49 171 1234567'
66
+ Dialy::Number.new('0163-1234567').to_s.should == '+49 163 1234567'
67
+ Dialy::Number.new('0171-1234567').to_s.should == '+49 171 1234567'
68
68
  end
69
69
  end
70
70
 
@@ -74,9 +74,9 @@ describe "Dialy" do
74
74
  end
75
75
 
76
76
  it "should format" do
77
- Dialy.format('(+49) (08541) 123456').should == '+49 8541 123456'
78
- Dialy.format('0 08 00-1 23 45 67').should == '+800 1234567'
79
- Dialy.format('[0351] 1 23 45 6').should == '+49 351 123456'
77
+ Dialy::Number.new('(+49) (08541) 123456').to_s.should == '+49 8541 123456'
78
+ Dialy::Number.new('0 08 00-1 23 45 67').to_s.should == '+800 1234567'
79
+ Dialy::Number.new('[0351] 1 23 45 6').to_s.should == '+49 351 123456'
80
80
  end
81
81
  end
82
82
 
@@ -86,23 +86,27 @@ describe "Dialy" do
86
86
  end
87
87
 
88
88
  it "should format" do
89
- Dialy.format('0041-71-123 45 67').should == '+41 71 1234567'
90
- Dialy.format('71-123 45 67').should == '+41 71 1234567'
89
+ Dialy::Number.new('0041-71-123 45 67').to_s.should == '+41 71 1234567'
90
+ Dialy::Number.new('71-123 45 67').to_s.should == '+41 71 1234567'
91
91
  end
92
92
  end
93
93
 
94
94
  describe "Wrong formatting" do
95
95
  it "should fail with +" do
96
- lambda { Dialy.format('++49') }.should raise_error(ArgumentError)
97
- lambda { Dialy.format('0+49 221') }.should raise_error(ArgumentError)
96
+ lambda { Dialy::Number.new('++49') }.should raise_error(Dialy::WrongFormatting)
97
+ lambda { Dialy::Number.new('0+49 221') }.should raise_error(Dialy::WrongFormatting)
98
98
  end
99
99
 
100
100
  it "should fail for non existing area_code" do
101
- lambda { Dialy.format('+49 2396 1234567') }.should raise_error(ArgumentError)
101
+ lambda { Dialy::Number.new('+49 2396 1234567') }.should raise_error(Dialy::UnknownAreaCode)
102
102
  end
103
103
 
104
- it "should fail for non existing country_code" do
105
- lambda { Dialy.format('+429 1234 1234567') }.should raise_error(ArgumentError)
104
+ it "should fail for non existing (+) country_code" do
105
+ lambda { Dialy::Number.new('+429 1234 1234567') }.should raise_error(Dialy::UnknownCountryCode)
106
+ end
107
+
108
+ it "should fail for nun existing (00) country_code" do
109
+ lambda { Dialy::Number.new('003834-123456') }.should raise_error(Dialy::UnknownCountryCode)
106
110
  end
107
111
  end
108
112
  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: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
9
- - 1
10
- version: 0.2.1
8
+ - 3
9
+ - 0
10
+ version: 0.3.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-10 00:00:00 +02:00
18
+ date: 2010-10-05 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency