cep 0.0.2 → 0.0.4

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 +4 -4
  2. data/lib/cep.rb +6 -81
  3. metadata +47 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eaac41eb5a1d32299a91c8ce321e99740d7330b2
4
- data.tar.gz: e8cd1c802504a7d69746d4480f6294b3c0b0489d
3
+ metadata.gz: e7f73612ee1110ee8ace40b70cf0b21737f121db
4
+ data.tar.gz: b939df23cb6d4a99a7fd8b2f8d688b1b23843dd7
5
5
  SHA512:
6
- metadata.gz: a673e48a49de2c85d9f979055ee1b6f5dc95f93a398251849e5549a551e51e37c491982cb2c90846008043827f632dced7d7f7e35d83c2559fd17f80bc579a38
7
- data.tar.gz: a41bed911ef8a6d24085cf9d6a0b070bb021cbb680cea2e1844b0171fc9eae2c7126bd7a90476288a34e0ffbf9c0abcafe74009ff771552ba6ae98e4a28438e5
6
+ metadata.gz: 57d6debd7b057ae6ba6ce6b57e8714d72ed4b3980c5a75f36a0ac6b6cfa833ed1f42aca58b111061f193780b7e51cf65e67fdaed152c9472fb385a3917a14bda
7
+ data.tar.gz: 6fd94ef37f79990613b4d82f6cc35ed8646f1227e36cae2878b786efc6a1f9feefdb5884f521da3b9678cdd6d6489f051939c836ec62792b68f23c498db86ebe
data/lib/cep.rb CHANGED
@@ -1,23 +1,20 @@
1
1
  require 'net/http'
2
2
  require 'nokogiri'
3
3
  require 'addressable/uri'
4
+ require 'correios/address_search_response'
4
5
 
5
6
  class Cep
6
7
 
7
8
  URL = 'http://m.correios.com.br/movel/buscaCepConfirma.do'
8
9
 
9
- def initialize
10
- @ceps = []
11
- @_cep = {}
12
- end
13
-
14
- def address(cep)
15
- response = query({cepEntrada: cep, metodo: :buscarCep})
16
- values = values(response)
10
+ def search(term)
11
+ response = query({cepEntrada: term, metodo: :buscarCep})
12
+ Correios::AddressSearchResponse.new(response.body).addresses
17
13
  end
14
+ alias_method :address, :search
18
15
 
19
16
  def cep(cep)
20
- address(cep)[0]
17
+ search(cep)[0]
21
18
  end
22
19
 
23
20
  def url(params = {})
@@ -30,76 +27,4 @@ class Cep
30
27
  uri = URI.parse(url(params))
31
28
  Net::HTTP.get_response(uri)
32
29
  end
33
-
34
- def values(response)
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 = {}
71
- end
72
- @ceps
73
- end
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
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
105
30
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cep
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcelo Jacobus
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-04 00:00:00.000000000 Z
12
+ date: 2013-09-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
@@ -39,6 +39,34 @@ dependencies:
39
39
  - - '>='
40
40
  - !ruby/object:Gem::Version
41
41
  version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: active_support
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: i18n
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
42
70
  - !ruby/object:Gem::Dependency
43
71
  name: rspec
44
72
  requirement: !ruby/object:Gem::Requirement
@@ -67,8 +95,24 @@ dependencies:
67
95
  - - '>='
68
96
  - !ruby/object:Gem::Version
69
97
  version: '0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: guard-rspec
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
70
112
  description: API for getting address of a given CEP or ADDRESS
71
- email: marcelo.jacobus@gmail.com
113
+ email:
114
+ - marcelo.jacobus@gmail.com
115
+ - ''
72
116
  executables: []
73
117
  extensions: []
74
118
  extra_rdoc_files: []