cep 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/cep.rb +74 -11
  3. metadata +29 -22
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eaac41eb5a1d32299a91c8ce321e99740d7330b2
4
+ data.tar.gz: e8cd1c802504a7d69746d4480f6294b3c0b0489d
5
+ SHA512:
6
+ metadata.gz: a673e48a49de2c85d9f979055ee1b6f5dc95f93a398251849e5549a551e51e37c491982cb2c90846008043827f632dced7d7f7e35d83c2559fd17f80bc579a38
7
+ data.tar.gz: a41bed911ef8a6d24085cf9d6a0b070bb021cbb680cea2e1844b0171fc9eae2c7126bd7a90476288a34e0ffbf9c0abcafe74009ff771552ba6ae98e4a28438e5
data/lib/cep.rb CHANGED
@@ -6,19 +6,18 @@ class Cep
6
6
 
7
7
  URL = 'http://m.correios.com.br/movel/buscaCepConfirma.do'
8
8
 
9
+ def initialize
10
+ @ceps = []
11
+ @_cep = {}
12
+ end
13
+
9
14
  def address(cep)
10
15
  response = query({cepEntrada: cep, metodo: :buscarCep})
11
16
  values = values(response)
17
+ end
12
18
 
13
- city_and_state = values[2].split('/').map{|v| v.strip}
14
-
15
- {
16
- logradouro: values[0],
17
- bairro: values[1],
18
- localidade: city_and_state[0],
19
- estado: city_and_state[1],
20
- cep: values[3]
21
- }
19
+ def cep(cep)
20
+ address(cep)[0]
22
21
  end
23
22
 
24
23
  def url(params = {})
@@ -33,10 +32,74 @@ class Cep
33
32
  end
34
33
 
35
34
  def values(response)
36
- Nokogiri::HTML(response.body).css('.respostadestaque').map do |el|
37
- el.content.gsub(/[\n\t]/,'').strip.gsub(/\s+/,' ')
35
+ Nokogiri::HTML(response.body).css('.respostadestaque').each do |el|
36
+ el.parent.children.children.each_slice(2).each do |k,v|
37
+ elements = clean_element(k, v)
38
+ handle_keys(elements)
39
+ end
40
+ end
41
+ @ceps.uniq
42
+ end
43
+
44
+ private
45
+
46
+ def clean_element(key, value)
47
+ key = resolve_text(key.text).gsub(/:/,'').downcase unless key.nil?
48
+ value = resolve_text(value.text) unless value.nil?
49
+ [key, value]
50
+ end
51
+
52
+ def resolve_text(text)
53
+ text.gsub(/[\n\t]/,'').gsub(/\s+/,' ').strip
54
+ end
55
+
56
+ def handle_keys(elements)
57
+ key = elements[0]
58
+ value = elements[1]
59
+ unless key == 'opções' and value == nil
60
+ add_cep_in_ceps(key)
61
+ resolve_adrress(key, value)
62
+ resolve_locality(key, value)
63
+ resolve_others(key, value)
64
+ end
65
+ end
66
+
67
+ def add_cep_in_ceps(key)
68
+ if key_name_present?(key, ['logradouro','endereço'])
69
+ @ceps << @_cep unless @_cep.empty?
70
+ @_cep = {}
38
71
  end
72
+ @ceps
39
73
  end
40
74
 
75
+ def resolve_adrress(key, value)
76
+ if key_name_present?(key, ['logradouro','endereço'])
77
+ @_cep[:tipo_logradouro] = value.split(' ').map{|v| v.strip}[0]
78
+ @_cep[:logradouro] = value
79
+ end
80
+ @_cep
81
+ end
82
+
83
+ def resolve_locality(key, value)
84
+ if key_name_present?(key, ['localidade / uf'])
85
+ @_cep[locality(key,0).to_sym] = locality(value,0)
86
+ @_cep[locality(key,1).to_sym] = locality(value,1)
87
+ end
88
+ @_cep
89
+ end
41
90
 
91
+ def resolve_others(key, value)
92
+ unless key_name_present?(key, ['logradouro','endereço','localidade / uf'])
93
+ @_cep[key.to_sym] = value
94
+ end
95
+ @_cep
96
+ end
97
+
98
+ def locality(el, pos)
99
+ el.split('/').map{|v| v.strip}[pos]
100
+ end
101
+
102
+ def key_name_present?(key, names=[])
103
+ names.include?(key)
104
+ end
42
105
  end
metadata CHANGED
@@ -1,11 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cep
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
5
- prerelease:
4
+ version: 0.0.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - Marcelo Jacobus
8
+ - Fernando Pereira
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
@@ -14,52 +14,60 @@ dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
16
16
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
17
  requirements:
19
- - - ! '>='
18
+ - - '>='
20
19
  - !ruby/object:Gem::Version
21
20
  version: '0'
22
21
  type: :runtime
23
22
  prerelease: false
24
23
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
24
  requirements:
27
- - - ! '>='
25
+ - - '>='
28
26
  - !ruby/object:Gem::Version
29
27
  version: '0'
30
28
  - !ruby/object:Gem::Dependency
31
29
  name: addressable
32
30
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
31
  requirements:
35
- - - ! '>='
32
+ - - '>='
36
33
  - !ruby/object:Gem::Version
37
34
  version: '0'
38
35
  type: :runtime
39
36
  prerelease: false
40
37
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
38
  requirements:
43
- - - ! '>='
39
+ - - '>='
44
40
  - !ruby/object:Gem::Version
45
41
  version: '0'
46
42
  - !ruby/object:Gem::Dependency
47
43
  name: rspec
48
44
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
45
  requirements:
51
- - - ! '>='
46
+ - - '>='
52
47
  - !ruby/object:Gem::Version
53
48
  version: '0'
54
49
  type: :development
55
50
  prerelease: false
56
51
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
52
  requirements:
59
- - - ! '>='
53
+ - - '>='
60
54
  - !ruby/object:Gem::Version
61
55
  version: '0'
62
- description: API for getting address of a given CEP
56
+ - !ruby/object:Gem::Dependency
57
+ name: simplecov
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ description: API for getting address of a given CEP or ADDRESS
63
71
  email: marcelo.jacobus@gmail.com
64
72
  executables: []
65
73
  extensions: []
@@ -68,26 +76,25 @@ files:
68
76
  - lib/cep.rb
69
77
  homepage: https://github.com/mjacobus/cep.git
70
78
  licenses: []
79
+ metadata: {}
71
80
  post_install_message:
72
81
  rdoc_options: []
73
82
  require_paths:
74
83
  - lib
75
84
  required_ruby_version: !ruby/object:Gem::Requirement
76
- none: false
77
85
  requirements:
78
- - - ! '>='
86
+ - - '>='
79
87
  - !ruby/object:Gem::Version
80
88
  version: '0'
81
89
  required_rubygems_version: !ruby/object:Gem::Requirement
82
- none: false
83
90
  requirements:
84
- - - ! '>='
91
+ - - '>='
85
92
  - !ruby/object:Gem::Version
86
93
  version: '0'
87
94
  requirements: []
88
95
  rubyforge_project:
89
- rubygems_version: 1.8.24
96
+ rubygems_version: 2.0.6
90
97
  signing_key:
91
- specification_version: 3
92
- summary: API for getting address of a given CEP
98
+ specification_version: 4
99
+ summary: API for getting address of a given CEP or ADDRESS
93
100
  test_files: []