cep 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cep.rb +42 -0
- metadata +93 -0
data/lib/cep.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'nokogiri'
|
3
|
+
require 'addressable/uri'
|
4
|
+
|
5
|
+
class Cep
|
6
|
+
|
7
|
+
URL = 'http://m.correios.com.br/movel/buscaCepConfirma.do'
|
8
|
+
|
9
|
+
def address(cep)
|
10
|
+
response = query({cepEntrada: cep, metodo: :buscarCep})
|
11
|
+
values = values(response)
|
12
|
+
|
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
|
+
}
|
22
|
+
end
|
23
|
+
|
24
|
+
def url(params = {})
|
25
|
+
uri = Addressable::URI.new
|
26
|
+
uri.query_values = params
|
27
|
+
url = "#{Cep::URL}?#{uri.query}"
|
28
|
+
end
|
29
|
+
|
30
|
+
def query(params = {})
|
31
|
+
uri = URI.parse(url(params))
|
32
|
+
Net::HTTP.get_response(uri)
|
33
|
+
end
|
34
|
+
|
35
|
+
def values(response)
|
36
|
+
Nokogiri::HTML(response.body).css('.respostadestaque').map do |el|
|
37
|
+
el.content.gsub(/[\n\t]/,'').strip.gsub(/\s+/,' ')
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cep
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Marcelo Jacobus
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-04 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: nokogiri
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: addressable
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: API for getting address of a given CEP
|
63
|
+
email: marcelo.jacobus@gmail.com
|
64
|
+
executables: []
|
65
|
+
extensions: []
|
66
|
+
extra_rdoc_files: []
|
67
|
+
files:
|
68
|
+
- lib/cep.rb
|
69
|
+
homepage: https://github.com/mjacobus/cep.git
|
70
|
+
licenses: []
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
requirements: []
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 1.8.24
|
90
|
+
signing_key:
|
91
|
+
specification_version: 3
|
92
|
+
summary: API for getting address of a given CEP
|
93
|
+
test_files: []
|