big-phoney 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -8,7 +8,7 @@ But! Here's what you can do:
8
8
 
9
9
 
10
10
  phone = PhoneNumber.new('20 484-123-1234 x 123')
11
- phone.number # => "4844679726"
11
+ phone.number # => "4841231234"
12
12
  phone.area_code # => "484"
13
13
  phone.extension # => "123"
14
14
  phone.country_code # => "20"
data/big-phoney.gemspec CHANGED
@@ -2,12 +2,12 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{big-phoney}
5
- s.version = "0.0.3"
5
+ s.version = "0.0.4"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
- s.authors = ["Pat Nakajima"]
9
- s.date = %q{2010-08-03}
10
- s.files = ["LICENSE", "README.md", "big-phoney.gemspec", "lib/big-phoney.rb", "lib/big_phoney.rb", "lib/big_phoney/.gitignore", "lib/big_phoney/country_codes.rb", "spec/big_phoney_spec.rb", "spec/spec_helper.rb"]
8
+ s.authors = ["Pat Nakajima", "Brandon Keene"]
9
+ s.date = %q{2010-08-26}
10
+ s.files = ["LICENSE", "README.md", "big-phoney.gemspec", "lib/big-phoney.rb", "lib/big_phoney.rb", "lib/big_phoney/.gitignore", "spec/big_phoney_spec.rb", "spec/spec_helper.rb"]
11
11
  s.require_paths = ["lib"]
12
12
  s.rubygems_version = %q{1.3.7}
13
13
  s.summary = %q{Simple phone number parsing in Ruby}
data/lib/big-phoney.rb CHANGED
@@ -1,95 +1,2 @@
1
- MIN_US_LENGTH = 10
2
- MINLEN = 7
3
- EXT = /\s*(?:(?:ext|ex|xt|x)[\s.:]*(\d+))/i
4
-
5
1
  $LOAD_PATH.unshift File.join(File.dirname(__FILE__))
6
-
7
- require 'big_phoney/country_codes'
8
-
9
- class PhoneNumber
10
- def self.assume_us? ; @assume_us end
11
- def self.assume_us=(us) ; @assume_us = us end
12
-
13
- attr_accessor :country_code, :ext, :number
14
-
15
- def initialize(number)
16
- @number = number.strip
17
- parse_extension
18
- parse_country_code
19
- end
20
-
21
- def to_s
22
- result = '(%s) %s-%s' % [area_code, prefix, rest]
23
-
24
- if country_code != '1'
25
- result = '+%s %s' % [country_code, result]
26
- end
27
-
28
- if extension
29
- result = '%s ext. %s' % [result, extension]
30
- end
31
-
32
- result
33
- end
34
-
35
- def valid?
36
- errors.empty?
37
- end
38
-
39
- def area_code
40
- number[0..2]
41
- end
42
-
43
- def prefix
44
- number[3..5]
45
- end
46
-
47
- def rest
48
- number[6..9]
49
- end
50
-
51
- def errors
52
- @errors ||= []
53
- end
54
-
55
- alias_method :extension, :ext
56
-
57
- private
58
-
59
- def parse_extension
60
- @number =~ EXT
61
- if $1 && $1.size > 4
62
- errors << :extension
63
- else
64
- @ext = $1
65
- @number.sub!(EXT, '')
66
- end
67
-
68
- @number.gsub!(/\D/, '')
69
- @number.sub!(/^0+/, '')
70
- end
71
-
72
- def parse_country_code
73
- if @number.size == 10
74
- @number = "1#{@number}"
75
- end
76
-
77
- 0.upto(2) do |len|
78
- break if @country_code
79
- if COUNTRY_CODES.include?(country_code = @number[0..len])
80
- @country_code ||= country_code
81
- @number.sub!(/^#{country_code}/, '')
82
- end
83
- end
84
-
85
- if @country_code.nil?
86
- errors << :country_code
87
- end
88
-
89
- if @country_code == '1'
90
- errors << :too_short if @number.size < MIN_US_LENGTH
91
- else
92
- errors << :too_short if @number.size <= MINLEN
93
- end
94
- end
95
- end
2
+ require 'big_phoney/phone_number'
@@ -1,105 +1,109 @@
1
1
  require 'spec/spec_helper'
2
2
 
3
- describe PhoneNumber do
3
+ describe BigPhoney::PhoneNumber do
4
4
  describe "validity" do
5
5
  it "is valid with a valid number" do
6
- number = PhoneNumber.new('(484) 123-1234')
6
+ number = BigPhoney::PhoneNumber.new('(484) 123-1234')
7
7
  number.should be_valid
8
8
  end
9
9
 
10
10
  it "is invalid with wrong country code" do
11
- number = PhoneNumber.new('+29 (484) 123-1234')
11
+ number = BigPhoney::PhoneNumber.new('+29 (484) 123-1234')
12
12
  number.should_not be_valid
13
13
  number.errors.should include(:country_code)
14
14
  end
15
15
 
16
16
  it "is invalid with extension that is too long" do
17
- number = PhoneNumber.new('(484) 123-1234 x2343423432')
17
+ number = BigPhoney::PhoneNumber.new('(484) 123-1234 x2343423432')
18
18
  number.should_not be_valid
19
19
  number.errors.should include(:extension)
20
20
  end
21
21
 
22
22
  it "is invalid when too short" do
23
- number = PhoneNumber.new('535341345')
23
+ number = BigPhoney::PhoneNumber.new('535341345')
24
24
  number.should_not be_valid
25
25
  number.errors.should include(:too_short)
26
26
  end
27
27
  end
28
28
 
29
+ it "handles nil values" do
30
+ BigPhoney::PhoneNumber.new(nil).number.should == ""
31
+ end
32
+
29
33
  describe "stripping other characters" do
30
34
  it "strips parens" do
31
- PhoneNumber.new('(484) 123-1234').number.should == '4841231234'
35
+ BigPhoney::PhoneNumber.new('(484) 123-1234').number.should == '4841231234'
32
36
  end
33
37
 
34
38
  it "allows spaces" do
35
- PhoneNumber.new('484 123 1234').number.should == '4841231234'
39
+ BigPhoney::PhoneNumber.new('484 123 1234').number.should == '4841231234'
36
40
  end
37
41
 
38
42
  it "allows dashes" do
39
- PhoneNumber.new('484-123-1234').number.should == '4841231234'
43
+ BigPhoney::PhoneNumber.new('484-123-1234').number.should == '4841231234'
40
44
  end
41
45
 
42
46
  it "allows dots" do
43
- PhoneNumber.new('484.123.1234').number.should == '4841231234'
47
+ BigPhoney::PhoneNumber.new('484.123.1234').number.should == '4841231234'
44
48
  end
45
49
 
46
50
  it "allows plus signs" do
47
- PhoneNumber.new('+1 (484) 123-1234').number.should == '4841231234'
51
+ BigPhoney::PhoneNumber.new('+1 (484) 123-1234').number.should == '4841231234'
48
52
  end
49
53
  end
50
54
 
51
55
  describe "parsing extension" do
52
56
  it "returns nil when there is none" do
53
- PhoneNumber.new('4841231234').ext.should be_nil
57
+ BigPhoney::PhoneNumber.new('4841231234').ext.should be_nil
54
58
  end
55
59
 
56
60
  it "when 'ext' (no spaces) is used" do
57
- PhoneNumber.new('4841231234ext123').ext.should == '123'
61
+ BigPhoney::PhoneNumber.new('4841231234ext123').ext.should == '123'
58
62
  end
59
63
 
60
64
  it "when ' ext ' (with spaces) is used" do
61
- PhoneNumber.new('4841231234 ext 123').ext.should == '123'
65
+ BigPhoney::PhoneNumber.new('4841231234 ext 123').ext.should == '123'
62
66
  end
63
67
 
64
68
  it "when ' ext. ' (with period) is used" do
65
- PhoneNumber.new('4841231234 ext. 123').ext.should == '123'
69
+ BigPhoney::PhoneNumber.new('4841231234 ext. 123').ext.should == '123'
66
70
  end
67
71
 
68
72
  it "when 'ex' (no spaces) is used" do
69
- PhoneNumber.new('4841231234ex123').ext.should == '123'
73
+ BigPhoney::PhoneNumber.new('4841231234ex123').ext.should == '123'
70
74
  end
71
75
 
72
76
  it "when ' ex ' (with spaces) is used" do
73
- PhoneNumber.new('4841231234 ex 123').ext.should == '123'
77
+ BigPhoney::PhoneNumber.new('4841231234 ex 123').ext.should == '123'
74
78
  end
75
79
 
76
80
  it "when 'x' (no spaces) is used" do
77
- PhoneNumber.new('4841231234x123').ext.should == '123'
81
+ BigPhoney::PhoneNumber.new('4841231234x123').ext.should == '123'
78
82
  end
79
83
 
80
84
  it "when ' x ' (with spaces) is used" do
81
- PhoneNumber.new('4841231234 x 123').ext.should == '123'
85
+ BigPhoney::PhoneNumber.new('4841231234 x 123').ext.should == '123'
82
86
  end
83
87
 
84
88
  it "gets removed from number" do
85
- PhoneNumber.new('4841231234 x 123').number.should == '4841231234'
89
+ BigPhoney::PhoneNumber.new('4841231234 x 123').number.should == '4841231234'
86
90
  end
87
91
  end
88
92
 
89
93
  describe "country code" do
90
94
  context "when in US-only mode" do
91
95
  before(:each) do
92
- PhoneNumber.assume_us = true
96
+ BigPhoney::PhoneNumber.assume_us = true
93
97
  end
94
98
 
95
99
  it "assumes 1" do
96
- phone = PhoneNumber.new('4841231234')
100
+ phone = BigPhoney::PhoneNumber.new('4841231234')
97
101
  phone.country_code.should == '1'
98
102
  phone.number.should == '4841231234'
99
103
  end
100
104
 
101
105
  it "also parses country code as 1" do
102
- phone = PhoneNumber.new('14841231234')
106
+ phone = BigPhoney::PhoneNumber.new('14841231234')
103
107
  phone.country_code.should == '1'
104
108
  phone.number.should == '4841231234'
105
109
  end
@@ -107,29 +111,29 @@ describe PhoneNumber do
107
111
 
108
112
  context "when not in US-only mode" do
109
113
  before(:each) do
110
- PhoneNumber.assume_us = false
114
+ BigPhoney::PhoneNumber.assume_us = false
111
115
  end
112
116
 
113
117
  it "parses 1 digit country code" do
114
- phone = PhoneNumber.new('74841231234')
118
+ phone = BigPhoney::PhoneNumber.new('74841231234')
115
119
  phone.country_code.should == '7'
116
120
  phone.number.should == '4841231234'
117
121
  end
118
122
 
119
123
  it "parses 2 digit country code" do
120
- phone = PhoneNumber.new('204841231234')
124
+ phone = BigPhoney::PhoneNumber.new('204841231234')
121
125
  phone.country_code.should == '20'
122
126
  phone.number.should == '4841231234'
123
127
  end
124
128
 
125
129
  it "parses 3 digit country code" do
126
- phone = PhoneNumber.new('9964841231234')
130
+ phone = BigPhoney::PhoneNumber.new('9964841231234')
127
131
  phone.country_code.should == '996'
128
132
  phone.number.should == '4841231234'
129
133
  end
130
134
 
131
135
  it "parses international type numbers" do
132
- phone = PhoneNumber.new('54-11-4444-3333')
136
+ phone = BigPhoney::PhoneNumber.new('54-11-4444-3333')
133
137
  phone.country_code.should == '54'
134
138
  phone.number.should == '1144443333'
135
139
  end
@@ -138,22 +142,22 @@ describe PhoneNumber do
138
142
 
139
143
  describe "area_code" do
140
144
  it "takes the first 3 digits of number" do
141
- phone = PhoneNumber.new('4841231234')
145
+ phone = BigPhoney::PhoneNumber.new('4841231234')
142
146
  phone.area_code.should == '484'
143
147
  end
144
148
  end
145
149
 
146
150
  describe "formatting" do
147
151
  it "formats with parens by default" do
148
- PhoneNumber.new('4841231234').to_s.should == '(484) 123-1234'
152
+ BigPhoney::PhoneNumber.new('4841231234').to_s.should == '(484) 123-1234'
149
153
  end
150
154
 
151
155
  it "includes country code when not 1" do
152
- PhoneNumber.new('544841231234').to_s.should == '+54 (484) 123-1234'
156
+ BigPhoney::PhoneNumber.new('544841231234').to_s.should == '+54 (484) 123-1234'
153
157
  end
154
158
 
155
159
  it "includes extension" do
156
- PhoneNumber.new('4841231234x123').to_s.should == '(484) 123-1234 ext. 123'
160
+ BigPhoney::PhoneNumber.new('4841231234x123').to_s.should == '(484) 123-1234 ext. 123'
157
161
  end
158
162
  end
159
163
  end
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,10 @@
1
1
  require 'rubygems'
2
- require 'spec'
3
- require 'rr'
2
+
3
+ begin
4
+ require 'spec'
5
+ rescue LoadError
6
+ require 'rspec'
7
+ end
4
8
 
5
9
  require File.dirname(__FILE__) + '/../lib/big_phoney'
6
10
 
7
- Spec::Runner.configure { |c| c.mock_with(:rr) }
metadata CHANGED
@@ -1,21 +1,22 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: big-phoney
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Pat Nakajima
14
+ - Brandon Keene
14
15
  autorequire:
15
16
  bindir: bin
16
17
  cert_chain: []
17
18
 
18
- date: 2010-08-03 00:00:00 -04:00
19
+ date: 2010-08-26 00:00:00 -04:00
19
20
  default_executable:
20
21
  dependencies: []
21
22
 
@@ -34,7 +35,6 @@ files:
34
35
  - lib/big-phoney.rb
35
36
  - lib/big_phoney.rb
36
37
  - lib/big_phoney/.gitignore
37
- - lib/big_phoney/country_codes.rb
38
38
  - spec/big_phoney_spec.rb
39
39
  - spec/spec_helper.rb
40
40
  has_rdoc: true
@@ -1,29 +0,0 @@
1
- class PhoneNumber
2
- COUNTRY_CODES = %w[
3
- 1 7 20 27 30 31 32 33 34
4
- 36 39 40 41 43 44 45 46 47
5
- 48 49 51 52 53 54 55 56 57
6
- 58 60 61 62 63 64 65 66 81
7
- 82 84 86 90 91 92 93 94 95
8
- 98 212 213 216 218 220 221 222 223
9
- 224 225 226 227 228 229 230 231 232
10
- 233 234 235 236 237 238 239 240 241
11
- 242 243 244 245 246 247 248 249 250
12
- 251 252 253 254 255 256 257 258 260
13
- 261 262 263 264 265 266 267 268 269
14
- 290 291 297 298 299 350 351 352 353
15
- 354 355 356 357 358 359 370 371 372
16
- 373 374 375 376 377 378 380 381 385
17
- 386 387 388 389 420 421 423 500 501
18
- 502 503 504 505 506 507 508 509 590
19
- 591 592 593 594 595 596 597 598 599
20
- 670 672 673 674 675 676 677 678 679
21
- 680 681 682 683 684 685 686 687 688
22
- 689 690 691 692 800 808 850 852 853
23
- 655 856 870 871 872 873 874 878 880
24
- 881 882 886 960 961 962 963 964 965
25
- 966 967 968 970 971 972 973 974 975
26
- 976 977 979 991 992 993 994 995 996
27
- 998
28
- ]
29
- end