Class: Telein::Util::Phone
- Inherits:
-
Object
- Object
- Telein::Util::Phone
- 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
-
#matching ⇒ MatchData
readonly
The Matching parts of Phone::FORMAT.
Instance Method Summary collapse
-
#area_code ⇒ String
Returns the area code for phone.
-
#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.
-
#initialize(value) ⇒ Phone
constructor
A new instance of Phone.
-
#number ⇒ String
Returns the number part of phone (everything minus area code).
-
#to_telein_s ⇒ String
Returns a string ready to be consumed by Telein API.
-
#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.
Constructor Details
Instance Attribute Details
#matching ⇒ MatchData (readonly)
The Matching parts of Phone::FORMAT
11 12 13 |
# File 'lib/telein/util/phone.rb', line 11 def matching @matching end |
Instance Method Details
#area_code ⇒ String
Returns the area code for 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
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 |
#number ⇒ String
Returns the number part of phone (everything minus area code)
46 47 48 |
# File 'lib/telein/util/phone.rb', line 46 def number self.matching[:number].delete('-') if self.matching end |
#to_telein_s ⇒ String
Returns a string ready to be consumed by Telein API
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.
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 |