class Telein::Util::Phone

Class that encapsulates phone data logic

Constants

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: legislacao.anatel.gov.br/resolucoes/16-2001/383-resolucao-263

Attributes

matching[R]

The Matching parts of Phone::FORMAT

@return [MatchData] A MatchData object containing the matching made against value @see initialize

Public Class Methods

new(value) click to toggle source

@param value [String] a string containing phone information

# File lib/telein/util/phone.rb, line 20
def initialize(value)
  @matching = value.match(FORMAT) if value
end

Public Instance Methods

area_code() click to toggle source

Returns the area code for phone

@return [String] the area code of phone

# File lib/telein/util/phone.rb, line 39
def area_code
  self.matching[:area_code] if self.matching
end
has_extra_digit?() click to toggle source

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

@return [Boolean] whether phone has an extra 9

# 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
number() click to toggle source

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

@return [String] the number part of phone

# File lib/telein/util/phone.rb, line 46
def number
  self.matching[:number].delete('-') if self.matching
end
to_telein_s() click to toggle source

Returns a string ready to be consumed by Telein API @example Return a formatted phone

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

@return [String] a string in Telein format

# 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?() click to toggle source

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

@return [Boolean] whether phone is valid or invalid

# File lib/telein/util/phone.rb, line 65
def valid?
  if self.area_code && self.number
    true
  else
    false
  end
end