norwegian_phone 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/norwegian_phone/version.rb +1 -1
- data/lib/norwegian_phone.rb +40 -36
- data/spec/international_spec.rb +3 -3
- data/spec/isdnize_spec.rb +6 -6
- data/spec/normalize_spec.rb +21 -21
- data/spec/spec_helper.rb +0 -4
- data/spec/validation_spec.rb +9 -9
- metadata +3 -3
data/lib/norwegian_phone.rb
CHANGED
@@ -1,46 +1,50 @@
|
|
1
1
|
module NorwegianPhone
|
2
2
|
|
3
|
-
|
4
|
-
number
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
def msisdnize(number)
|
15
|
-
return nil if missing?(number)
|
3
|
+
class << self
|
4
|
+
def normalize(number)
|
5
|
+
number ||= ""
|
6
|
+
number = number.gsub(/[.\- \t\r\n\(\)]/, '')
|
7
|
+
number.gsub!(/^(\+)(.*)/) { |m| $1 + $2.gsub(/\+/, '') }
|
8
|
+
number.gsub!(/^00/, '+')
|
9
|
+
number.gsub!(/^\+47/, '')
|
10
|
+
number.gsub!(/^\+*0+/, '+')
|
11
|
+
number.gsub!(/^47/, '') if number.size > 8
|
12
|
+
number
|
13
|
+
end
|
16
14
|
|
17
|
-
|
18
|
-
|
19
|
-
number.gsub!(/^\+/, '')
|
20
|
-
number
|
21
|
-
end
|
15
|
+
def msisdnize(number)
|
16
|
+
return nil if missing?(number)
|
22
17
|
|
23
|
-
|
24
|
-
|
25
|
-
|
18
|
+
number = normalize(number)
|
19
|
+
number = "47#{number}" unless number =~ /^\+/
|
20
|
+
number.gsub!(/^\+/, '')
|
21
|
+
number
|
22
|
+
end
|
26
23
|
|
27
|
-
|
28
|
-
|
29
|
-
return false if number == ""
|
30
|
-
case number.strip
|
31
|
-
when /\A\+?[0-9[:space:]]+\z/
|
32
|
-
return true
|
24
|
+
def unmsisdnize(number)
|
25
|
+
normalize("+#{number}") if number
|
33
26
|
end
|
34
|
-
false
|
35
|
-
end
|
36
27
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
28
|
+
def number_valid?(number)
|
29
|
+
return false if number.nil?
|
30
|
+
return false if number == ""
|
31
|
+
case number.strip
|
32
|
+
when /\A\+?[0-9[:space:]]+\z/
|
33
|
+
return true
|
34
|
+
end
|
35
|
+
false
|
36
|
+
end
|
41
37
|
|
42
|
-
|
43
|
-
def
|
44
|
-
|
38
|
+
# Norway-centric. True if not a norwegian number.
|
39
|
+
def international?(number)
|
40
|
+
!!(normalize(number) =~ /^\+/)
|
45
41
|
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def missing?(number)
|
46
|
+
number.nil? || (number == "")
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
46
50
|
end
|
data/spec/international_spec.rb
CHANGED
@@ -2,14 +2,14 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe NorwegianPhone, "#international" do
|
4
4
|
it "knows a norse number when it sees one" do
|
5
|
-
|
5
|
+
NorwegianPhone.international?("12345678").should == false
|
6
6
|
end
|
7
7
|
|
8
8
|
it "knows a norse number even when it looks international" do
|
9
|
-
|
9
|
+
NorwegianPhone.international?("+4712345678").should == false
|
10
10
|
end
|
11
11
|
|
12
12
|
it "correctly identifies a non-norwegian international number" do
|
13
|
-
|
13
|
+
NorwegianPhone.international?("+4612345678").should == true
|
14
14
|
end
|
15
15
|
end
|
data/spec/isdnize_spec.rb
CHANGED
@@ -3,28 +3,28 @@ require 'spec_helper'
|
|
3
3
|
describe NorwegianPhone, "#msisdnize" do
|
4
4
|
|
5
5
|
it "ignores nils" do
|
6
|
-
|
6
|
+
NorwegianPhone.msisdnize(nil).should eq(nil)
|
7
7
|
end
|
8
8
|
|
9
9
|
it "ignores empty strings" do
|
10
|
-
|
10
|
+
NorwegianPhone.msisdnize("").should eq(nil)
|
11
11
|
end
|
12
12
|
|
13
13
|
it "adds country code to naked norwegian-ish numbers" do
|
14
|
-
|
14
|
+
NorwegianPhone.msisdnize("12345678").should eq("4712345678")
|
15
15
|
end
|
16
16
|
|
17
17
|
it "removes +sign from international number" do
|
18
|
-
|
18
|
+
NorwegianPhone.msisdnize("+123456789").should eq("123456789")
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
22
|
describe NorwegianPhone, "#unmsisdnize" do
|
23
23
|
it "localizes norwegian numbers" do
|
24
|
-
|
24
|
+
NorwegianPhone.unmsisdnize("4712345678").should eq("12345678")
|
25
25
|
end
|
26
26
|
|
27
27
|
it "adds a + to international numbers" do
|
28
|
-
|
28
|
+
NorwegianPhone.unmsisdnize("123456789").should eq("+123456789")
|
29
29
|
end
|
30
30
|
end
|
data/spec/normalize_spec.rb
CHANGED
@@ -6,54 +6,54 @@ describe NorwegianPhone, "#normalize" do
|
|
6
6
|
|
7
7
|
context "with country code" do
|
8
8
|
it "deletes prepended country code (47)" do
|
9
|
-
|
9
|
+
NorwegianPhone.normalize("4712345678").should == number
|
10
10
|
end
|
11
11
|
|
12
12
|
it "deletes prepended 0047" do
|
13
|
-
|
13
|
+
NorwegianPhone.normalize("004712345678").should == number
|
14
14
|
end
|
15
15
|
|
16
16
|
it "deletes prepended +47" do
|
17
|
-
|
17
|
+
NorwegianPhone.normalize("+4712345678").should == number
|
18
18
|
end
|
19
19
|
|
20
20
|
it "leaves extra digits intact" do
|
21
|
-
|
21
|
+
NorwegianPhone.normalize("+47123456789").should == "#{number}9"
|
22
22
|
end
|
23
23
|
|
24
24
|
it "removes extraneous prepended +signs" do
|
25
|
-
|
25
|
+
NorwegianPhone.normalize("++4712345678").should == number
|
26
26
|
end
|
27
27
|
|
28
28
|
it "strips interspersed +signs" do
|
29
|
-
|
29
|
+
NorwegianPhone.normalize("+47123+45+678").should == number
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
33
|
context "without country code" do
|
34
34
|
it "leaves extra digits intact" do
|
35
|
-
|
35
|
+
NorwegianPhone.normalize("123456789").should == "#{number}9"
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
39
|
it "strips extraneous surrounding whitespace" do
|
40
|
-
|
40
|
+
NorwegianPhone.normalize("\t\n \r\n12345678 \t\r\n\n ").should == number
|
41
41
|
end
|
42
42
|
|
43
43
|
it "strips all internal whitespace" do
|
44
|
-
|
44
|
+
NorwegianPhone.normalize("12 345 67 8").should == number
|
45
45
|
end
|
46
46
|
|
47
47
|
it "strips dashes" do
|
48
|
-
|
48
|
+
NorwegianPhone.normalize("12-34-56-78").should == number
|
49
49
|
end
|
50
50
|
|
51
51
|
it "strips periods" do
|
52
|
-
|
52
|
+
NorwegianPhone.normalize("12.34.56.78").should == number
|
53
53
|
end
|
54
54
|
|
55
55
|
it "strips parentheses" do
|
56
|
-
|
56
|
+
NorwegianPhone.normalize("(123)45678").should == number
|
57
57
|
end
|
58
58
|
|
59
59
|
|
@@ -63,39 +63,39 @@ describe NorwegianPhone, "#normalize" do
|
|
63
63
|
let(:number) { "+123456789" }
|
64
64
|
|
65
65
|
it "replaces 0 with +" do
|
66
|
-
|
66
|
+
NorwegianPhone.normalize("0123456789").should == number
|
67
67
|
end
|
68
68
|
|
69
69
|
it "replaces 00 with +" do
|
70
|
-
|
70
|
+
NorwegianPhone.normalize("00123456789").should == number
|
71
71
|
end
|
72
72
|
|
73
73
|
it "leaves + alone" do
|
74
|
-
|
74
|
+
NorwegianPhone.normalize("+123456789").should == number
|
75
75
|
end
|
76
76
|
|
77
77
|
it "strips extraneous +signs" do
|
78
|
-
|
78
|
+
NorwegianPhone.normalize("++123456789").should == number
|
79
79
|
end
|
80
80
|
|
81
81
|
it "strips extraneous surrounding whitespace" do
|
82
|
-
|
82
|
+
NorwegianPhone.normalize("\t\n \r\n+123456789 \t\r\n\n ").should == number
|
83
83
|
end
|
84
84
|
|
85
85
|
it "strips all internal whitespace" do
|
86
|
-
|
86
|
+
NorwegianPhone.normalize("+12 345 67 89").should == number
|
87
87
|
end
|
88
88
|
|
89
89
|
it "strips dashes" do
|
90
|
-
|
90
|
+
NorwegianPhone.normalize("+12-34-56-789").should == number
|
91
91
|
end
|
92
92
|
|
93
93
|
it "strips periods" do
|
94
|
-
|
94
|
+
NorwegianPhone.normalize("+12.34.56.789").should == number
|
95
95
|
end
|
96
96
|
|
97
97
|
it "strips parentheses" do
|
98
|
-
|
98
|
+
NorwegianPhone.normalize("+(123)456789").should == number
|
99
99
|
end
|
100
100
|
end
|
101
101
|
|
data/spec/spec_helper.rb
CHANGED
data/spec/validation_spec.rb
CHANGED
@@ -2,40 +2,40 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe NorwegianPhone, "#number_valid?" do
|
4
4
|
it "is invalid if nil" do
|
5
|
-
|
5
|
+
NorwegianPhone.number_valid?(nil).should be_false
|
6
6
|
end
|
7
7
|
|
8
8
|
it "is invalid if blank" do
|
9
|
-
|
9
|
+
NorwegianPhone.number_valid?("").should be_false
|
10
10
|
end
|
11
11
|
|
12
12
|
it "is invalid with letters" do
|
13
|
-
|
13
|
+
NorwegianPhone.number_valid?("1abc2").should be_false
|
14
14
|
end
|
15
15
|
|
16
16
|
context "with funky characters" do
|
17
17
|
it "rejects parens" do
|
18
|
-
|
18
|
+
NorwegianPhone.number_valid?("123(45)678").should be_false
|
19
19
|
end
|
20
20
|
|
21
21
|
it "rejects hyphens" do
|
22
|
-
|
22
|
+
NorwegianPhone.number_valid?("123-45-678").should be_false
|
23
23
|
end
|
24
24
|
|
25
25
|
it "rejects periods" do
|
26
|
-
|
26
|
+
NorwegianPhone.number_valid?("123.45.678").should be_false
|
27
27
|
end
|
28
28
|
|
29
29
|
it "rejects +sign in the middle" do
|
30
|
-
|
30
|
+
NorwegianPhone.number_valid?("123+45+678").should be_false
|
31
31
|
end
|
32
32
|
|
33
33
|
it "accepts spaces" do
|
34
|
-
|
34
|
+
NorwegianPhone.number_valid?("123 45 678").should be_true
|
35
35
|
end
|
36
36
|
|
37
37
|
it "accepts +sign in the beginning" do
|
38
|
-
|
38
|
+
NorwegianPhone.number_valid?("+123 45 678").should be_true
|
39
39
|
end
|
40
40
|
end
|
41
41
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: norwegian_phone
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2011-09-28 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70097449596920 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70097449596920
|
25
25
|
description: Standardizes norwegian phone numbers, and leaves international numbers
|
26
26
|
unchanged.
|
27
27
|
email:
|