cep-aberto 0.1.2
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.
- checksums.yaml +7 -0
- data/.gitignore +50 -0
- data/LICENSE +21 -0
- data/README.md +64 -0
- data/cep-aberto.gemspec +13 -0
- data/lib/cep-aberto.rb +1 -0
- data/lib/cep_aberto/cep.rb +68 -0
- metadata +51 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f9d4140860329970af79ea89a40fe79302440aeb
|
4
|
+
data.tar.gz: 8e33fbe4e0815c4968027870f1ab3743e988ff38
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 806f68fb463b30f3ff3dfae8a10cbc02e2a9b82ec0d5d48b7e6531994fbf782e63f91347b06f1c56071cbd4e24892c6b34b729d6c64870e343b0e2af1da6da49
|
7
|
+
data.tar.gz: 4b0243428f6aa846dedd999c328d18a9338b7fd09ee81c61fc1a60f526ccbce09369ce0eb6f826a733d37f23e99740149edcaa4540efa5c0af492b7b2617d194
|
data/.gitignore
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
# Used by dotenv library to load environment variables.
|
14
|
+
# .env
|
15
|
+
|
16
|
+
## Specific to RubyMotion:
|
17
|
+
.dat*
|
18
|
+
.repl_history
|
19
|
+
build/
|
20
|
+
*.bridgesupport
|
21
|
+
build-iPhoneOS/
|
22
|
+
build-iPhoneSimulator/
|
23
|
+
|
24
|
+
## Specific to RubyMotion (use of CocoaPods):
|
25
|
+
#
|
26
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
27
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
28
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
29
|
+
#
|
30
|
+
# vendor/Pods/
|
31
|
+
|
32
|
+
## Documentation cache and generated files:
|
33
|
+
/.yardoc/
|
34
|
+
/_yardoc/
|
35
|
+
/doc/
|
36
|
+
/rdoc/
|
37
|
+
|
38
|
+
## Environment normalization:
|
39
|
+
/.bundle/
|
40
|
+
/vendor/bundle
|
41
|
+
/lib/bundler/man/
|
42
|
+
|
43
|
+
# for a library or gem, you might want to ignore these files since the code is
|
44
|
+
# intended to run in multiple environments; otherwise, check them in:
|
45
|
+
# Gemfile.lock
|
46
|
+
# .ruby-version
|
47
|
+
# .ruby-gemset
|
48
|
+
|
49
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
50
|
+
.rvmrc
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017 Jefferson dos Santos Felix
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# cep-aberto
|
2
|
+
Ruby Connector for the CEP ABERTO API (www.cepaberto.com)
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
Include cep-aberto in Gemfile:
|
6
|
+
```ruby
|
7
|
+
gem 'cep-aberto'
|
8
|
+
```
|
9
|
+
And bundle with:
|
10
|
+
```bash
|
11
|
+
bundle
|
12
|
+
```
|
13
|
+
Or execute manually:
|
14
|
+
```bash
|
15
|
+
gem install cep-aberto
|
16
|
+
```
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
First, you need a Token Authorization to use this gem. Go to [CEP Aberto site](http://www.cepaberto.com/) and sign up for free.
|
20
|
+
|
21
|
+
### Find address
|
22
|
+
Run:
|
23
|
+
```ruby
|
24
|
+
require "cep-aberto"
|
25
|
+
CepAberto::Cep::find("01001000", "1234567890abcdef01234567890abcde")
|
26
|
+
```
|
27
|
+
|
28
|
+
This method return a hash with the full address for the zipcode:
|
29
|
+
```ruby
|
30
|
+
=> {"altitude"=>760.0,
|
31
|
+
"bairro"=>"Sé",
|
32
|
+
"cep"=>"01001000",
|
33
|
+
"latitude"=>"-23.5479099981",
|
34
|
+
"longitude"=>"-46.636",
|
35
|
+
"logradouro"=>"Praça da Sé",
|
36
|
+
"cidade"=>"São Paulo",
|
37
|
+
"ddd"=>11,
|
38
|
+
"ibge"=>"3550308",
|
39
|
+
"estado"=>"SP"}
|
40
|
+
```
|
41
|
+
|
42
|
+
### List cities
|
43
|
+
To list cities from a given state, do this:
|
44
|
+
```ruby
|
45
|
+
require "cep-aberto"
|
46
|
+
CepAberto::Cep::cities("SP", "1234567890abcdef01234567890abcde")
|
47
|
+
```
|
48
|
+
And the result is:
|
49
|
+
```ruby
|
50
|
+
=> ["Adamantina",
|
51
|
+
"Adolfo",
|
52
|
+
"Agisse (Rancharia)",
|
53
|
+
"Aguaí",
|
54
|
+
"Águas da Prata",
|
55
|
+
"Águas de Lindóia",
|
56
|
+
"Águas de Santa Bárbara",
|
57
|
+
"Águas de São Pedro",
|
58
|
+
"Água Vermelha (São Carlos)",
|
59
|
+
...
|
60
|
+
```
|
61
|
+
|
62
|
+
## Thanks
|
63
|
+
|
64
|
+
Thanks to [CEP Aberto](http://www.cepaberto.com)
|
data/cep-aberto.gemspec
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'cep-aberto'
|
3
|
+
s.version = '0.1.2'
|
4
|
+
s.date = '2017-08-08'
|
5
|
+
s.summary = "Ruby Connector for the CEP ABERTO API (www.cepaberto.com). A token is required to use this gem."
|
6
|
+
s.description = s.summary
|
7
|
+
s.authors = ["Jefferson Felix"]
|
8
|
+
s.email = 'jsfelix@gmail.com'
|
9
|
+
s.files = `git ls-files`.split("\n")
|
10
|
+
s.homepage =
|
11
|
+
'https://github.com/jsfelix/cep-aberto'
|
12
|
+
s.license = 'MIT'
|
13
|
+
end
|
data/lib/cep-aberto.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "cep_aberto/cep.rb"
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# Main module CepAberto.
|
2
|
+
# Author:: Jefferson Felix
|
3
|
+
module CepAberto
|
4
|
+
# This class holds the methods to find zipcodes and address using the CEP
|
5
|
+
# Aberto API.
|
6
|
+
class Cep
|
7
|
+
|
8
|
+
require "net/http"
|
9
|
+
require "uri"
|
10
|
+
require "json"
|
11
|
+
|
12
|
+
# CEP Aberto API (v2)
|
13
|
+
@domain = "http://www.cepaberto.com/api/v2/"
|
14
|
+
|
15
|
+
# Find an address from a zipcode using the CEP Aberto API.
|
16
|
+
# Params:
|
17
|
+
# +zipcode+:: the zipcode to be searched by CEP Aberto API
|
18
|
+
# +token+:: the user authorization token
|
19
|
+
# Returns a hash with the full address
|
20
|
+
def Cep::find(cep, token)
|
21
|
+
validate_cep(cep)
|
22
|
+
uri = URI("#{@domain}ceps.json")
|
23
|
+
params = {'cep' => cep}
|
24
|
+
address = request_cep(uri, params, token)
|
25
|
+
if(address["logradouro"].is_a?String)
|
26
|
+
address["logradouro"] = address["logradouro"].split(",")[0]
|
27
|
+
end
|
28
|
+
return address
|
29
|
+
end
|
30
|
+
|
31
|
+
# Returns a list of cities from a given state.
|
32
|
+
# Params:
|
33
|
+
# +state+:: State acronym (e.g. SP)
|
34
|
+
# +token+:: the user authorization token
|
35
|
+
# Returns an array with all cities from the state
|
36
|
+
def Cep::cities(state, token)
|
37
|
+
uri = URI("#{@domain}cities.json")
|
38
|
+
params = {'estado' => state}
|
39
|
+
all_cities = request_cep(uri, params, token)
|
40
|
+
cities_arr = Array.new
|
41
|
+
all_cities.each do |c|
|
42
|
+
cities_arr.push(c["nome"])
|
43
|
+
end
|
44
|
+
return cities_arr
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
# Validate cep entry
|
50
|
+
# * Must be a string
|
51
|
+
# * Must be 8 characters
|
52
|
+
def Cep::validate_cep(str)
|
53
|
+
raise ArgumentError, 'Argument is not string with 8 characters' unless str.is_a? String and str.length == 8
|
54
|
+
end
|
55
|
+
|
56
|
+
# Request API
|
57
|
+
def Cep::request_cep(uri, params, token)
|
58
|
+
headers = { "Authorization" => "Token token=#{token}" }
|
59
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
60
|
+
request = Net::HTTP::Get.new(uri.path)
|
61
|
+
request.set_form_data(params)
|
62
|
+
request = Net::HTTP::Get.new(uri.path + '?' + request.body, headers)
|
63
|
+
response = http.request(request).body
|
64
|
+
JSON.parse(response)
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
end
|
metadata
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cep-aberto
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jefferson Felix
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-08-08 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Ruby Connector for the CEP ABERTO API (www.cepaberto.com). A token is
|
14
|
+
required to use this gem.
|
15
|
+
email: jsfelix@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- LICENSE
|
22
|
+
- README.md
|
23
|
+
- cep-aberto.gemspec
|
24
|
+
- lib/cep-aberto.rb
|
25
|
+
- lib/cep_aberto/cep.rb
|
26
|
+
homepage: https://github.com/jsfelix/cep-aberto
|
27
|
+
licenses:
|
28
|
+
- MIT
|
29
|
+
metadata: {}
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 2.6.11
|
47
|
+
signing_key:
|
48
|
+
specification_version: 4
|
49
|
+
summary: Ruby Connector for the CEP ABERTO API (www.cepaberto.com). A token is required
|
50
|
+
to use this gem.
|
51
|
+
test_files: []
|