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 CHANGED
@@ -1,41 +1,39 @@
1
1
  module NoPhone
2
2
 
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
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
- def msisdnize(number)
16
- number = normalize(number)
17
- number = "47#{number}" unless number =~ /^\+/
18
- number.gsub!(/^\+/, '')
19
- number
20
- end
14
+ def msisdnize(number)
15
+ number = normalize(number)
16
+ number = "47#{number}" unless number =~ /^\+/
17
+ number.gsub!(/^\+/, '')
18
+ number
19
+ end
21
20
 
22
- def unmsisdnize(number)
23
- normalize("+#{number}") if number
24
- end
21
+ def unmsisdnize(number)
22
+ normalize("+#{number}") if number
23
+ end
25
24
 
26
- def valid?(number)
27
- return false if number.nil?
28
- return false if number == ""
29
- case number.strip
30
- when /\A\+?[0-9[:space:]]+\z/
31
- return true
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
- # Norway-centric. True if not a norwegian number.
37
- def international?(number)
38
- !!(normalize(number) =~ /^\+/)
39
- end
35
+ # Norway-centric. True if not a norwegian number.
36
+ def international?(number)
37
+ !!(normalize(number) =~ /^\+/)
40
38
  end
41
39
  end
@@ -1,3 +1,3 @@
1
1
  module NoPhone
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -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
- NoPhone.international?("12345678").should == false
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
- NoPhone.international?("+4712345678").should == false
9
+ NoPhoneWrapper.international?("+4712345678").should == false
10
10
  end
11
11
 
12
12
  it "correctly identifies a non-norwegian international number" do
13
- NoPhone.international?("+4612345678").should == true
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
- NoPhone.msisdnize("12345678").should eq("4712345678")
5
+ NoPhoneWrapper.msisdnize("12345678").should eq("4712345678")
6
6
  end
7
7
 
8
8
  it "removes +sign from international number" do
9
- NoPhone.msisdnize("+123456789").should eq("123456789")
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
- NoPhone.unmsisdnize("4712345678").should eq("12345678")
15
+ NoPhoneWrapper.unmsisdnize("4712345678").should eq("12345678")
16
16
  end
17
17
 
18
18
  it "adds a + to international numbers" do
19
- NoPhone.unmsisdnize("123456789").should eq("+123456789")
19
+ NoPhoneWrapper.unmsisdnize("123456789").should eq("+123456789")
20
20
  end
21
21
  end
@@ -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
- NoPhone.normalize("4712345678").should == number
9
+ NoPhoneWrapper.normalize("4712345678").should == number
10
10
  end
11
11
 
12
12
  it "deletes prepended 0047" do
13
- NoPhone.normalize("004712345678").should == number
13
+ NoPhoneWrapper.normalize("004712345678").should == number
14
14
  end
15
15
 
16
16
  it "deletes prepended +47" do
17
- NoPhone.normalize("+4712345678").should == number
17
+ NoPhoneWrapper.normalize("+4712345678").should == number
18
18
  end
19
19
 
20
20
  it "leaves extra digits intact" do
21
- NoPhone.normalize("+47123456789").should == "#{number}9"
21
+ NoPhoneWrapper.normalize("+47123456789").should == "#{number}9"
22
22
  end
23
23
 
24
24
  it "removes extraneous prepended +signs" do
25
- NoPhone.normalize("++4712345678").should == number
25
+ NoPhoneWrapper.normalize("++4712345678").should == number
26
26
  end
27
27
 
28
28
  it "strips interspersed +signs" do
29
- NoPhone.normalize("+47123+45+678").should == number
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
- NoPhone.normalize("123456789").should == "#{number}9"
35
+ NoPhoneWrapper.normalize("123456789").should == "#{number}9"
36
36
  end
37
37
  end
38
38
 
39
39
  it "strips extraneous surrounding whitespace" do
40
- NoPhone.normalize("\t\n \r\n12345678 \t\r\n\n ").should == number
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
- NoPhone.normalize("12 345 67 8").should == number
44
+ NoPhoneWrapper.normalize("12 345 67 8").should == number
45
45
  end
46
46
 
47
47
  it "strips dashes" do
48
- NoPhone.normalize("12-34-56-78").should == number
48
+ NoPhoneWrapper.normalize("12-34-56-78").should == number
49
49
  end
50
50
 
51
51
  it "strips periods" do
52
- NoPhone.normalize("12.34.56.78").should == number
52
+ NoPhoneWrapper.normalize("12.34.56.78").should == number
53
53
  end
54
54
 
55
55
  it "strips parentheses" do
56
- NoPhone.normalize("(123)45678").should == number
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
- NoPhone.normalize("0123456789").should == number
66
+ NoPhoneWrapper.normalize("0123456789").should == number
67
67
  end
68
68
 
69
69
  it "replaces 00 with +" do
70
- NoPhone.normalize("00123456789").should == number
70
+ NoPhoneWrapper.normalize("00123456789").should == number
71
71
  end
72
72
 
73
73
  it "leaves + alone" do
74
- NoPhone.normalize("+123456789").should == number
74
+ NoPhoneWrapper.normalize("+123456789").should == number
75
75
  end
76
76
 
77
77
  it "strips extraneous +signs" do
78
- NoPhone.normalize("++123456789").should == number
78
+ NoPhoneWrapper.normalize("++123456789").should == number
79
79
  end
80
80
 
81
81
  it "strips extraneous surrounding whitespace" do
82
- NoPhone.normalize("\t\n \r\n+123456789 \t\r\n\n ").should == number
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
- NoPhone.normalize("+12 345 67 89").should == number
86
+ NoPhoneWrapper.normalize("+12 345 67 89").should == number
87
87
  end
88
88
 
89
89
  it "strips dashes" do
90
- NoPhone.normalize("+12-34-56-789").should == number
90
+ NoPhoneWrapper.normalize("+12-34-56-789").should == number
91
91
  end
92
92
 
93
93
  it "strips periods" do
94
- NoPhone.normalize("+12.34.56.789").should == number
94
+ NoPhoneWrapper.normalize("+12.34.56.789").should == number
95
95
  end
96
96
 
97
97
  it "strips parentheses" do
98
- NoPhone.normalize("+(123)456789").should == number
98
+ NoPhoneWrapper.normalize("+(123)456789").should == number
99
99
  end
100
100
  end
101
101
 
data/spec/spec_helper.rb CHANGED
@@ -2,3 +2,7 @@ require 'rubygems'
2
2
  require 'bundler/setup'
3
3
 
4
4
  require 'no_phone'
5
+
6
+ class NoPhoneWrapper
7
+ extend NoPhone
8
+ end
@@ -2,40 +2,40 @@ require 'spec_helper'
2
2
 
3
3
  describe NoPhone, "#valid?" do
4
4
  it "is invalid if nil" do
5
- NoPhone.valid?(nil).should be_false
5
+ NoPhoneWrapper.valid?(nil).should be_false
6
6
  end
7
7
 
8
8
  it "is invalid if blank" do
9
- NoPhone.valid?("").should be_false
9
+ NoPhoneWrapper.valid?("").should be_false
10
10
  end
11
11
 
12
12
  it "is invalid with letters" do
13
- NoPhone.valid?("1abc2").should be_false
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
- NoPhone.valid?("123(45)678").should be_false
18
+ NoPhoneWrapper.valid?("123(45)678").should be_false
19
19
  end
20
20
 
21
21
  it "rejects hyphens" do
22
- NoPhone.valid?("123-45-678").should be_false
22
+ NoPhoneWrapper.valid?("123-45-678").should be_false
23
23
  end
24
24
 
25
25
  it "rejects periods" do
26
- NoPhone.valid?("123.45.678").should be_false
26
+ NoPhoneWrapper.valid?("123.45.678").should be_false
27
27
  end
28
28
 
29
29
  it "rejects +sign in the middle" do
30
- NoPhone.valid?("123+45+678").should be_false
30
+ NoPhoneWrapper.valid?("123+45+678").should be_false
31
31
  end
32
32
 
33
33
  it "accepts spaces" do
34
- NoPhone.valid?("123 45 678").should be_true
34
+ NoPhoneWrapper.valid?("123 45 678").should be_true
35
35
  end
36
36
 
37
37
  it "accepts +sign in the beginning" do
38
- NoPhone.valid?("+123 45 678").should be_true
38
+ NoPhoneWrapper.valid?("+123 45 678").should be_true
39
39
  end
40
40
  end
41
41
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 3
9
- version: 0.0.3
8
+ - 4
9
+ version: 0.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Simen Svale Skogsrud, Katrina Owen