telefonia_br 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/rules.rb +194 -0
  3. data/lib/telefonia_br.rb +108 -0
  4. metadata +61 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b68a87e6bfba7ba92228182e261d65c578b13117
4
+ data.tar.gz: 5a9295da6e1ce86cb7e61b03e696db1d32a76a80
5
+ SHA512:
6
+ metadata.gz: adfe0470399e32fa1900704cd35757102b653119bac139e6af427eec5c6979d316e86ab7cc9a700dff74e780cd5339f7853d5ef8449ffa3ba802d2f8f314d454
7
+ data.tar.gz: 1e771b980ac7043d66578610ea87b97c9a436f3c72c7caad8ca181176d5d394a5719bf350c00d7881479c099a99a027660b668ca61f417c137e80e415c4dfdcd
data/lib/rules.rb ADDED
@@ -0,0 +1,194 @@
1
+ DDDS = {
2
+ '11' => { ninth_digit: true, state: 'SP', region: 'Região Metropolitana de São Paulo.' },
3
+ '12' => { ninth_digit: true, state: 'SP', region: 'São José dos Campos e Região.' },
4
+ '13' => { ninth_digit: true, state: 'SP', region: 'Região Metropolitana da Baixada Santista.' },
5
+ '14' => { ninth_digit: true, state: 'SP', region: 'Bauru, Jaú, Marília, Botucatu e Região.' },
6
+ '15' => { ninth_digit: true, state: 'SP', region: 'Sorocaba e Região.' },
7
+ '16' => { ninth_digit: true, state: 'SP', region: 'Ribeirão Preto, São Carlos, Araraquara e Região.' },
8
+ '17' => { ninth_digit: true, state: 'SP', region: 'São José do Rio Preto e Região.' },
9
+ '18' => { ninth_digit: true, state: 'SP', region: 'Presidente Prudente, Araçatuba e Região.' },
10
+ '19' => { ninth_digit: true, state: 'SP', region: 'Região Metropolitana de Campinas.' },
11
+ '21' => { ninth_digit: true, state: 'RJ', region: 'Região Metropolitana do Rio de Janeiro.' },
12
+ '22' => { ninth_digit: true, state: 'RJ', region: 'Campos dos Goytacazes e Região.' },
13
+ '24' => { ninth_digit: true, state: 'RJ', region: 'Volta Redonda, Petrópolis e Região.' },
14
+ '27' => { ninth_digit: true, state: 'ES', region: 'Região Metropolitana de Vitória.' },
15
+ '28' => { ninth_digit: true, state: 'ES', region: 'Cachoeiro de Itapemirim e Região.' },
16
+ '31' => { ninth_digit: false, state: 'MG', region: 'Região Metropolitana de Belo Horizonte.' },
17
+ '32' => { ninth_digit: false, state: 'MG', region: 'Juiz de Fora e Região.' },
18
+ '33' => { ninth_digit: false, state: 'MG', region: 'Governador Valadares e Região.' },
19
+ '34' => { ninth_digit: false, state: 'MG', region: 'Uberlândia e região.' },
20
+ '35' => { ninth_digit: false, state: 'MG', region: 'Poços de Caldas, Pouso Alegre, Varginha e Região.' },
21
+ '37' => { ninth_digit: false, state: 'MG', region: 'Divinópolis, Itaúna e Região.' },
22
+ '38' => { ninth_digit: false, state: 'MG', region: 'Montes Claros e Região.' },
23
+ '41' => { ninth_digit: false, state: 'PR', region: 'Região Metropolitana de Curitiba.' },
24
+ '42' => { ninth_digit: false, state: 'PR', region: 'Ponta Grossa e Região.' },
25
+ '43' => { ninth_digit: false, state: 'PR', region: 'Londrina e Região.' },
26
+ '44' => { ninth_digit: false, state: 'PR', region: 'Maringá e Região.' },
27
+ '45' => { ninth_digit: false, state: 'PR', region: 'Cascavel e Região.' },
28
+ '46' => { ninth_digit: false, state: 'PR', region: 'Francisco Beltrão, Pato Branco e Região.' },
29
+ '47' => { ninth_digit: false, state: 'SC', region: 'Joinville, Blumenau, Balneário Camboriú e Região.' },
30
+ '48' => { ninth_digit: false, state: 'SC', region: 'Região Metropolitana de Florianópolis e Criciúma.' },
31
+ '49' => { ninth_digit: false, state: 'SC', region: 'Chapecó, Lages e Região.' },
32
+ '51' => { ninth_digit: false, state: 'RS', region: 'Região Metropolitana de Porto Alegre.' },
33
+ '53' => { ninth_digit: false, state: 'RS', region: 'Pelotas e Região.' },
34
+ '54' => { ninth_digit: false, state: 'RS', region: 'Caxias do Sul e Região.' },
35
+ '55' => { ninth_digit: false, state: 'RS', region: 'Santa Maria e Região.' },
36
+ '61' => { ninth_digit: false, state: 'DF', region: 'Brasília e Região.' },
37
+ '62' => { ninth_digit: false, state: 'GO', region: 'Região Metropolitana de Goiânia.' },
38
+ '63' => { ninth_digit: false, state: 'TO', region: 'Todos os municípios do estado.' },
39
+ '64' => { ninth_digit: false, state: 'GO', region: 'Rio Verde e Região.' },
40
+ '65' => { ninth_digit: false, state: 'MT', region: 'Região Metropolitana de Cuiabá.' },
41
+ '66' => { ninth_digit: false, state: 'MT', region: '' },
42
+ '67' => { ninth_digit: false, state: 'MS', region: 'Todos os municípios do estado.' },
43
+ '68' => { ninth_digit: false, state: 'AC', region: 'Todos os municípios do estado.' },
44
+ '69' => { ninth_digit: false, state: 'RO', region: 'Todos os municípios do estado.' },
45
+ '71' => { ninth_digit: false, state: 'BA', region: 'Região Metropolitana de Salvador.' },
46
+ '73' => { ninth_digit: false, state: 'BA', region: 'Itabuna, Ilhéus e Região.' },
47
+ '74' => { ninth_digit: false, state: 'BA', region: 'Juazeiro e Região.' },
48
+ '75' => { ninth_digit: false, state: 'BA', region: 'Feira de Santana e Região.' },
49
+ '77' => { ninth_digit: false, state: 'BA', region: 'Vitória da Conquista e Região.' },
50
+ '79' => { ninth_digit: false, state: 'SE', region: 'Todos os municípios do estado.' },
51
+ '81' => { ninth_digit: false, state: 'PE', region: 'Região Metropolitana de Recife.' },
52
+ '82' => { ninth_digit: false, state: 'AL', region: 'Todos os municípios do estado.' },
53
+ '83' => { ninth_digit: false, state: 'PB', region: 'Todos os municípios do estado.' },
54
+ '84' => { ninth_digit: false, state: 'RN', region: 'Todos os municípios do estado.' },
55
+ '85' => { ninth_digit: false, state: 'CE', region: 'Região Metropolitana de Fortaleza.' },
56
+ '86' => { ninth_digit: false, state: 'PI', region: 'Região de Teresina.' },
57
+ '87' => { ninth_digit: false, state: 'PE', region: 'Região de Petrolina.' },
58
+ '88' => { ninth_digit: false, state: 'CE', region: 'Região de Juazeiro do Norte.' },
59
+ '89' => { ninth_digit: false, state: 'PI', region: 'Região de Picos e Floriano.' },
60
+ '91' => { ninth_digit: false, state: 'PA', region: 'Região Metropolitana de Belém.' },
61
+ '92' => { ninth_digit: false, state: 'AM', region: 'Região de Manaus.' },
62
+ '93' => { ninth_digit: false, state: 'PA', region: 'Região de Santarém.' },
63
+ '94' => { ninth_digit: false, state: 'PA', region: 'Região de Marabá.' },
64
+ '95' => { ninth_digit: false, state: 'RR', region: 'Todos os municípios do estado.' },
65
+ '96' => { ninth_digit: false, state: 'AP', region: 'Todos os municípios do estado.' },
66
+ '97' => { ninth_digit: false, state: 'AM', region: 'Região de Tefé e Coari.' },
67
+ '98' => { ninth_digit: false, state: 'MA', region: 'Região Metropolitana de São Luís.' },
68
+ '99' => { ninth_digit: false, state: 'MA', region: 'Região de Imperatriz.' }
69
+ }
70
+
71
+ band_A_and_B = (96..99).to_a + (91..94).to_a
72
+
73
+ MOBILE_BANDS = {
74
+ #####################################################
75
+ # STATE | BAND A and B | BAND D | BAND E #
76
+ #####################################################
77
+ 'SP' => (band_A_and_B + (81..87).to_a + (88..89).to_a).uniq,
78
+ 'RJ' => (band_A_and_B + (87..88).to_a + (80..83).to_a).uniq,
79
+ 'ES' => (band_A_and_B + (87..88).to_a + (80..83).to_a).uniq,
80
+ 'MG' => (band_A_and_B + (87..88).to_a + (81..84).to_a).uniq,
81
+ 'PR' => (band_A_and_B + (87..88).to_a + (84..85).to_a).uniq,
82
+ 'SC' => (band_A_and_B + (87..88).to_a + (84..85).to_a).uniq,
83
+ 'RS' => (band_A_and_B + (81..83).to_a + (84..85).to_a).uniq,
84
+ 'DF' => (band_A_and_B + (81..83).to_a + (84..85).to_a).uniq,
85
+ 'GO' => (band_A_and_B + (81..83).to_a + (84..85).to_a).uniq,
86
+ 'TO' => (band_A_and_B + (81..83).to_a + (84..85).to_a).uniq,
87
+ 'MT' => (band_A_and_B + (81..83).to_a + (84..85).to_a).uniq,
88
+ 'MS' => (band_A_and_B + (81..83).to_a + (84..85).to_a).uniq,
89
+ 'AC' => (band_A_and_B + (81..83).to_a + (84..85).to_a).uniq,
90
+ 'RO' => (band_A_and_B + (81..83).to_a + (84..85).to_a).uniq,
91
+ 'BA' => (band_A_and_B + (87..88).to_a + (81..83).to_a).uniq,
92
+ 'SE' => (band_A_and_B + (87..88).to_a + (81..83).to_a).uniq,
93
+ 'PE' => (band_A_and_B + (87..88).to_a + (80..89).to_a).uniq,
94
+ 'AL' => (band_A_and_B + (87..88).to_a + (80..89).to_a).uniq,
95
+ 'PB' => (band_A_and_B + (87..88).to_a + (80..89).to_a).uniq,
96
+ 'RN' => (band_A_and_B + (87..88).to_a + (80..89).to_a).uniq,
97
+ 'CE' => (band_A_and_B + (87..88).to_a + (80..89).to_a).uniq,
98
+ 'PI' => (band_A_and_B + (87..88).to_a + (80..89).to_a).uniq,
99
+ 'PA' => (band_A_and_B + (87..88).to_a + (80..83).to_a).uniq,
100
+ 'AM' => (band_A_and_B + (87..88).to_a + (80..83).to_a).uniq,
101
+ 'RR' => (band_A_and_B + (87..88).to_a + (80..83).to_a).uniq,
102
+ 'AP' => (band_A_and_B + (87..88).to_a + (80..83).to_a).uniq,
103
+ 'MA' => (band_A_and_B + (87..88).to_a + (80..83).to_a).uniq
104
+ }
105
+
106
+ MOBILE_SPECIAL_CASES_FIRST_2_DIGITS = {
107
+ 'SP' => [76, 89, 91, 92, 93, 94, 71, 72, 73, 74, 75, 95, 65, 68, 80, 99].uniq,
108
+ 'RJ' => [74, 75, 76, 71, 72, 95, 84, 85, 86, 89].uniq,
109
+ 'ES' => [],
110
+ 'MG' => [85, 86, 89, 96].uniq,
111
+ 'PR' => [81].uniq,
112
+ 'SC' => [],
113
+ 'RS' => [95].uniq,
114
+ 'DF' => [86, 99].uniq,
115
+ 'GO' => [86, 99].uniq,
116
+ 'TO' => [86, 99].uniq,
117
+ 'MT' => [86, 99].uniq,
118
+ 'MS' => [86, 99].uniq,
119
+ 'AC' => [86, 99].uniq,
120
+ 'RO' => [86, 99].uniq,
121
+ 'BA' => [86].uniq,
122
+ 'SE' => [86].uniq,
123
+ 'PE' => [88, 84, 85, 86].uniq,
124
+ 'AL' => [88, 84, 85, 86].uniq,
125
+ 'PB' => [88, 84, 85, 86].uniq,
126
+ 'RN' => [88, 84, 85, 86].uniq,
127
+ 'CE' => [88, 84, 85, 86].uniq,
128
+ 'PI' => [88, 84, 85, 86].uniq,
129
+ 'PA' => [84].uniq,
130
+ 'AM' => [84].uniq,
131
+ 'RR' => [84].uniq,
132
+ 'AP' => [84].uniq,
133
+ 'MA' => [84].uniq
134
+ }
135
+
136
+ MOBILE_SPECIAL_CASES_FIRST_4_DIGITS = {
137
+ 'SP' => ((6340..6369).to_a + (7011..7051).to_a + (7950..7967).to_a + (6310..6339).to_a + (6589..6599).to_a + (7052..7062).to_a + (8800..8899).to_a + (6057..6060).to_a + (6193..6199).to_a + (6370..6499).to_a + [7099] + (6100..6193).to_a + [6299] + (6651..6799).to_a + [6999] + (7971..7999).to_a + (8814..8899).to_a + (7900..7949).to_a + (5700..5768).to_a + (5787..5799).to_a + (5475..5499).to_a + (5472..5474).to_a + (5769..5786).to_a).uniq,
138
+ 'RJ' => [],
139
+ 'ES' => [],
140
+ 'MG' => ((9960..9979).to_a + (9991..9999).to_a).uniq,
141
+ 'PR' => ((9941..9998).to_a).uniq,
142
+ 'SC' => [],
143
+ 'RS' => ((9911..9939).to_a).uniq,
144
+ 'DF' => ((9551..9559).to_a + (9981..9989).to_a).uniq,
145
+ 'GO' => ((9551..9559).to_a + (9981..9989).to_a).uniq,
146
+ 'TO' => ((9551..9559).to_a + (9981..9989).to_a).uniq,
147
+ 'MT' => ((9551..9559).to_a + (9981..9989).to_a).uniq,
148
+ 'MS' => ((9551..9559).to_a + (9981..9989).to_a).uniq,
149
+ 'AC' => ((9551..9559).to_a + (9981..9989).to_a).uniq,
150
+ 'RO' => ((9551..9559).to_a + (9981..9989).to_a).uniq,
151
+ 'BA' => [],
152
+ 'SE' => [],
153
+ 'PE' => ((8719..8721).to_a + (8100..82000).to_a).uniq,
154
+ 'AL' => ((8719..8721).to_a + (8100..82000).to_a).uniq,
155
+ 'PB' => ((8719..8721).to_a + (8100..82000).to_a).uniq,
156
+ 'RN' => ((8719..8721).to_a + (8100..82000).to_a).uniq,
157
+ 'CE' => ((8719..8721).to_a + (8100..82000).to_a).uniq,
158
+ 'PI' => ((8719..8721).to_a + (8100..82000).to_a).uniq,
159
+ 'PA' => [],
160
+ 'AM' => [],
161
+ 'RR' => [],
162
+ 'AP' => [],
163
+ 'MA' => []
164
+ }
165
+
166
+ MOBILE_SME_FIRST_2_DIGITS = {
167
+ 'SP' => [],
168
+ 'RJ' => [70, 77, 78].uniq,
169
+ 'ES' => [70, 77, 78].uniq,
170
+ 'MG' => [],
171
+ 'PR' => [],
172
+ 'SC' => [],
173
+ 'RS' => [],
174
+ 'DF' => [],
175
+ 'GO' => [],
176
+ 'TO' => [],
177
+ 'MT' => [],
178
+ 'MS' => [],
179
+ 'AC' => [],
180
+ 'RO' => [],
181
+ 'BA' => [],
182
+ 'SE' => [],
183
+ 'PE' => [],
184
+ 'AL' => [],
185
+ 'PB' => [],
186
+ 'RN' => [],
187
+ 'CE' => [],
188
+ 'PI' => [],
189
+ 'PA' => [],
190
+ 'AM' => [],
191
+ 'RR' => [],
192
+ 'AP' => [],
193
+ 'MA' => []
194
+ }
@@ -0,0 +1,108 @@
1
+ class TelBr
2
+ require 'rules'
3
+ attr_reader :telephone
4
+
5
+ def initialize(telephone)
6
+ @telephone = telephone.to_s
7
+ validate_telephone
8
+ end
9
+
10
+ def valid?
11
+ @error.empty? ? true : false
12
+ end
13
+
14
+ def error
15
+ @error
16
+ end
17
+
18
+ def ddd
19
+ stripped[0,2]
20
+ end
21
+
22
+ def state
23
+ DDDS[ddd][:state]
24
+ end
25
+
26
+ def region
27
+ DDDS[ddd][:region]
28
+ end
29
+
30
+ def number
31
+ stripped[2,9]
32
+ end
33
+
34
+ def stripped
35
+ @telephone.scan(/[0-9]/).join
36
+ end
37
+
38
+ def formatted
39
+ number.size == 8 ? "(#{ddd}) #{number[0,4]}-#{number[4,4]}" : "(#{ddd}) #{number[0,5]}-#{number[5,4]}"
40
+ end
41
+
42
+ def type
43
+ number_type
44
+ end
45
+
46
+ private
47
+
48
+ def validate_telephone
49
+ return @error = "Invalid telephone" unless valid_string?
50
+ return @error = "Invalid DDD" unless valid_ddd?
51
+ return @error = "Invalid number" unless valid_number_type?
52
+ return @error = "Number should have 8 digits" if should_have_eight_digits?
53
+ return @error = "Number should have 9 digits" if should_have_nine_digits?
54
+ return @error = ""
55
+ end
56
+
57
+ def valid_string?
58
+ [10, 11].include?(stripped.size)
59
+ end
60
+
61
+ def valid_ddd?
62
+ DDDS.keys.include?(ddd)
63
+ end
64
+
65
+ def valid_number_type?
66
+ number_type != 'invalid'
67
+ end
68
+
69
+ def should_have_eight_digits?
70
+ (!ddd_requires_nineth_digit? && number.size == 9) || (!is_mobile? && number.size == 9)
71
+ end
72
+
73
+ def should_have_nine_digits?
74
+ ddd_requires_nineth_digit? && is_mobile? && number.size == 8
75
+ end
76
+
77
+ def is_mobile?
78
+ type == 'mobile' ? true : false
79
+ end
80
+
81
+ def ddd_requires_nineth_digit?
82
+ DDDS[ddd][:ninth_digit] && is_mobile?
83
+ end
84
+
85
+ def number_type
86
+ return 'invalid' if number.nil? || (number.size == 9 && number[0].to_i != 9)
87
+ number.size == 9 ? eight_digits = number[1,8] : eight_digits = number
88
+ case
89
+ # SPECIAL CASES
90
+ when MOBILE_SME_FIRST_2_DIGITS[DDDS[ddd][:state]].include?(eight_digits[0,2].to_i)
91
+ return 'mobile sme'
92
+ when MOBILE_SPECIAL_CASES_FIRST_4_DIGITS[DDDS[ddd][:state]].include?(eight_digits[0,4].to_i)
93
+ return 'mobile'
94
+ when MOBILE_SPECIAL_CASES_FIRST_2_DIGITS[DDDS[ddd][:state]].include?(eight_digits[0,2].to_i)
95
+ return 'mobile'
96
+
97
+ # DEFAULT CASES
98
+ when eight_digits[0,2].to_i == 57 # 57 rural landline
99
+ return 'rural landline'
100
+ when (2..5).include?(eight_digits[0,1].to_i) # 2 to 5 landline
101
+ return 'landline'
102
+ when MOBILE_BANDS[DDDS[ddd][:state]].include?(eight_digits[0,2].to_i)
103
+ return 'mobile'
104
+ else
105
+ return 'invalid'
106
+ end
107
+ end
108
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: telefonia_br
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Gabriel Hilal
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: This gem does some tricks with Brazilian Telephone Numbers. It validates
28
+ telephone numbers based on its area code, bands of operation and the rules defined
29
+ by ANATEL.
30
+ email: gabrielhilal@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - lib/rules.rb
36
+ - lib/telefonia_br.rb
37
+ homepage: http://github.com/gabrielhilal/telefonia_br
38
+ licenses:
39
+ - MIT
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.2.2
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Brazilian Phone Number Validator
61
+ test_files: []