Class: PhoneWrangler::PhoneNumber

Inherits:
Object
  • Object
show all
Defined in:
lib/phone_wrangler.rb

Constant Summary collapse

NUMBER_PARTS =
[:area_code, :prefix, :number, :extension]
@@default_area_code =
nil
@@formats =
{
        :us => "%c (%a) %p-%n x %e",
        :us_short => "(%a) %p-%n",
        :nanp_short => "(%a) %p-%n"
}
@@pattern_map =
{
        /%c/ => :country_code,
        /%a/ => :area_code,
        /%p/ => :prefix,
        /%n/ => :number,
        /%e/ => :extension
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = '') ⇒ PhoneNumber

——————-args—————————————–



41
42
43
# File 'lib/phone_wrangler.rb', line 41

def initialize(args='')
  self.raw = args
end

Instance Attribute Details

#area_codeObject

Returns the value of attribute area_code.



6
7
8
# File 'lib/phone_wrangler.rb', line 6

def area_code
  @area_code
end

#extensionObject

Returns the value of attribute extension.



6
7
8
# File 'lib/phone_wrangler.rb', line 6

def extension
  @extension
end

#numberObject

Returns the value of attribute number.



6
7
8
# File 'lib/phone_wrangler.rb', line 6

def number
  @number
end

#originalObject (readonly)

Returns the value of attribute original.



8
9
10
# File 'lib/phone_wrangler.rb', line 8

def original
  @original
end

#prefixObject

Returns the value of attribute prefix.



6
7
8
# File 'lib/phone_wrangler.rb', line 6

def prefix
  @prefix
end

Class Method Details

.default_area_codeObject



14
15
16
# File 'lib/phone_wrangler.rb', line 14

def self.default_area_code
  @@default_area_code
end

.default_area_code=(val) ⇒ Object



18
19
20
# File 'lib/phone_wrangler.rb', line 18

def self.default_area_code=(val)
  @@default_area_code = val
end

Instance Method Details

#==(other) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/phone_wrangler.rb', line 66

def == other
  case other
  when PhoneNumber
    self.unpack == other.unpack
  when String, Hash
    self.unpack == PhoneNumber.new(other).unpack
  else
    false
  end
end

#default_area_codeObject



22
23
24
# File 'lib/phone_wrangler.rb', line 22

def default_area_code
  @@default_area_code
end

#digitsObject

TODO: Should #digits method include the extension digits at all? Probably not with an ‘x’, anyway.



107
108
109
110
111
112
113
114
# File 'lib/phone_wrangler.rb', line 107

def digits
  digitstring = ''
  [:area_code, :prefix, :number].each {|part|
    digitstring += self.send(part).to_s unless self.send(part).nil?
  }
  digitstring += " x#{extension}" unless extension.nil?
  digitstring
end

#empty?Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
86
# File 'lib/phone_wrangler.rb', line 77

def empty?
  answer = true
  NUMBER_PARTS.each do |field|
    if self.respond_to?(field)
      field_val = self.send(field)
      answer = answer && (field_val.nil? || field_val.empty?)
    end
  end
  return answer
end

#has_area_code?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/phone_wrangler.rb', line 62

def has_area_code?
  ! area_code.nil?
end

#pack(args) ⇒ Object




148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/phone_wrangler.rb', line 148

def pack(args)

  phArea    = ''
  phPrefix  = ''
  phNumber  = ''
  phExtension  = ''

  if args.size >= 3
    phArea    = args[0].to_s
    phPrefix  = args[1].to_s
    phNumber  = args[3].to_s
    if args.size == 4
      phExtension  = args[4].to_s
    end
  end

  return phArea + phPrefix + phNumber + phExtension
end

#pack!(args) ⇒ Object




168
169
170
# File 'lib/phone_wrangler.rb', line 168

def pack!(args)
  @phone_number = pack(args)
end

#parse_from_string(raw_string) ⇒ Object

There are lots of regexp-for-phone-number dissussions, but I found this one most useful:

http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation

Nice discussion here, and it had this one, which became the germ of mine. I added optional parentheses around the area code, the /x and spacing for readability, and changed out W for

./-

for the delimiters to tighten what I’d accept a little bit.

The original was (in perl)

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*))?';


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/phone_wrangler.rb', line 125

def parse_from_string(raw_string)
  # Optional +  1 -./ 256 (opt) 456 -./ 1234 ext(opt) 1234 (opt)
  phone_regexp = / \+? \s* 1? \s* [.\/-]? \s*
                   [\(]?([2-9][0-8]\d)?[\)]? \s* [.\/-]? \s*
                   ([2-9]\d{2}) \s* [.\/-]? \s*
                   (\d{4}) \s* (?:\s*e?x?t?\s*(\d+))? /x
  match = phone_regexp.match(raw_string)
  if ! match.nil?
    # puts "Setting values #{match.captures.pretty_inspect}"
    @area_code = match.captures[0]
    @prefix = match.captures[1]
    @number = match.captures[2]
    @extension = match.captures[3]
  else
    # puts "No matchy :("
  end

  if ! default_area_code.nil? and ( @area_code.nil? or @area_code.empty? )
    @area_code = default_area_code
  end
end

#raw=(args) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/phone_wrangler.rb', line 45

def raw= (args)
  @original = args
  case args
  when String
    parse_from_string(args)
  when Hash
    args = { :area_code => PhoneNumber.default_area_code }.merge(args)
    NUMBER_PARTS.each do |key|
      send("#{key}=", args[key]) if args[key]
    end
  when Array
    self.pack!(args)
  else
    raise ArgumentError.new("Sorry, can't handle arguments of type #{args.class}")
  end
end

#to_s(format = '') ⇒ Object




89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/phone_wrangler.rb', line 89

def to_s(format = '')
  return '' if self.empty?

  case format
  when Symbol
    format = @@formats[format]
  when ''
    format += "(%a) " unless @area_code.nil? or @area_code.empty?
    format += "%p-" unless @prefix.nil? or @prefix.empty?
    format += "%n" unless @number.nil? or @number.empty?
    format += " x%e" unless @extension.nil? or @extension.empty?
  end

  format_number(format)
end

#unpackObject




173
174
175
176
177
178
179
180
# File 'lib/phone_wrangler.rb', line 173

def unpack
  return {
    :area_code => area_code,
    :prefix => prefix,
    :number => number,
    :extension => extension
  }
end