Class: Telein::Util::Phone

Inherits:
Object
  • Object
show all
Defined in:
lib/telein/util/phone.rb

Overview

Class that encapsulates phone data logic

Constant Summary collapse

FORMAT =

General format of a Brazilian phone. An area_code and a number that may contain and extra 9 Valid area codes are ruled by Resolution nº 263 from Anatel: http://legislacao.anatel.gov.br/resolucoes/16-2001/383-resolucao-263

/\A\(?(?<area_code>[14689][1-9]|[23][12478]|[357][1345]|77|79)\)?\s*(?<number>(((?<extra_digit>9?)\d)|[2-9])\d{3}\-?\d{4})\Z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Phone

Returns a new instance of Phone.

Parameters:

  • value (String)

    a string containing phone information



20
21
22
# File 'lib/telein/util/phone.rb', line 20

def initialize(value)
  @matching = value.match(FORMAT) if value
end

Instance Attribute Details

#matchingMatchData (readonly)

The Matching parts of Phone::FORMAT

Returns:

  • (MatchData)

    A +MatchData+ object containing the matching made against value

See Also:



11
12
13
# File 'lib/telein/util/phone.rb', line 11

def matching
  @matching
end

Instance Method Details

#area_codeString

Returns the area code for phone

Returns:

  • (String)

    the area code of phone



39
40
41
# File 'lib/telein/util/phone.rb', line 39

def area_code
  self.matching[:area_code] if self.matching
end

#has_extra_digit?Boolean

Checks if phone has an extra digit The extra digit '9' was introduced only to phones in 11 area code (São Paulo) to account for new phones

Returns:

  • (Boolean)

    whether phone has an extra 9



55
56
57
58
# File 'lib/telein/util/phone.rb', line 55

def has_extra_digit?
  return false unless self.matching
  self.matching[:extra_digit] == '9' ? true : false
end

#numberString

Returns the number part of phone (everything minus area code)

Returns:

  • (String)

    the number part of phone



46
47
48
# File 'lib/telein/util/phone.rb', line 46

def number
  self.matching[:number].delete('-') if self.matching
end

#to_telein_sString

Returns a string ready to be consumed by Telein API

Examples:

Return a formatted phone

phone = Telein::Util::Phone.new('(12) 3434-5656')
phone.to_telein_s # => '1234345656'

Returns:

  • (String)

    a string in Telein format



30
31
32
33
34
# File 'lib/telein/util/phone.rb', line 30

def to_telein_s
  return nil.to_s unless self.valid?

  [self.area_code,self.number].join
end

#valid?Boolean

Checks if phone is valid For phones in the 1[1-9] and 2[1-9] areas, the method may return false positives, since only cellphones have that extra digit as of Q2 of 2014.

Returns:

  • (Boolean)

    whether phone is valid or invalid



65
66
67
68
69
70
71
# File 'lib/telein/util/phone.rb', line 65

def valid?
  if self.area_code && self.number
    true
  else
    false
  end
end