no_phone 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/no_phone.rb +30 -32
- data/lib/no_phone/version.rb +1 -1
- data/spec/international_spec.rb +3 -3
- data/spec/isdnize_spec.rb +4 -4
- data/spec/normalize_spec.rb +21 -21
- data/spec/spec_helper.rb +4 -0
- data/spec/validation_spec.rb +9 -9
- metadata +2 -2
data/lib/no_phone.rb
CHANGED
@@ -1,41 +1,39 @@
|
|
1
1
|
module NoPhone
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
end
|
3
|
+
def normalize(number)
|
4
|
+
number ||= ""
|
5
|
+
number = number.gsub(/[.\- \t\r\n\(\)]/, '')
|
6
|
+
number.gsub!(/^(\+)(.*)/) { |m| $1 + $2.gsub(/\+/, '') }
|
7
|
+
number.gsub!(/^00/, '+')
|
8
|
+
number.gsub!(/^\+47/, '')
|
9
|
+
number.gsub!(/^\+*0+/, '+')
|
10
|
+
number.gsub!(/^47/, '') if number.size > 8
|
11
|
+
number
|
12
|
+
end
|
14
13
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
14
|
+
def msisdnize(number)
|
15
|
+
number = normalize(number)
|
16
|
+
number = "47#{number}" unless number =~ /^\+/
|
17
|
+
number.gsub!(/^\+/, '')
|
18
|
+
number
|
19
|
+
end
|
21
20
|
|
22
|
-
|
23
|
-
|
24
|
-
|
21
|
+
def unmsisdnize(number)
|
22
|
+
normalize("+#{number}") if number
|
23
|
+
end
|
25
24
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
end
|
33
|
-
false
|
25
|
+
def valid?(number)
|
26
|
+
return false if number.nil?
|
27
|
+
return false if number == ""
|
28
|
+
case number.strip
|
29
|
+
when /\A\+?[0-9[:space:]]+\z/
|
30
|
+
return true
|
34
31
|
end
|
32
|
+
false
|
33
|
+
end
|
35
34
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
end
|
35
|
+
# Norway-centric. True if not a norwegian number.
|
36
|
+
def international?(number)
|
37
|
+
!!(normalize(number) =~ /^\+/)
|
40
38
|
end
|
41
39
|
end
|
data/lib/no_phone/version.rb
CHANGED
data/spec/international_spec.rb
CHANGED
@@ -2,14 +2,14 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe NoPhone, "#international" do
|
4
4
|
it "knows a norse number when it sees one" do
|
5
|
-
|
5
|
+
NoPhoneWrapper.international?("12345678").should == false
|
6
6
|
end
|
7
7
|
|
8
8
|
it "knows a norse number even when it looks international" do
|
9
|
-
|
9
|
+
NoPhoneWrapper.international?("+4712345678").should == false
|
10
10
|
end
|
11
11
|
|
12
12
|
it "correctly identifies a non-norwegian international number" do
|
13
|
-
|
13
|
+
NoPhoneWrapper.international?("+4612345678").should == true
|
14
14
|
end
|
15
15
|
end
|
data/spec/isdnize_spec.rb
CHANGED
@@ -2,20 +2,20 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe NoPhone, "#msisdnize" do
|
4
4
|
it "adds country code to naked norwegian-ish numbers" do
|
5
|
-
|
5
|
+
NoPhoneWrapper.msisdnize("12345678").should eq("4712345678")
|
6
6
|
end
|
7
7
|
|
8
8
|
it "removes +sign from international number" do
|
9
|
-
|
9
|
+
NoPhoneWrapper.msisdnize("+123456789").should eq("123456789")
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
13
|
describe NoPhone, "#unmsisdnize" do
|
14
14
|
it "localizes norwegian numbers" do
|
15
|
-
|
15
|
+
NoPhoneWrapper.unmsisdnize("4712345678").should eq("12345678")
|
16
16
|
end
|
17
17
|
|
18
18
|
it "adds a + to international numbers" do
|
19
|
-
|
19
|
+
NoPhoneWrapper.unmsisdnize("123456789").should eq("+123456789")
|
20
20
|
end
|
21
21
|
end
|
data/spec/normalize_spec.rb
CHANGED
@@ -6,54 +6,54 @@ describe NoPhone, "#normalize" do
|
|
6
6
|
|
7
7
|
context "with country code" do
|
8
8
|
it "deletes prepended country code (47)" do
|
9
|
-
|
9
|
+
NoPhoneWrapper.normalize("4712345678").should == number
|
10
10
|
end
|
11
11
|
|
12
12
|
it "deletes prepended 0047" do
|
13
|
-
|
13
|
+
NoPhoneWrapper.normalize("004712345678").should == number
|
14
14
|
end
|
15
15
|
|
16
16
|
it "deletes prepended +47" do
|
17
|
-
|
17
|
+
NoPhoneWrapper.normalize("+4712345678").should == number
|
18
18
|
end
|
19
19
|
|
20
20
|
it "leaves extra digits intact" do
|
21
|
-
|
21
|
+
NoPhoneWrapper.normalize("+47123456789").should == "#{number}9"
|
22
22
|
end
|
23
23
|
|
24
24
|
it "removes extraneous prepended +signs" do
|
25
|
-
|
25
|
+
NoPhoneWrapper.normalize("++4712345678").should == number
|
26
26
|
end
|
27
27
|
|
28
28
|
it "strips interspersed +signs" do
|
29
|
-
|
29
|
+
NoPhoneWrapper.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
|
+
NoPhoneWrapper.normalize("123456789").should == "#{number}9"
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
39
|
it "strips extraneous surrounding whitespace" do
|
40
|
-
|
40
|
+
NoPhoneWrapper.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
|
+
NoPhoneWrapper.normalize("12 345 67 8").should == number
|
45
45
|
end
|
46
46
|
|
47
47
|
it "strips dashes" do
|
48
|
-
|
48
|
+
NoPhoneWrapper.normalize("12-34-56-78").should == number
|
49
49
|
end
|
50
50
|
|
51
51
|
it "strips periods" do
|
52
|
-
|
52
|
+
NoPhoneWrapper.normalize("12.34.56.78").should == number
|
53
53
|
end
|
54
54
|
|
55
55
|
it "strips parentheses" do
|
56
|
-
|
56
|
+
NoPhoneWrapper.normalize("(123)45678").should == number
|
57
57
|
end
|
58
58
|
|
59
59
|
|
@@ -63,39 +63,39 @@ describe NoPhone, "#normalize" do
|
|
63
63
|
let(:number) { "+123456789" }
|
64
64
|
|
65
65
|
it "replaces 0 with +" do
|
66
|
-
|
66
|
+
NoPhoneWrapper.normalize("0123456789").should == number
|
67
67
|
end
|
68
68
|
|
69
69
|
it "replaces 00 with +" do
|
70
|
-
|
70
|
+
NoPhoneWrapper.normalize("00123456789").should == number
|
71
71
|
end
|
72
72
|
|
73
73
|
it "leaves + alone" do
|
74
|
-
|
74
|
+
NoPhoneWrapper.normalize("+123456789").should == number
|
75
75
|
end
|
76
76
|
|
77
77
|
it "strips extraneous +signs" do
|
78
|
-
|
78
|
+
NoPhoneWrapper.normalize("++123456789").should == number
|
79
79
|
end
|
80
80
|
|
81
81
|
it "strips extraneous surrounding whitespace" do
|
82
|
-
|
82
|
+
NoPhoneWrapper.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
|
+
NoPhoneWrapper.normalize("+12 345 67 89").should == number
|
87
87
|
end
|
88
88
|
|
89
89
|
it "strips dashes" do
|
90
|
-
|
90
|
+
NoPhoneWrapper.normalize("+12-34-56-789").should == number
|
91
91
|
end
|
92
92
|
|
93
93
|
it "strips periods" do
|
94
|
-
|
94
|
+
NoPhoneWrapper.normalize("+12.34.56.789").should == number
|
95
95
|
end
|
96
96
|
|
97
97
|
it "strips parentheses" do
|
98
|
-
|
98
|
+
NoPhoneWrapper.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 NoPhone, "#valid?" do
|
4
4
|
it "is invalid if nil" do
|
5
|
-
|
5
|
+
NoPhoneWrapper.valid?(nil).should be_false
|
6
6
|
end
|
7
7
|
|
8
8
|
it "is invalid if blank" do
|
9
|
-
|
9
|
+
NoPhoneWrapper.valid?("").should be_false
|
10
10
|
end
|
11
11
|
|
12
12
|
it "is invalid with letters" do
|
13
|
-
|
13
|
+
NoPhoneWrapper.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
|
+
NoPhoneWrapper.valid?("123(45)678").should be_false
|
19
19
|
end
|
20
20
|
|
21
21
|
it "rejects hyphens" do
|
22
|
-
|
22
|
+
NoPhoneWrapper.valid?("123-45-678").should be_false
|
23
23
|
end
|
24
24
|
|
25
25
|
it "rejects periods" do
|
26
|
-
|
26
|
+
NoPhoneWrapper.valid?("123.45.678").should be_false
|
27
27
|
end
|
28
28
|
|
29
29
|
it "rejects +sign in the middle" do
|
30
|
-
|
30
|
+
NoPhoneWrapper.valid?("123+45+678").should be_false
|
31
31
|
end
|
32
32
|
|
33
33
|
it "accepts spaces" do
|
34
|
-
|
34
|
+
NoPhoneWrapper.valid?("123 45 678").should be_true
|
35
35
|
end
|
36
36
|
|
37
37
|
it "accepts +sign in the beginning" do
|
38
|
-
|
38
|
+
NoPhoneWrapper.valid?("+123 45 678").should be_true
|
39
39
|
end
|
40
40
|
end
|
41
41
|
end
|