Class: Normalic::PhoneNumber

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

Overview

only handles U.S. phone numbers

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fields = {}) ⇒ PhoneNumber

Returns a new instance of PhoneNumber.



6
7
8
9
10
# File 'lib/normalic/phone_number.rb', line 6

def initialize(fields={})
  @npa = fields[:npa]
  @nxx = fields[:nxx]
  @slid = fields[:slid]
end

Instance Attribute Details

#npaObject

Returns the value of attribute npa.



4
5
6
# File 'lib/normalic/phone_number.rb', line 4

def npa
  @npa
end

#nxxObject

Returns the value of attribute nxx.



4
5
6
# File 'lib/normalic/phone_number.rb', line 4

def nxx
  @nxx
end

#slidObject

Returns the value of attribute slid.



4
5
6
# File 'lib/normalic/phone_number.rb', line 4

def slid
  @slid
end

Class Method Details

.parse(raw) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/normalic/phone_number.rb', line 12

def self.parse(raw)
  digs = raw.to_s.gsub(/[^\d]/,'')
  while digs != (trim = digs.gsub(/^[01]/,''))
    digs = trim
  end
  if digs.length < 10
    return nil
  end
  self.new(:npa => digs[0,3],
           :nxx => digs[3,3],
           :slid => digs[6,4])
end

Instance Method Details

#==(other) ⇒ Object



45
46
47
# File 'lib/normalic/phone_number.rb', line 45

def ==(other)
  self.to_s == other.to_s ? true : false
end

#[](field_name) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/normalic/phone_number.rb', line 29

def [](field_name)
  begin
    self.send(field_name.to_s)
  rescue NoMethodError => e
    nil
  end
end

#[]=(field_name, value) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/normalic/phone_number.rb', line 37

def []=(field_name, value)
  begin
    self.send("#{field_name}=", value)
  rescue NoMethodError => e
    nil
  end
end

#to_sObject



25
26
27
# File 'lib/normalic/phone_number.rb', line 25

def to_s
  "#{npa} #{nxx} #{slid}"
end