norwegian_phone 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ .rvmrc
5
+ .rspec
6
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in no_phone.gemspec
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,30 @@
1
+ = NorwegianPhone
2
+
3
+ == Norwegian Phone Number Normalizer
4
+
5
+ Attempts to ensure that there is only one way to write norwegian phone numbers.
6
+
7
+ After normalization a norwegian phone number should be bare bones without
8
+ national code (e.g. 90779024 if you entered +4790779024).
9
+
10
+ International numbers should all be prefixed with a +.
11
+
12
+ A number is considered to be international if it begins with + or 00 but not +47 or 0047.
13
+
14
+ Lacking complete knowledge of the phone system this is based on common sense and especially for foregin phones there might be many ways to reach the same phone enabling a sneaky user to enter undetected duplicates.
15
+
16
+
17
+ == Usage
18
+
19
+ gem install no_phone
20
+
21
+
22
+ require 'no_phone'
23
+
24
+ class SomethingSpecial
25
+ extend NorwegianPhone
26
+ end
27
+
28
+ SomethingSpecial.normalize "0047 (123) 45-678"
29
+ => "12345678"
30
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,3 @@
1
+ module NorwegianPhone
2
+ VERSION = "0.0.8"
3
+ end
@@ -0,0 +1,46 @@
1
+ module NorwegianPhone
2
+
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
13
+
14
+ def msisdnize(number)
15
+ return nil if missing?(number)
16
+
17
+ number = normalize(number)
18
+ number = "47#{number}" unless number =~ /^\+/
19
+ number.gsub!(/^\+/, '')
20
+ number
21
+ end
22
+
23
+ def unmsisdnize(number)
24
+ normalize("+#{number}") if number
25
+ end
26
+
27
+ def number_valid?(number)
28
+ return false if number.nil?
29
+ return false if number == ""
30
+ case number.strip
31
+ when /\A\+?[0-9[:space:]]+\z/
32
+ return true
33
+ end
34
+ false
35
+ end
36
+
37
+ # Norway-centric. True if not a norwegian number.
38
+ def international?(number)
39
+ !!(normalize(number) =~ /^\+/)
40
+ end
41
+
42
+ private
43
+ def missing?(number)
44
+ number.nil? || (number == "")
45
+ end
46
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "norwegian_phone/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "norwegian_phone"
7
+ s.version = NorwegianPhone::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Simen Svale Skogsrud, Katrina Owen"]
10
+ s.email = ["katrina@bengler.no"]
11
+ s.homepage = "http://rubygems.org/gems/norwegian_phone"
12
+ s.summary = %q{Norwegian phone number scrubber}
13
+ s.description = %q{Standardizes norwegian phone numbers, and leaves international numbers unchanged.}
14
+
15
+ s.rubyforge_project = "norwegian_phone"
16
+
17
+ s.add_development_dependency('rspec')
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.require_paths = ["lib"]
23
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe NorwegianPhone, "#international" do
4
+ it "knows a norse number when it sees one" do
5
+ NorwegianPhoneWrapper.international?("12345678").should == false
6
+ end
7
+
8
+ it "knows a norse number even when it looks international" do
9
+ NorwegianPhoneWrapper.international?("+4712345678").should == false
10
+ end
11
+
12
+ it "correctly identifies a non-norwegian international number" do
13
+ NorwegianPhoneWrapper.international?("+4612345678").should == true
14
+ end
15
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe NorwegianPhone, "#msisdnize" do
4
+
5
+ it "ignores nils" do
6
+ NorwegianPhoneWrapper.msisdnize(nil).should eq(nil)
7
+ end
8
+
9
+ it "ignores empty strings" do
10
+ NorwegianPhoneWrapper.msisdnize("").should eq(nil)
11
+ end
12
+
13
+ it "adds country code to naked norwegian-ish numbers" do
14
+ NorwegianPhoneWrapper.msisdnize("12345678").should eq("4712345678")
15
+ end
16
+
17
+ it "removes +sign from international number" do
18
+ NorwegianPhoneWrapper.msisdnize("+123456789").should eq("123456789")
19
+ end
20
+ end
21
+
22
+ describe NorwegianPhone, "#unmsisdnize" do
23
+ it "localizes norwegian numbers" do
24
+ NorwegianPhoneWrapper.unmsisdnize("4712345678").should eq("12345678")
25
+ end
26
+
27
+ it "adds a + to international numbers" do
28
+ NorwegianPhoneWrapper.unmsisdnize("123456789").should eq("+123456789")
29
+ end
30
+ end
@@ -0,0 +1,102 @@
1
+ require 'spec_helper'
2
+
3
+ describe NorwegianPhone, "#normalize" do
4
+ context "Norwegian numbers" do
5
+ let(:number) { "12345678" }
6
+
7
+ context "with country code" do
8
+ it "deletes prepended country code (47)" do
9
+ NorwegianPhoneWrapper.normalize("4712345678").should == number
10
+ end
11
+
12
+ it "deletes prepended 0047" do
13
+ NorwegianPhoneWrapper.normalize("004712345678").should == number
14
+ end
15
+
16
+ it "deletes prepended +47" do
17
+ NorwegianPhoneWrapper.normalize("+4712345678").should == number
18
+ end
19
+
20
+ it "leaves extra digits intact" do
21
+ NorwegianPhoneWrapper.normalize("+47123456789").should == "#{number}9"
22
+ end
23
+
24
+ it "removes extraneous prepended +signs" do
25
+ NorwegianPhoneWrapper.normalize("++4712345678").should == number
26
+ end
27
+
28
+ it "strips interspersed +signs" do
29
+ NorwegianPhoneWrapper.normalize("+47123+45+678").should == number
30
+ end
31
+ end
32
+
33
+ context "without country code" do
34
+ it "leaves extra digits intact" do
35
+ NorwegianPhoneWrapper.normalize("123456789").should == "#{number}9"
36
+ end
37
+ end
38
+
39
+ it "strips extraneous surrounding whitespace" do
40
+ NorwegianPhoneWrapper.normalize("\t\n \r\n12345678 \t\r\n\n ").should == number
41
+ end
42
+
43
+ it "strips all internal whitespace" do
44
+ NorwegianPhoneWrapper.normalize("12 345 67 8").should == number
45
+ end
46
+
47
+ it "strips dashes" do
48
+ NorwegianPhoneWrapper.normalize("12-34-56-78").should == number
49
+ end
50
+
51
+ it "strips periods" do
52
+ NorwegianPhoneWrapper.normalize("12.34.56.78").should == number
53
+ end
54
+
55
+ it "strips parentheses" do
56
+ NorwegianPhoneWrapper.normalize("(123)45678").should == number
57
+ end
58
+
59
+
60
+ end
61
+
62
+ context "International numbers" do
63
+ let(:number) { "+123456789" }
64
+
65
+ it "replaces 0 with +" do
66
+ NorwegianPhoneWrapper.normalize("0123456789").should == number
67
+ end
68
+
69
+ it "replaces 00 with +" do
70
+ NorwegianPhoneWrapper.normalize("00123456789").should == number
71
+ end
72
+
73
+ it "leaves + alone" do
74
+ NorwegianPhoneWrapper.normalize("+123456789").should == number
75
+ end
76
+
77
+ it "strips extraneous +signs" do
78
+ NorwegianPhoneWrapper.normalize("++123456789").should == number
79
+ end
80
+
81
+ it "strips extraneous surrounding whitespace" do
82
+ NorwegianPhoneWrapper.normalize("\t\n \r\n+123456789 \t\r\n\n ").should == number
83
+ end
84
+
85
+ it "strips all internal whitespace" do
86
+ NorwegianPhoneWrapper.normalize("+12 345 67 89").should == number
87
+ end
88
+
89
+ it "strips dashes" do
90
+ NorwegianPhoneWrapper.normalize("+12-34-56-789").should == number
91
+ end
92
+
93
+ it "strips periods" do
94
+ NorwegianPhoneWrapper.normalize("+12.34.56.789").should == number
95
+ end
96
+
97
+ it "strips parentheses" do
98
+ NorwegianPhoneWrapper.normalize("+(123)456789").should == number
99
+ end
100
+ end
101
+
102
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'norwegian_phone'
5
+
6
+ class NorwegianPhoneWrapper
7
+ extend NorwegianPhone
8
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe NorwegianPhone, "#number_valid?" do
4
+ it "is invalid if nil" do
5
+ NorwegianPhoneWrapper.number_valid?(nil).should be_false
6
+ end
7
+
8
+ it "is invalid if blank" do
9
+ NorwegianPhoneWrapper.number_valid?("").should be_false
10
+ end
11
+
12
+ it "is invalid with letters" do
13
+ NorwegianPhoneWrapper.number_valid?("1abc2").should be_false
14
+ end
15
+
16
+ context "with funky characters" do
17
+ it "rejects parens" do
18
+ NorwegianPhoneWrapper.number_valid?("123(45)678").should be_false
19
+ end
20
+
21
+ it "rejects hyphens" do
22
+ NorwegianPhoneWrapper.number_valid?("123-45-678").should be_false
23
+ end
24
+
25
+ it "rejects periods" do
26
+ NorwegianPhoneWrapper.number_valid?("123.45.678").should be_false
27
+ end
28
+
29
+ it "rejects +sign in the middle" do
30
+ NorwegianPhoneWrapper.number_valid?("123+45+678").should be_false
31
+ end
32
+
33
+ it "accepts spaces" do
34
+ NorwegianPhoneWrapper.number_valid?("123 45 678").should be_true
35
+ end
36
+
37
+ it "accepts +sign in the beginning" do
38
+ NorwegianPhoneWrapper.number_valid?("+123 45 678").should be_true
39
+ end
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: norwegian_phone
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.8
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Simen Svale Skogsrud, Katrina Owen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-28 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70273353792000 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70273353792000
25
+ description: Standardizes norwegian phone numbers, and leaves international numbers
26
+ unchanged.
27
+ email:
28
+ - katrina@bengler.no
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - Gemfile
35
+ - README.rdoc
36
+ - Rakefile
37
+ - lib/norwegian_phone.rb
38
+ - lib/norwegian_phone/version.rb
39
+ - norwegian_phone.gemspec
40
+ - spec/international_spec.rb
41
+ - spec/isdnize_spec.rb
42
+ - spec/normalize_spec.rb
43
+ - spec/spec_helper.rb
44
+ - spec/validation_spec.rb
45
+ homepage: http://rubygems.org/gems/norwegian_phone
46
+ licenses: []
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project: norwegian_phone
65
+ rubygems_version: 1.8.6
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Norwegian phone number scrubber
69
+ test_files:
70
+ - spec/international_spec.rb
71
+ - spec/isdnize_spec.rb
72
+ - spec/normalize_spec.rb
73
+ - spec/spec_helper.rb
74
+ - spec/validation_spec.rb