Skip to content

Instantly share code, notes, and snippets.

@halan
Created February 16, 2011 12:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halan/829267 to your computer and use it in GitHub Desktop.
Save halan/829267 to your computer and use it in GitHub Desktop.
Classe com funções de decodificação de números de telefones brasileiros
class TelephoneNumber < String
@@phone_er = /^\+?([0-9]{2})? *\(? *0?([0-9]{2}) *\)? *([0-9]{4}) *-? *([0-9]{4})/
attr_accessor :ddi, :ddd, :prefix, :number
def initialize number
number_matched = number.match @@phone_er
self.ddi, self.ddd, self.prefix, self.number = number_matched.captures
self.ddi ||= '55'
super "+#{self.ddi} (#{self.ddd.rjust 3, '0'}) #{self.prefix}-#{self.number}"
end
def clean
self.gsub /[^0-9]/, ''
end
def to_s
self.clean
end
def uf
return if not national?
founded_uf = {
'SP' => 11..19,
'RJ' => 21..24,
'ES' => 27..28,
'MG' => 31..38,
'PR' => 41..46,
'SC' => 47..49,
'RS' => 51..55,
'DF' => [61],
'GO' => [64],
'MT' => 65..66,
'MS' => [67],
'AC' => [68],
'RO' => [69],
'BA' => 71..77,
'SE' => [79],
'PE' => [81, 87],
'AL' => [82],
'PB' => [83],
'RN' => [84],
'CE' => [85],
'PI' => [89],
'PA' => [91, 93, 94],
'AM' => [92, 97],
'RR' => [95],
'AP' => [96],
'MA' => 98..99
}.find do |uf, ddd_range|
ddd_range.include? self.ddd.to_i
end
founded_uf.first if founded_uf
end
def mobile?
not operator.blank?
end
def landline?
(2..5).include? self.prefix.to_i
end
def valid?
not national? or (not self.ddd.blank? and (self.mobile? or self.landline?) and not self.number.blank?)
end
def brazilian?
self.ddi == '55'
end
alias :national? :brazilian?
def operator
return if not national?
walk_on_table special_operators_table or walk_on_table operators_table or (self.prefix.first == '7' and 'Nextel')
end
private
def walk_on_table table
operators_area = table.find do |ddd_range, ops|
ddd_range.include? self.ddd.to_i
end
operator_founded = operators_area.last.find do |operator_with_range|
prefix_range, operator_name = operator_with_range
if prefix_range.kind_of? Range
prefix_range.include? self.prefix[0..(prefix_range.last.to_s.length-1)].to_i
else
self.prefix =~ prefix_range
end
end
operator_founded.last if operator_founded
end
def operators_table
{
21..28 => [[96..99, 'Vivo'], [91..94, 'Claro'], [86..88, 'Oi'], [80..83, 'Tim']],
91..99 => [[96..99, 'Amazônia Celular'], [91..94, 'Vivo'], [86..88, 'Oi'], [80..83, 'Tim']],
31..38 => [[96..99, 'Telemig Celular'], [91..94, 'Tim'], [86..88, 'Oi'], [82..84, 'Claro']],
71..79 => [[96..99, 'Vivo'], [91..94, 'Tim'], [86..88, 'Oi'], [81..82, 'Claro']],
81..89 => [[96..99, 'Tim'], [91..94, 'Claro'], [86..88, 'Oi'], [/^8/, 'Vivo']],
41..49 => [[96..99, 'Tim'], [91..94, 'Vivo'], [87..88, 'Claro'], [84..85, 'Brasil Telecom']],
51..55 => [[96..99, 'Vivo'], [91..94, 'Claro'], [81..82, 'Tim'], [84..85, 'Brasil Telecom']],
61..69 => [[96..99, 'Vivo'], [91..94, 'Claro'], [81..82, 'Tim'], [84..85, 'Brasil Telecom']],
11..19 => [[96..99, 'Vivo'], [91..94, 'Claro'], [81..87, 'Tim'], [/^8/, 'Oi']]
}
end
def special_operators_table
{
11..19 => [[6340..6369, 'Tim'], [7011..7051, 'Tim'], [7950..7967, 'Tim'], [6310..6339, 'Claro'], [6589..6599, 'Claro'], [7052..7062, 'Claro'], [/^(76|89)/, 'Claro'], [8800..8899, 'Claro'], [91..94, 'Claro'], [/^6193/, 'Vivo'], [6370..6499, 'Vivo'], [71..75, 'Vivo'], [/^95/, 'Vivo'], [/^6299/, 'Oi'], [/^65/, 'Oi'], [6651..6799, 'Oi'], [7971..7999, 'Oi'], [/^80/, 'Oi'], [8814..8889, 'Oi'], [7900..7949, 'Unicel']],
53..53 => [[9911..9939, 'Tim']],
43..43 => [[81..81, 'Tim'], [9941-9998, 'Sercomtel']],
81..89 => [[8100..8188, 'Vivo'], [/^85/, 'Oi']],
21..24 => [[74..76, 'Claro'], [/^(71|95)/, 'Vivo'], [84..85, 'Oi'], [/^89/, 'Oi']],
61..69 => [[9551..9559, 'Claro'], [9981..9989, 'Claro'], [/^86/, 'BrT']],
91..99 => [[/^84/, 'Claro']],
31..38 => [[85..89, 'Oi'], [/^96/,'CTBC'], [9960..9979, 'CTBC'], [9991..9999, 'CTBC']]
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment