phone_wrangler 0.1.2 → 0.1.3
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.rdoc +151 -2
- data/VERSION +1 -1
- data/lib/phone_wrangler.rb +10 -34
- data/phone_wrangler.gemspec +2 -2
- data/test/phone_wrangler_test.rb +12 -0
- metadata +2 -2
data/README.rdoc
CHANGED
|
@@ -1,6 +1,155 @@
|
|
|
1
1
|
= phone_wrangler
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The phone_wrangler gem helps me parse, compare and normalize
|
|
4
|
+
NANP[http://en.wikipedia.org/wiki/NANP] phone numbers (basically
|
|
5
|
+
the US, Canada, and some territories). It's not intended to be
|
|
6
|
+
a generalized phone number handler for international numbers.
|
|
7
|
+
There are two good reasons (and one lame one) for this:
|
|
8
|
+
|
|
9
|
+
1. In the apps where I use this, I don't have any international phone numbers
|
|
10
|
+
(nor will I; they're not public-facing web apps).
|
|
11
|
+
2. I need to be able to handle short phone numbers ("555-1111"), which means
|
|
12
|
+
I have to treat a number prefix starting with something-besides-1 as an
|
|
13
|
+
area code or prefix, rather than following the prefix code.
|
|
14
|
+
3. I don't know how to parse the wicked prefix code for
|
|
15
|
+
<A HREF= "http://en.wikipedia.org/wiki/Country_calling_codes">country calling codes</A>.
|
|
16
|
+
|
|
17
|
+
I'll let you decide which of those three is the lame one.
|
|
18
|
+
|
|
19
|
+
= FAQ
|
|
20
|
+
|
|
21
|
+
1) PhoneWrangler or PhoneNumber? Are you stupid or something?
|
|
22
|
+
|
|
23
|
+
Probably. This gem was going to be called phone_number (the class is called PhoneNumber),
|
|
24
|
+
but I was too slow, and midas created the cool phone_number[http://github.com/midas/phone_number]
|
|
25
|
+
gem while I was dorking around. I wanted to extract this anyway (for my own use) and don't
|
|
26
|
+
want to change all my code at the moment to use PhoneWrangler as the class, so there we are.
|
|
27
|
+
|
|
28
|
+
2. So it's more of a lazy thing?
|
|
29
|
+
|
|
30
|
+
Yes. Eventually, I will probably cave in and fix it, which will infuriate *both* the
|
|
31
|
+
loyal fans of phone_wrangler (aka the PhoneNumber wrapper gem).
|
|
32
|
+
|
|
33
|
+
= Installing PhoneWrangler
|
|
34
|
+
|
|
35
|
+
== Requirements
|
|
36
|
+
|
|
37
|
+
Ruby. And a computer, probably.
|
|
38
|
+
|
|
39
|
+
== Install
|
|
40
|
+
|
|
41
|
+
gem sources -a http://gemcutter.org
|
|
42
|
+
sudo gem install phone_wrangler
|
|
43
|
+
|
|
44
|
+
== Installation for Rails
|
|
45
|
+
|
|
46
|
+
If you want to vendor the gem in your Rails app, add this to +config/environment.rb+ :
|
|
47
|
+
|
|
48
|
+
config.gem "phone_number", :version => '0.0.1', :source => 'http://gemcutter.org'
|
|
49
|
+
|
|
50
|
+
Optionally, you can add
|
|
51
|
+
|
|
52
|
+
include PhoneWrangler
|
|
53
|
+
|
|
54
|
+
to get easy access to the PhoneNumber class without having to add the module name,
|
|
55
|
+
like PhoneWrangler::PhoneNumber, which is really too clunky to contemplate, yes?
|
|
56
|
+
|
|
57
|
+
Then run
|
|
58
|
+
|
|
59
|
+
sudo rake:gems:install
|
|
60
|
+
|
|
61
|
+
= Using PhoneWrangler
|
|
62
|
+
|
|
63
|
+
You can create a PhoneNumber object from almost any phone-number-looking string:
|
|
64
|
+
|
|
65
|
+
home = PhoneNumber.new('800/555-2468 x012')
|
|
66
|
+
|
|
67
|
+
or
|
|
68
|
+
|
|
69
|
+
home = PhoneNumber.new('555-2468 ext 12')
|
|
70
|
+
|
|
71
|
+
or
|
|
72
|
+
|
|
73
|
+
home = PhoneNumber.new("202.444.1234")
|
|
74
|
+
|
|
75
|
+
or
|
|
76
|
+
|
|
77
|
+
home = PhoneNumber.new("5551234")
|
|
78
|
+
|
|
79
|
+
plus other variations. Because users enter phone numbers in *my* app with
|
|
80
|
+
reckless abandon in format, I try to accept and parse any form for which I can
|
|
81
|
+
figure out with some confidence which part is which.
|
|
82
|
+
|
|
83
|
+
You can also create a PhoneNumber from a hash:
|
|
84
|
+
|
|
85
|
+
home = PhoneNumber.new(:prefix => '555', :number => '1234', :area_code => '444') # 444-555-1234
|
|
86
|
+
|
|
87
|
+
or
|
|
88
|
+
|
|
89
|
+
home = PhoneNumber.new(:extension => '99', :prefix => '555', :number => '1012') # 555-1012 x 99
|
|
90
|
+
|
|
91
|
+
One of the most useful things (for me) about a PhoneNumber object is it allows me to compare two
|
|
92
|
+
phone numbers in different layouts:
|
|
93
|
+
|
|
94
|
+
pn1 = PhoneNumber.new("555-403-1212")
|
|
95
|
+
pn2 = PhoneNumber.new(" 1 555/403.1212 ")
|
|
96
|
+
|
|
97
|
+
pn1 == pn2 # true
|
|
98
|
+
|
|
99
|
+
pn3 = PhoneNumber.new(:prefix => '403', :number => '1212')
|
|
100
|
+
pn3.area_code = '555'
|
|
101
|
+
pn1 == pn3 # true
|
|
102
|
+
|
|
103
|
+
And did I mention you can set or retrieve any of the PhoneNumber's components with accessors?
|
|
104
|
+
|
|
105
|
+
pn = PhoneNumber.new("555-403-1212")
|
|
106
|
+
pn.extension # nil
|
|
107
|
+
pn.area_code # '555'
|
|
108
|
+
pn.prefix = '444' # "555-444-1212"
|
|
109
|
+
|
|
110
|
+
A PhoneNumber object can return you the original input if you ask nicely:
|
|
111
|
+
|
|
112
|
+
home = PhoneNumber.new(:prefix => '555', :number => '1234', :area_code => '444') # 444-555-1234
|
|
113
|
+
home.raw # { :prefix => '555', :number => '1234', :area_code => '444' }
|
|
114
|
+
home = PhoneNumber.new('555-2468 ext 12')
|
|
115
|
+
home.raw # '555-2468 ext 12'
|
|
116
|
+
|
|
117
|
+
Keep in mind that if you change pieces of PhoneNumber using the accessors, the _actual_ content
|
|
118
|
+
of PhoneNumber will diverge from the original input. But we can't change the past, can we?
|
|
119
|
+
|
|
120
|
+
You can also overwrite the contents of a PhoneNumber object by giving it a new phone number:
|
|
121
|
+
|
|
122
|
+
pn = PhoneNumber.new("555-1212")
|
|
123
|
+
pn.to_s # "555-1212"
|
|
124
|
+
pn.raw = "555-444-4040"
|
|
125
|
+
pn.to_s # "(555) 444-4040"
|
|
126
|
+
pn.raw = {:area_code => '222', :prefix => '555', :number => '0909'}
|
|
127
|
+
pn.to_s # "(222) 555-0909"
|
|
128
|
+
|
|
129
|
+
When you change PhoneNumber _this_ way, it stores the new input as the 'original' input. Gotta
|
|
130
|
+
keep up with the changing times!
|
|
131
|
+
|
|
132
|
+
PhoneNumber also supports a number of named formats (:us, :us_short, :nanp_short at the moment;
|
|
133
|
+
look in the code for +@@formats+), as well as sprintf-style string interpolation, as arguments
|
|
134
|
+
to +to_s+. For example:
|
|
135
|
+
|
|
136
|
+
pn = PhoneNumber.new("404-555-1212 ext 37")
|
|
137
|
+
pn.to_s # "(404) 555-1212 x 37"
|
|
138
|
+
pn.to_s(:us_short) # "(404) 555-1212"
|
|
139
|
+
pn.to_s("Digitz [%a] -->%p~%n") # "Digitz [404] -->555~1212"
|
|
140
|
+
|
|
141
|
+
There's more, but it's not a very long or complicated library, so look at the code!
|
|
142
|
+
|
|
143
|
+
= Adulation
|
|
144
|
+
|
|
145
|
+
All words of adulation, adoration, encouragement, worshipful praise, and awestruck
|
|
146
|
+
respect are most welcome.
|
|
147
|
+
|
|
148
|
+
But since most words are of a very different kind, I suppose I'll have to listen to
|
|
149
|
+
them, too.
|
|
150
|
+
|
|
151
|
+
Send either kind to me here, or find me on Twitter (@erebor) or email me. I'll bet
|
|
152
|
+
you can find my address.
|
|
4
153
|
|
|
5
154
|
== Note on Patches/Pull Requests
|
|
6
155
|
|
|
@@ -15,4 +164,4 @@ Description goes here.
|
|
|
15
164
|
|
|
16
165
|
== Copyright
|
|
17
166
|
|
|
18
|
-
Copyright (c) 2010 Ryan Waldron. See LICENSE for details.
|
|
167
|
+
Copyright (c) 2010 Ryan Waldron (erebor). See LICENSE for details.
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.1.
|
|
1
|
+
0.1.3
|
data/lib/phone_wrangler.rb
CHANGED
|
@@ -5,6 +5,8 @@ module PhoneWrangler
|
|
|
5
5
|
NUMBER_PARTS = [:area_code, :prefix, :number, :extension]
|
|
6
6
|
attr_accessor :area_code, :prefix, :number, :extension
|
|
7
7
|
|
|
8
|
+
attr_reader :original
|
|
9
|
+
|
|
8
10
|
unless defined? @@default_area_code
|
|
9
11
|
@@default_area_code = nil
|
|
10
12
|
end
|
|
@@ -22,7 +24,7 @@ module PhoneWrangler
|
|
|
22
24
|
end
|
|
23
25
|
|
|
24
26
|
@@formats = {
|
|
25
|
-
:us => "%c (%a) %p-%n x %
|
|
27
|
+
:us => "%c (%a) %p-%n x %e",
|
|
26
28
|
:us_short => "(%a) %p-%n",
|
|
27
29
|
:nanp_short => "(%a) %p-%n"
|
|
28
30
|
}
|
|
@@ -37,11 +39,11 @@ module PhoneWrangler
|
|
|
37
39
|
|
|
38
40
|
#-------------------args-----------------------------------------
|
|
39
41
|
def initialize(args='')
|
|
40
|
-
@original = args
|
|
41
42
|
self.raw = args
|
|
42
43
|
end
|
|
43
44
|
|
|
44
45
|
def raw= (args)
|
|
46
|
+
@original = args
|
|
45
47
|
case args
|
|
46
48
|
when String
|
|
47
49
|
parse_from_string(args)
|
|
@@ -91,19 +93,15 @@ module PhoneWrangler
|
|
|
91
93
|
when Symbol
|
|
92
94
|
format = @@formats[format]
|
|
93
95
|
when ''
|
|
94
|
-
format += "(%a) " unless area_code.nil? or area_code.empty?
|
|
95
|
-
format += "%p-" unless prefix.nil? or prefix.empty?
|
|
96
|
-
format += "%n" unless number.nil? or number.empty?
|
|
97
|
-
format += " x%e" unless extension.nil? or extension.empty?
|
|
96
|
+
format += "(%a) " unless @area_code.nil? or @area_code.empty?
|
|
97
|
+
format += "%p-" unless @prefix.nil? or @prefix.empty?
|
|
98
|
+
format += "%n" unless @number.nil? or @number.empty?
|
|
99
|
+
format += " x%e" unless @extension.nil? or @extension.empty?
|
|
98
100
|
end
|
|
99
101
|
|
|
100
102
|
format_number(format)
|
|
101
103
|
end
|
|
102
104
|
|
|
103
|
-
def original
|
|
104
|
-
@original
|
|
105
|
-
end
|
|
106
|
-
|
|
107
105
|
# TODO: Should #digits method include the extension digits at all? Probably not
|
|
108
106
|
# with an 'x', anyway.
|
|
109
107
|
def digits
|
|
@@ -186,7 +184,8 @@ module PhoneWrangler
|
|
|
186
184
|
|
|
187
185
|
def format_number(format)
|
|
188
186
|
@@pattern_map.each do |pat, field|
|
|
189
|
-
|
|
187
|
+
replace_with = ( self.send(field) if self.respond_to?(field) ) || ''
|
|
188
|
+
format = format.gsub( pat, replace_with)
|
|
190
189
|
end
|
|
191
190
|
format
|
|
192
191
|
end
|
|
@@ -194,26 +193,3 @@ module PhoneWrangler
|
|
|
194
193
|
end
|
|
195
194
|
|
|
196
195
|
end
|
|
197
|
-
=begin
|
|
198
|
-
home = PhoneNumber.new('800/555-2468 x012')
|
|
199
|
-
# or
|
|
200
|
-
home = PhoneNumber.new
|
|
201
|
-
home.number = "256-777-7650"
|
|
202
|
-
# or
|
|
203
|
-
# home = PhoneNumber.new
|
|
204
|
-
({:area_code=>'800', :prefix=>'555', :number=>'2020'})
|
|
205
|
-
|
|
206
|
-
puts home.original
|
|
207
|
-
puts home.area_code
|
|
208
|
-
puts home.prefix
|
|
209
|
-
puts home.number
|
|
210
|
-
puts home.extension
|
|
211
|
-
puts home.to_s
|
|
212
|
-
puts home.to_s("(%a) %p-%n")
|
|
213
|
-
puts home.pack({:area_code=>'555', :prefix=>'888', :number=>'1212'})
|
|
214
|
-
p home.unpack
|
|
215
|
-
puts home.pack!({:area_code=>'555', :prefix=>'888', :number=>'1212'})
|
|
216
|
-
puts home.formatted
|
|
217
|
-
|
|
218
|
-
=end
|
|
219
|
-
|
data/phone_wrangler.gemspec
CHANGED
|
@@ -5,11 +5,11 @@
|
|
|
5
5
|
|
|
6
6
|
Gem::Specification.new do |s|
|
|
7
7
|
s.name = %q{phone_wrangler}
|
|
8
|
-
s.version = "0.1.
|
|
8
|
+
s.version = "0.1.3"
|
|
9
9
|
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
11
11
|
s.authors = ["Ryan Waldron"]
|
|
12
|
-
s.date = %q{2010-01-
|
|
12
|
+
s.date = %q{2010-01-14}
|
|
13
13
|
s.description = %q{Handle phone numbers intelligently}
|
|
14
14
|
s.email = %q{rew@erebor.com}
|
|
15
15
|
s.extra_rdoc_files = [
|
data/test/phone_wrangler_test.rb
CHANGED
|
@@ -143,6 +143,12 @@ class PhoneWranglerTest < Test::Unit::TestCase
|
|
|
143
143
|
assert_equal "256", PhoneNumber.default_area_code
|
|
144
144
|
PhoneNumber.default_area_code = nil # put it back, it's a class attr
|
|
145
145
|
end
|
|
146
|
+
|
|
147
|
+
should "set original to new value when re-assigned with raw=" do
|
|
148
|
+
pn = PhoneNumber.new("1-234-567-8901")
|
|
149
|
+
pn.raw = "456-909-8073"
|
|
150
|
+
assert_equal "456-909-8073", pn.original
|
|
151
|
+
end
|
|
146
152
|
end
|
|
147
153
|
|
|
148
154
|
context "With a default_area_code set" do
|
|
@@ -222,6 +228,12 @@ class PhoneWranglerTest < Test::Unit::TestCase
|
|
|
222
228
|
assert_equal " FF 431--4310", @pn.to_s("%a FF %p--%n")
|
|
223
229
|
end
|
|
224
230
|
|
|
231
|
+
should "correctly interpolate named patterns" do
|
|
232
|
+
@pn.extension = "999"
|
|
233
|
+
assert_equal " (256) 555-1234 x 999", @pn.to_s(:us)
|
|
234
|
+
assert_equal "(256) 555-1234", @pn.to_s(:us_short)
|
|
235
|
+
assert_equal "(256) 555-1234", @pn.to_s(:nanp_short)
|
|
236
|
+
end
|
|
225
237
|
|
|
226
238
|
should "return original data" do
|
|
227
239
|
assert_equal @phone_hash, @pn.original
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: phone_wrangler
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ryan Waldron
|
|
@@ -9,7 +9,7 @@ autorequire:
|
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
11
|
|
|
12
|
-
date: 2010-01-
|
|
12
|
+
date: 2010-01-14 00:00:00 -06:00
|
|
13
13
|
default_executable:
|
|
14
14
|
dependencies:
|
|
15
15
|
- !ruby/object:Gem::Dependency
|