Class: PhoneNumber::Number

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

Constant Summary collapse

REGEX =
/^\+?(\d{0,2})[ \.\-]?\(?(\d{3})\)?[ \.\-]?(\d{3})[ \.\-]?(\d{4})[ x]?(\d*)$/
INVALID_MESSAGE =
"invalid format, try something like: 111-111-1111"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number) ⇒ Number

Returns a new instance of Number.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/phone_number/number.rb', line 7

def initialize( number )
  if number.is_a?( String ) && m = number.match( REGEX )
    @country_code, @area_code, @subscriber_number_prefix, @subscriber_number_postfix, @extension = m[1], m[2], m[3], m[4], m[5]
  elsif number.is_a?( Hash )
    @country_code, @area_code, @subscriber_number_prefix, @subscriber_number_postfix, @extension = number[:country_code], number[:area_code], number[:subscriber_number_prefix], number[:subscriber_number_postfix], number[:extension]
  end

  @pattern_map = {
    /%c/ => (@country_code || '').gsub( /0/, '' ) || "",
    /%C/ => @country_code || "",
    /%a/ => @area_code || "",
    /%m/ => @subscriber_number_prefix || "",
    /%p/ => @subscriber_number_postfix || "",
    /%x/ => @extension || ""
  }

  @@patterns = {
    :us => "%c (%a) %m-%p x %x",
    :us_short => "(%a) %m-%p",
    :nanp_short => "(%a) %m-%p"
  }

  self.freeze
end

Instance Attribute Details

#area_codeObject (readonly)

Returns the value of attribute area_code.



5
6
7
# File 'lib/phone_number/number.rb', line 5

def area_code
  @area_code
end

#country_codeObject (readonly)

Returns the value of attribute country_code.



5
6
7
# File 'lib/phone_number/number.rb', line 5

def country_code
  @country_code
end

#extensionObject (readonly)

Returns the value of attribute extension.



5
6
7
# File 'lib/phone_number/number.rb', line 5

def extension
  @extension
end

#subscriber_number_postfixObject (readonly)

Returns the value of attribute subscriber_number_postfix.



5
6
7
# File 'lib/phone_number/number.rb', line 5

def subscriber_number_postfix
  @subscriber_number_postfix
end

#subscriber_number_prefixObject (readonly)

Returns the value of attribute subscriber_number_prefix.



5
6
7
# File 'lib/phone_number/number.rb', line 5

def subscriber_number_prefix
  @subscriber_number_prefix
end

Class Method Details

.convert(number) ⇒ Object



65
66
67
# File 'lib/phone_number/number.rb', line 65

def self.convert( number )
  parse( number )
end

.parse(number) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/phone_number/number.rb', line 32

def self.parse( number )
  number.gsub!( / x /, 'x' )
  if m = number.match( REGEX )
    cntry_cd = m[1].size == 2 ? m[1] : "0#{m[1]}"
    cntry_cd = '01' if cntry_cd.nil? || cntry_cd.empty? || cntry_cd == '0'
    return Number.new( :country_code => cntry_cd, :area_code => m[2], :subscriber_number_prefix => m[3],
                       :subscriber_number_postfix => m[4], :extension => m[5] )
  end

  Number.new( {} )
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  return (@country_code.nil? || @country_code.empty?) && (@area_code.nil? || @area_code.empty?) && (@subscriber_number_prefix.nil? || @subscriber_number_prefix.empty?) &&
          (@subscriber_number_postfix.nil? || @subscriber_number_postfix.empty?) && (@extension.nil? || @extension.empty?)
end

#rawObject



69
70
71
72
# File 'lib/phone_number/number.rb', line 69

def raw
  ext = (@extension.nil? || @extension.empty?) ? "" : "x#{@extension}"
  "#{@country_code}#{@area_code}#{@subscriber_number_prefix}#{@subscriber_number_postfix}#{ext}"
end

#to_s(pattern = "") ⇒ Object

Creates a phone number based on pattern provided. Defaults to US (NANP) formatting (111) 111-1111.

Symbols:

c - country code
a - area code
m - subscriber prefix
p - subscriber postfix


57
58
59
60
61
62
63
# File 'lib/phone_number/number.rb', line 57

def to_s( pattern="" )
  return '' if self.empty?
  to_return = pattern.is_a?( Symbol ) ? @@patterns[pattern] : pattern
  to_return = @@patterns[:us_short] if to_return.empty?
  @pattern_map.each { |pat, replacement| to_return = to_return.gsub( pat, replacement ) }
  to_return.strip
end