phone_wrangler 0.1.0 → 0.1.1

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -21,6 +21,20 @@ module PhoneWrangler
21
21
  @@default_area_code
22
22
  end
23
23
 
24
+ @@formats = {
25
+ :us => "%c (%a) %m-%p x %x",
26
+ :us_short => "(%a) %m-%p",
27
+ :nanp_short => "(%a) %m-%p"
28
+ }
29
+
30
+ @@pattern_map = {
31
+ /%c/ => :country_code,
32
+ /%a/ => :area_code,
33
+ /%p/ => :prefix,
34
+ /%n/ => :number,
35
+ /%e/ => :extension
36
+ }
37
+
24
38
  #-------------------args-----------------------------------------
25
39
  def initialize(args='')
26
40
  @original = args
@@ -58,15 +72,31 @@ module PhoneWrangler
58
72
  end
59
73
  end
60
74
 
75
+ def empty?
76
+ answer = true
77
+ NUMBER_PARTS.each do |field|
78
+ if self.respond_to?(field)
79
+ field_val = self.send(field)
80
+ answer = answer && (field_val.nil? || field_val.empty?)
81
+ end
82
+ end
83
+ return answer
84
+ end
85
+
61
86
  #------------------------------------------------------------
62
- def to_s(format = nil)
63
- if format.nil?
64
- format = ''
87
+ def to_s(format = '')
88
+ return '' if self.empty?
89
+
90
+ case format
91
+ when Symbol
92
+ format = @@formats[format]
93
+ when ''
65
94
  format += "(%a) " unless area_code.nil? or area_code.empty?
66
95
  format += "%p-" unless prefix.nil? or prefix.empty?
67
96
  format += "%n" unless number.nil? or number.empty?
68
97
  format += " x%e" unless extension.nil? or extension.empty?
69
98
  end
99
+
70
100
  format_number(format)
71
101
  end
72
102
 
@@ -75,7 +105,7 @@ module PhoneWrangler
75
105
  end
76
106
 
77
107
  # TODO: Should #digits method include the extension digits at all? Probably not
78
- # with an 'x' anyway.
108
+ # with an 'x', anyway.
79
109
  def digits
80
110
  digitstring = ''
81
111
  [:area_code, :prefix, :number].each {|part|
@@ -95,15 +125,18 @@ module PhoneWrangler
95
125
  # my $us_phone_regex = '1?\s*\W\s*([2-9][0-8][0-9])\W*([2-9][0-9]{2})\W*([0-9]{4})(\se?x?t?(\d*))?';
96
126
 
97
127
  def parse_from_string(raw_string)
98
- # Optional 1 -./ 256 (opt) 456 -./ 1234 ext(opt) 1234 (opt)
99
- phone_regexp = /1? \s* [.\/-]? \s* [\(]?([2-9][0-8][0-9])?[\)]? \s* [.\/-]? \s* ([2-9][0-9]{2}) \s* [.\/-]? \s* ([0-9]{4}) \s* (\s*e?x?t?\s*(\d+))?/x
128
+ # Optional + 1 -./ 256 (opt) 456 -./ 1234 ext(opt) 1234 (opt)
129
+ phone_regexp = / \+? \s* 1? \s* [.\/-]? \s*
130
+ [\(]?([2-9][0-8]\d)?[\)]? \s* [.\/-]? \s*
131
+ ([2-9]\d{2}) \s* [.\/-]? \s*
132
+ (\d{4}) \s* (?:\s*e?x?t?\s*(\d+))? /x
100
133
  match = phone_regexp.match(raw_string)
101
134
  if ! match.nil?
102
135
  # puts "Setting values #{match.captures.pretty_inspect}"
103
136
  @area_code = match.captures[0]
104
137
  @prefix = match.captures[1]
105
138
  @number = match.captures[2]
106
- @extension = match.captures[4]
139
+ @extension = match.captures[3]
107
140
  else
108
141
  # puts "No matchy :("
109
142
  end
@@ -148,19 +181,14 @@ module PhoneWrangler
148
181
  }
149
182
  end
150
183
 
151
- # This next part borrowed from http://github.com/erebor/phone/blob/master/lib/phone.rb
152
- private
184
+ # This part borrowed from http://github.com/midas/phone_number/blob/master/lib/phone_number/number.rb
185
+ private
153
186
 
154
- def format_number(fmt)
155
- fmt.
156
- # gsub("%c", country_code || "").
157
- gsub("%a", area_code || "").
158
- gsub("%p", prefix || "").
159
- # gsub("%A", area_code_long || "").
160
- gsub("%n", number || "").
161
- # gsub("%f", number1 || "").
162
- # gsub("%l", number2 || "").
163
- gsub("%e", extension || "")
187
+ def format_number(format)
188
+ @@pattern_map.each do |pat, field|
189
+ format = format.gsub( pat, self.send(field) || '' ) if self.respond_to?(field)
190
+ end
191
+ format
164
192
  end
165
193
 
166
194
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{phone_wrangler}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
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"]
@@ -29,6 +29,16 @@ class PhoneWranglerTest < Test::Unit::TestCase
29
29
  assert_equal PhoneNumber, pn.class
30
30
  end
31
31
 
32
+ should "agree it is empty when it is" do
33
+ pn = PhoneNumber.new
34
+ assert pn.empty?
35
+ end
36
+
37
+ should "not agree it is empty when it is not" do
38
+ pn = PhoneNumber.new(:area_code => '256', :prefix => '555', :number => '1234')
39
+ assert ! pn.empty?
40
+ end
41
+
32
42
  should "correctly parse phone number strings" do
33
43
  pn = PhoneNumber.new("(256) 555-1234")
34
44
  assert_equal '256', pn.area_code
@@ -187,6 +197,13 @@ class PhoneWranglerTest < Test::Unit::TestCase
187
197
  assert_equal String, @pn.to_s.class
188
198
  end
189
199
 
200
+ # TODO: I don't know if this is the right behavior, actually. If the user
201
+ # passes in a format string with extra decorative content, does he want it
202
+ # back with only the decorative structure? Or is an empty string better?
203
+ should "return an empty string if the PhoneNumber is empty" do
204
+ assert_equal '', PhoneNumber.new.to_s("x%e foo")
205
+ end
206
+
190
207
  should "format numbers properly with to_s" do
191
208
  assert_equal "(256) 555-1234", @pn.to_s
192
209
  end
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.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Waldron