big-phoney 0.0.4 → 0.0.5
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/big-phoney.gemspec +2 -2
- data/lib/big_phoney/phone_number.rb +120 -0
- metadata +4 -3
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.
|
5
|
+
s.version = "0.0.5"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Pat Nakajima", "Brandon Keene"]
|
9
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"]
|
10
|
+
s.files = ["LICENSE", "README.md", "big-phoney.gemspec", "lib/big-phoney.rb", "lib/big_phoney.rb", "lib/big_phoney/phone_number.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}
|
@@ -0,0 +1,120 @@
|
|
1
|
+
module BigPhoney
|
2
|
+
class PhoneNumber
|
3
|
+
MIN_US_LENGTH = 10
|
4
|
+
MINLEN = 7
|
5
|
+
EXTENSION = /\s*(?:(?:ext|ex|xt|x)[\s.:]*(\d+))/i
|
6
|
+
COUNTRY_CODES = %w[
|
7
|
+
1 7 20 27 30 31 32 33 34
|
8
|
+
36 39 40 41 43 44 45 46 47
|
9
|
+
48 49 51 52 53 54 55 56 57
|
10
|
+
58 60 61 62 63 64 65 66 81
|
11
|
+
82 84 86 90 91 92 93 94 95
|
12
|
+
98 212 213 216 218 220 221 222 223
|
13
|
+
224 225 226 227 228 229 230 231 232
|
14
|
+
233 234 235 236 237 238 239 240 241
|
15
|
+
242 243 244 245 246 247 248 249 250
|
16
|
+
251 252 253 254 255 256 257 258 260
|
17
|
+
261 262 263 264 265 266 267 268 269
|
18
|
+
290 291 297 298 299 350 351 352 353
|
19
|
+
354 355 356 357 358 359 370 371 372
|
20
|
+
373 374 375 376 377 378 380 381 385
|
21
|
+
386 387 388 389 420 421 423 500 501
|
22
|
+
502 503 504 505 506 507 508 509 590
|
23
|
+
591 592 593 594 595 596 597 598 599
|
24
|
+
670 672 673 674 675 676 677 678 679
|
25
|
+
680 681 682 683 684 685 686 687 688
|
26
|
+
689 690 691 692 800 808 850 852 853
|
27
|
+
655 856 870 871 872 873 874 878 880
|
28
|
+
881 882 886 960 961 962 963 964 965
|
29
|
+
966 967 968 970 971 972 973 974 975
|
30
|
+
976 977 979 991 992 993 994 995 996
|
31
|
+
998
|
32
|
+
]
|
33
|
+
|
34
|
+
def self.assume_us? ; @assume_us end
|
35
|
+
def self.assume_us=(us) ; @assume_us = us end
|
36
|
+
|
37
|
+
attr_accessor :country_code, :ext, :number
|
38
|
+
|
39
|
+
def initialize(number)
|
40
|
+
@number = number.to_s.strip
|
41
|
+
parse_extension
|
42
|
+
parse_country_code
|
43
|
+
end
|
44
|
+
|
45
|
+
def to_s
|
46
|
+
result = '(%s) %s-%s' % [area_code, prefix, rest]
|
47
|
+
|
48
|
+
if country_code != '1'
|
49
|
+
result = '+%s %s' % [country_code, result]
|
50
|
+
end
|
51
|
+
|
52
|
+
if extension
|
53
|
+
result = '%s ext. %s' % [result, extension]
|
54
|
+
end
|
55
|
+
|
56
|
+
result
|
57
|
+
end
|
58
|
+
|
59
|
+
def valid?
|
60
|
+
errors.empty?
|
61
|
+
end
|
62
|
+
|
63
|
+
def area_code
|
64
|
+
number[0..2]
|
65
|
+
end
|
66
|
+
|
67
|
+
def prefix
|
68
|
+
number[3..5]
|
69
|
+
end
|
70
|
+
|
71
|
+
def rest
|
72
|
+
number[6..9]
|
73
|
+
end
|
74
|
+
|
75
|
+
def errors
|
76
|
+
@errors ||= []
|
77
|
+
end
|
78
|
+
|
79
|
+
alias_method :extension, :ext
|
80
|
+
|
81
|
+
private
|
82
|
+
|
83
|
+
def parse_extension
|
84
|
+
@number =~ EXTENSION
|
85
|
+
if $1 && $1.size > 4
|
86
|
+
errors << :extension
|
87
|
+
else
|
88
|
+
@ext = $1
|
89
|
+
@number.sub!(EXTENSION, '')
|
90
|
+
end
|
91
|
+
|
92
|
+
@number.gsub!(/\D/, '')
|
93
|
+
@number.sub!(/^0+/, '')
|
94
|
+
end
|
95
|
+
|
96
|
+
def parse_country_code
|
97
|
+
if @number.size == 10
|
98
|
+
@number = "1#{@number}"
|
99
|
+
end
|
100
|
+
|
101
|
+
0.upto(2) do |len|
|
102
|
+
break if @country_code
|
103
|
+
if COUNTRY_CODES.include?(country_code = @number[0..len])
|
104
|
+
@country_code ||= country_code
|
105
|
+
@number.sub!(/^#{country_code}/, '')
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
if @country_code.nil?
|
110
|
+
errors << :country_code
|
111
|
+
end
|
112
|
+
|
113
|
+
if @country_code == '1'
|
114
|
+
errors << :too_short if @number.size < MIN_US_LENGTH
|
115
|
+
else
|
116
|
+
errors << :too_short if @number.size <= MINLEN
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: big-phoney
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 5
|
10
|
+
version: 0.0.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Pat Nakajima
|
@@ -34,6 +34,7 @@ files:
|
|
34
34
|
- big-phoney.gemspec
|
35
35
|
- lib/big-phoney.rb
|
36
36
|
- lib/big_phoney.rb
|
37
|
+
- lib/big_phoney/phone_number.rb
|
37
38
|
- lib/big_phoney/.gitignore
|
38
39
|
- spec/big_phoney_spec.rb
|
39
40
|
- spec/spec_helper.rb
|