br_zip_code 0.2.3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +11 -17
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +3 -12
- data/LICENSE.txt +21 -0
- data/README.md +71 -0
- data/app/controllers/br_zip_code/zip_code_controller.rb +11 -17
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/br_zip_code.gemspec +19 -11
- data/config/locales/pt-BR.yml +4 -0
- data/lib/br_zip_code.rb +76 -60
- data/lib/br_zip_code/config.rb +5 -0
- data/lib/br_zip_code/engine.rb +3 -0
- data/lib/br_zip_code/service.rb +31 -0
- data/lib/br_zip_code/service/cep_livre.rb +66 -0
- data/lib/br_zip_code/service/correio_control.rb +28 -0
- data/lib/br_zip_code/service/postmon.rb +28 -0
- data/lib/br_zip_code/service/republica_virtual.rb +28 -0
- data/lib/br_zip_code/service/viacep.rb +29 -0
- data/lib/br_zip_code/version.rb +1 -1
- metadata +58 -18
- data/Gemfile.lock +0 -24
- data/MIT-LICENSE +0 -22
- data/README.mkdn +0 -54
- data/app/controllers/br_zip_code/application_controller.rb +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6acf525e6f3c4cad73bfd27a4058c915bef0b37d
|
4
|
+
data.tar.gz: b54669ed58ade983282c7cbe216b01f5e70b0237
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dbd95a86dcb87cfae490f3d95f1fac0cd7ee7c7df6113c9895e63dc90fed42d4720c1eae7f09c464f80ff5df6b240aa120588c0863c63ca40e17e57d30773310
|
7
|
+
data.tar.gz: 3694bcfa553ff959321b700beb27843d09b33a43185ab9e307a5cd3588e886d5e9563d32611f1ab8a2eaa8a2507eb72dfc5dea72511417ce97312a3d35b7f855
|
data/.gitignore
CHANGED
@@ -1,17 +1,11 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
.
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
pkg
|
13
|
-
rdoc
|
14
|
-
spec/reports
|
15
|
-
test/tmp
|
16
|
-
test/version_tmp
|
17
|
-
tmp
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/Gemfile.lock
|
4
|
+
/_yardoc/
|
5
|
+
/coverage/
|
6
|
+
/doc/
|
7
|
+
/pkg/
|
8
|
+
/spec/reports/
|
9
|
+
/tmp/
|
10
|
+
|
11
|
+
/.env
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
CHANGED
@@ -1,16 +1,7 @@
|
|
1
|
-
source
|
1
|
+
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
#
|
4
|
-
# Bundler will treat runtime dependencies like base dependencies, and
|
5
|
-
# development dependencies will be added by default to the :development group.
|
3
|
+
# Specify your gem's dependencies in br_zip_code.gemspec
|
6
4
|
gemspec
|
7
5
|
|
8
|
-
# Declare any dependencies that are still in development here instead of in
|
9
|
-
# your gemspec. These might include edge Rails or gems from your path or
|
10
|
-
# Git. Remember to move these dependencies to your gemspec before releasing
|
11
|
-
# your gem to rubygems.org.
|
12
|
-
|
13
|
-
# To use debugger
|
14
|
-
# gem 'debugger'
|
15
|
-
|
16
6
|
gem 'httparty'
|
7
|
+
gem 'nokogiri'
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Ariel Schvartz
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# BrZipCode
|
2
|
+
|
3
|
+
This gem helps you to find an address based on a brazilian zip code. For that, 3 webservices are used:
|
4
|
+
|
5
|
+
* [Postmon](http://postmon.com.br/ "Postmon")
|
6
|
+
* [Via CEP](http://viacep.com.br/ "Via CEP")
|
7
|
+
* [Correio Control](http://cep.correiocontrol.com.br "Correio Control")
|
8
|
+
* [República Virtual](http://republicavirtual.com.br/cep/ "República Virtual")
|
9
|
+
* [CEP Livre](http://ceplivre.com.br/ "CEP Livre")
|
10
|
+
|
11
|
+
The webservices are searched in order. If one is not available, the next is tested until one is available or none.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'br_zip_code'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install br_zip_code
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
BrZipCode.get("22451090") #=> {:street=>"Rua Duque Estrada", :state=>"RJ", :city=>"Rio de Janeiro", :district=>"Gávea"}
|
33
|
+
BrZipCode.get("22.451-090") #=> {:street=>"Rua Duque Estrada", :state=>"RJ", :city=>"Rio de Janeiro", :district=>"Gávea"}
|
34
|
+
BrZipCode.get("22451-090") #=> {:street=>"Rua Duque Estrada", :state=>"RJ", :city=>"Rio de Janeiro", :district=>"Gávea"}
|
35
|
+
|
36
|
+
BrZipCode.get("12345678") #=> nil
|
37
|
+
BrZipCode.get("1234567") #=> nil
|
38
|
+
```
|
39
|
+
|
40
|
+
### Basic JSON API
|
41
|
+
The route /zip_code/:zip_code.json (/zip_code/22441090.json, for example) is accessible.
|
42
|
+
Pass the zip_code and get the response, so you can create AJAX function to autocomplete addresses.
|
43
|
+
|
44
|
+
So, http://localhost:3000/zip_code/22441090.json will return:
|
45
|
+
```json
|
46
|
+
{
|
47
|
+
"success":true,
|
48
|
+
"zip_code_info":
|
49
|
+
{
|
50
|
+
"street_type":"Rua",
|
51
|
+
"street":"Rua General Venâncio Flores",
|
52
|
+
"state":"RJ",
|
53
|
+
"city":"Rio de Janeiro",
|
54
|
+
"neighborhood":"Leblon"
|
55
|
+
}
|
56
|
+
}
|
57
|
+
```
|
58
|
+
|
59
|
+
## Development
|
60
|
+
|
61
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
62
|
+
|
63
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
64
|
+
|
65
|
+
## Contributing
|
66
|
+
|
67
|
+
1. Fork it ( https://github.com/[my-github-username]/br_zip_code/fork )
|
68
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
69
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
70
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
71
|
+
5. Create a new Pull Request
|
@@ -1,25 +1,19 @@
|
|
1
|
-
require_dependency "new_relic_ping/application_controller"
|
2
|
-
|
3
1
|
module BrZipCode
|
4
|
-
class ZipCodeController < ApplicationController
|
2
|
+
class ZipCodeController < ApplicationController::Base
|
5
3
|
def show
|
6
|
-
|
7
|
-
|
8
|
-
zip_info = BrZipCode.get_address zip_code
|
4
|
+
begin
|
5
|
+
response = BrZipCode.get(params[:zip_code] || params[:id])
|
9
6
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
end
|
15
|
-
end
|
16
|
-
else
|
17
|
-
respond_to do |format|
|
18
|
-
format.json do
|
19
|
-
render json: { success: true, zip_info: zip_info }
|
20
|
-
end
|
7
|
+
if response.nil?
|
8
|
+
render json: { success: false, error: 'not-found', message: I18n.t('br_zip_code.not_found') }
|
9
|
+
else
|
10
|
+
render json: { success: true, zip_code_info: response }
|
21
11
|
end
|
12
|
+
rescue Net::OpenTimeout
|
13
|
+
render json: { success: false, error: 'not-found', message: I18n.t('br_zip_code.timeout') }
|
22
14
|
end
|
15
|
+
|
23
16
|
end
|
17
|
+
|
24
18
|
end
|
25
19
|
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "br_zip_code"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
require "pry"
|
11
|
+
Pry.start
|
12
|
+
|
13
|
+
# require "irb"
|
14
|
+
# IRB.start
|
data/bin/setup
ADDED
data/br_zip_code.gemspec
CHANGED
@@ -4,21 +4,29 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'br_zip_code/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name
|
8
|
-
spec.version
|
7
|
+
spec.name = "br_zip_code"
|
8
|
+
spec.version = BrZipCode::VERSION
|
9
9
|
spec.authors = ["Ariel Schvartz"]
|
10
10
|
spec.email = ["ari.shh@gmail.com"]
|
11
|
-
|
12
|
-
spec.
|
13
|
-
|
11
|
+
|
12
|
+
if spec.respond_to?(:metadata)
|
13
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
14
|
+
end
|
15
|
+
|
16
|
+
spec.summary = "BR Zip Code - #{spec.version}"
|
17
|
+
spec.description = "Gem that lets user find a brazilian address with the given zip_code."
|
18
|
+
spec.homepage = "https://github.com/arielschvartz/br_zip_code"
|
14
19
|
spec.license = "MIT"
|
15
20
|
|
16
|
-
spec.files = `git ls-files -z`.split("\x0")
|
17
|
-
spec.
|
18
|
-
spec.
|
21
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
24
|
spec.require_paths = ["lib"]
|
20
25
|
|
21
|
-
spec.
|
22
|
-
|
23
|
-
spec.
|
26
|
+
spec.add_dependency "rails"
|
27
|
+
|
28
|
+
spec.add_development_dependency "bundler", "~> 1.8"
|
29
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
30
|
+
spec.add_development_dependency "dotenv", "~> 2.0"
|
31
|
+
spec.add_development_dependency "pry"
|
24
32
|
end
|
data/lib/br_zip_code.rb
CHANGED
@@ -1,82 +1,98 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "br_zip_code/engine"
|
2
|
+
require "br_zip_code/version"
|
3
|
+
|
3
4
|
require 'httparty'
|
5
|
+
require 'dotenv'
|
6
|
+
Dotenv.load
|
7
|
+
|
8
|
+
require "br_zip_code/config"
|
9
|
+
require "br_zip_code/service"
|
10
|
+
require "br_zip_code/service/correio_control"
|
11
|
+
require "br_zip_code/service/postmon"
|
12
|
+
require "br_zip_code/service/viacep"
|
13
|
+
require "br_zip_code/service/republica_virtual"
|
14
|
+
require "br_zip_code/service/cep_livre"
|
4
15
|
|
5
16
|
module BrZipCode
|
17
|
+
|
18
|
+
class << self
|
19
|
+
attr_accessor :config
|
20
|
+
attr_accessor :services
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.configuration
|
24
|
+
self.config ||= BrZipCode::Config.new
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.configure &block
|
28
|
+
yield configuration
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.timeout
|
32
|
+
self.configuration.timeout || 3
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.attempts
|
36
|
+
self.configuration.attempts || 5
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.timeout= value
|
40
|
+
self.configuration.timeout = value
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.attempts= value
|
44
|
+
self.configuration.attempts = value
|
45
|
+
end
|
46
|
+
|
6
47
|
def self.strip zip_code
|
7
|
-
|
8
|
-
# if zip.scan(/[0-9]/).length != 8 or zip.length != 8
|
9
|
-
unless zip.to_s.match(/^\d{8}$/)
|
10
|
-
nil
|
11
|
-
else
|
12
|
-
zip
|
13
|
-
end
|
48
|
+
zip_code.to_s.gsub(/[^0-9]/, '')
|
14
49
|
end
|
15
50
|
|
16
51
|
def self.formatted zip_code
|
17
|
-
|
18
|
-
|
19
|
-
|
52
|
+
numbers = self.strip zip_code
|
53
|
+
|
54
|
+
if numbers.length == 8
|
55
|
+
numbers.insert(2, '.').insert(6, '-')
|
20
56
|
else
|
21
|
-
|
57
|
+
nil
|
22
58
|
end
|
23
59
|
end
|
24
60
|
|
25
|
-
def self.
|
26
|
-
|
61
|
+
def self.services
|
62
|
+
return @services unless @services.nil?
|
27
63
|
|
28
|
-
|
64
|
+
@services = []
|
29
65
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
attempt.zip_code_attempt_3 zip
|
39
|
-
rescue Exception => e
|
40
|
-
{ error: "CEP não encontrado" }
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
else
|
45
|
-
{ error: "CEP inválido" }
|
66
|
+
[
|
67
|
+
'Postmon',
|
68
|
+
'Viacep',
|
69
|
+
'CorreioControl',
|
70
|
+
'RepublicaVirtual',
|
71
|
+
'CepLivre'
|
72
|
+
].each do |class_name|
|
73
|
+
@services.push Module.const_get("BrZipCode::Service::#{class_name}")
|
46
74
|
end
|
47
75
|
|
76
|
+
@services
|
48
77
|
end
|
49
78
|
|
50
|
-
|
51
|
-
|
52
|
-
r = HTTParty.get("http://cep.republicavirtual.com.br/web_cep.php?cep=#{zip_code}&formato=json")
|
53
|
-
hash = { street_type: r["tipo_logradouro"], street: r["tipo_logradouro"] + " " + r["logradouro"], state: r["uf"], city: r["cidade"], neighborhood: r["bairro"] }
|
54
|
-
if hash[:street].blank?
|
55
|
-
raise Exception
|
56
|
-
else
|
57
|
-
hash
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
def zip_code_attempt_2 zip_code
|
62
|
-
r = HTTParty.get("http://cep.correiocontrol.com.br/#{zip_code}.json")
|
63
|
-
hash = { street_type: r["logradouro"].split(' ').first, street: r["logradouro"], state: r["uf"], city: r["localidade"], neighborhood: r["bairro"] }
|
64
|
-
if hash[:street].blank?
|
65
|
-
raise Exception
|
66
|
-
else
|
67
|
-
hash
|
68
|
-
end
|
69
|
-
end
|
79
|
+
def self.get zip_code
|
80
|
+
counter = 0
|
70
81
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
82
|
+
self.services.each do |s|
|
83
|
+
counter += 1
|
84
|
+
begin
|
85
|
+
response = s.new(zip_code)
|
86
|
+
if response.valid?
|
87
|
+
return response.to_hash
|
88
|
+
end
|
89
|
+
rescue Net::OpenTimeout
|
90
|
+
if counter == attempts
|
91
|
+
raise Net::OpenTimeout
|
92
|
+
else
|
93
|
+
next
|
94
|
+
end
|
78
95
|
end
|
79
96
|
end
|
80
97
|
end
|
81
|
-
|
82
98
|
end
|
data/lib/br_zip_code/engine.rb
CHANGED
@@ -0,0 +1,31 @@
|
|
1
|
+
module BrZipCode
|
2
|
+
class Service
|
3
|
+
attr_accessor :zip_code, :response, :response_body
|
4
|
+
|
5
|
+
def initialize zip_code, no_timeout = false
|
6
|
+
@zip_code = BrZipCode.strip(zip_code)
|
7
|
+
|
8
|
+
return unless self.basic_valid?
|
9
|
+
|
10
|
+
if no_timeout
|
11
|
+
@response = HTTParty.get(final_url)
|
12
|
+
else
|
13
|
+
@response = HTTParty.get(final_url, timeout: BrZipCode.timeout)
|
14
|
+
end
|
15
|
+
|
16
|
+
@response_body = @response.response.body
|
17
|
+
|
18
|
+
if self.valid?
|
19
|
+
@response_body = JSON.parse(@response_body)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def basic_valid?
|
24
|
+
@zip_code.length == 8
|
25
|
+
end
|
26
|
+
|
27
|
+
def valid?
|
28
|
+
not self.response.nil? and not self.response_body.nil? and not zip_code.nil?
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
|
3
|
+
module BrZipCode
|
4
|
+
class Service
|
5
|
+
class CepLivre < BrZipCode::Service
|
6
|
+
|
7
|
+
def initialize zip_code, no_timeout = false
|
8
|
+
raise 'Environment Variable CEP_LIVRE_TOKEN not set.' if ENV['CEP_LIVRE_TOKEN'].nil?
|
9
|
+
|
10
|
+
@zip_code = zip_code.to_s.gsub(/[^0-9]/, '')
|
11
|
+
|
12
|
+
return unless self.basic_valid?
|
13
|
+
|
14
|
+
@zip_code = @zip_code[0..4] + '-' + @zip_code[5..-1]
|
15
|
+
|
16
|
+
if no_timeout
|
17
|
+
@response = HTTParty.get(final_url)
|
18
|
+
else
|
19
|
+
@response = HTTParty.get(final_url, timeout: BrZipCode.timeout)
|
20
|
+
end
|
21
|
+
|
22
|
+
@response_body = @response.response.body
|
23
|
+
|
24
|
+
if self.valid?
|
25
|
+
xml = Nokogiri.XML(@response_body)
|
26
|
+
children = xml.xpath("//cep").children
|
27
|
+
|
28
|
+
names = children.map { |x| x.name }
|
29
|
+
values = children.map { |x| x.children }.flatten.map { |x| x.text }
|
30
|
+
|
31
|
+
h = Hash[names.zip values]
|
32
|
+
@response_body = {
|
33
|
+
'ceplivre' => {
|
34
|
+
'cep' => h
|
35
|
+
}
|
36
|
+
}
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def to_hash
|
41
|
+
return nil unless self.valid? and not self.response_body.nil?
|
42
|
+
|
43
|
+
cep_body = response_body['ceplivre']['cep']
|
44
|
+
|
45
|
+
{
|
46
|
+
street: [cep_body['tp_logradouro'],cep_body['logradouro']].join(' '),
|
47
|
+
district: cep_body['bairro'],
|
48
|
+
city: cep_body['cidade'],
|
49
|
+
state: cep_body['uf_sigla']
|
50
|
+
}
|
51
|
+
end
|
52
|
+
|
53
|
+
def valid?
|
54
|
+
super and self.response.code == 200 and not self.response_body == '' and not self.response_body['ceplivre'].nil?
|
55
|
+
end
|
56
|
+
|
57
|
+
def final_url
|
58
|
+
"#{base_url}#{self.zip_code}/xml"
|
59
|
+
end
|
60
|
+
|
61
|
+
def base_url
|
62
|
+
"http://ceplivre.com.br/consultar/cep/#{ENV['CEP_LIVRE_TOKEN']}/"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module BrZipCode
|
2
|
+
class Service
|
3
|
+
class CorreioControl < BrZipCode::Service
|
4
|
+
def to_hash
|
5
|
+
return nil unless self.valid? and not self.response_body.nil?
|
6
|
+
|
7
|
+
{
|
8
|
+
street: self.response_body['logradouro'],
|
9
|
+
district: self.response_body['bairro'],
|
10
|
+
city: self.response_body['localidade'],
|
11
|
+
state: self.response_body['uf']
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
def valid?
|
16
|
+
super and self.response.code == 200 and not self.response_body.include? 'erro'
|
17
|
+
end
|
18
|
+
|
19
|
+
def final_url
|
20
|
+
"#{base_url}#{self.zip_code}.json"
|
21
|
+
end
|
22
|
+
|
23
|
+
def base_url
|
24
|
+
"http://cep.correiocontrol.com.br/"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module BrZipCode
|
2
|
+
class Service
|
3
|
+
class Postmon < BrZipCode::Service
|
4
|
+
def to_hash
|
5
|
+
return nil unless self.valid? and not self.response_body.nil?
|
6
|
+
|
7
|
+
{
|
8
|
+
street: self.response_body['logradouro'],
|
9
|
+
district: self.response_body['bairro'],
|
10
|
+
city: self.response_body['cidade'],
|
11
|
+
state: self.response_body['estado']
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
def valid?
|
16
|
+
super and self.response.code == 200 and self.response_body != ''
|
17
|
+
end
|
18
|
+
|
19
|
+
def final_url
|
20
|
+
"#{base_url}#{self.zip_code}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def base_url
|
24
|
+
"http://api.postmon.com.br/v1/cep/"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module BrZipCode
|
2
|
+
class Service
|
3
|
+
class RepublicaVirtual < BrZipCode::Service
|
4
|
+
def to_hash
|
5
|
+
return nil unless self.valid? and not self.response_body.nil?
|
6
|
+
|
7
|
+
{
|
8
|
+
street: [self.response_body['tipo_logradouro'],self.response_body['logradouro']].join(' '),
|
9
|
+
district: self.response_body['bairro'],
|
10
|
+
city: self.response_body['cidade'],
|
11
|
+
state: self.response_body['uf']
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
def valid?
|
16
|
+
super and self.response.code == 200 and not self.response_body.include? '"resultado":"0"'
|
17
|
+
end
|
18
|
+
|
19
|
+
def final_url
|
20
|
+
"#{base_url}#{self.zip_code}&formato=json"
|
21
|
+
end
|
22
|
+
|
23
|
+
def base_url
|
24
|
+
"http://cep.republicavirtual.com.br/web_cep.php?cep="
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module BrZipCode
|
2
|
+
class Service
|
3
|
+
class Viacep < BrZipCode::Service
|
4
|
+
def to_hash
|
5
|
+
return nil unless self.valid? and not self.response_body.nil?
|
6
|
+
|
7
|
+
{
|
8
|
+
street: self.response_body['logradouro'],
|
9
|
+
district: self.response_body['bairro'],
|
10
|
+
city: self.response_body['localidade'],
|
11
|
+
state: self.response_body['uf']
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
def valid?
|
16
|
+
super and self.response.code == 200 and not self.response_body.include? 'erro'
|
17
|
+
end
|
18
|
+
|
19
|
+
def final_url
|
20
|
+
"#{base_url}#{self.zip_code}/json/"
|
21
|
+
end
|
22
|
+
|
23
|
+
def base_url
|
24
|
+
"http://viacep.com.br/ws/"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
data/lib/br_zip_code/version.rb
CHANGED
metadata
CHANGED
@@ -1,51 +1,79 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: br_zip_code
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ariel Schvartz
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-07-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
16
30
|
requirements:
|
17
31
|
- - "~>"
|
18
32
|
- !ruby/object:Gem::Version
|
19
|
-
version: '1.
|
33
|
+
version: '1.8'
|
20
34
|
type: :development
|
21
35
|
prerelease: false
|
22
36
|
version_requirements: !ruby/object:Gem::Requirement
|
23
37
|
requirements:
|
24
38
|
- - "~>"
|
25
39
|
- !ruby/object:Gem::Version
|
26
|
-
version: '1.
|
40
|
+
version: '1.8'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rake
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
30
44
|
requirements:
|
31
|
-
- - "
|
45
|
+
- - "~>"
|
32
46
|
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
47
|
+
version: '10.0'
|
34
48
|
type: :development
|
35
49
|
prerelease: false
|
36
50
|
version_requirements: !ruby/object:Gem::Requirement
|
37
51
|
requirements:
|
38
|
-
- - "
|
52
|
+
- - "~>"
|
39
53
|
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: dotenv
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.0'
|
41
69
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
70
|
+
name: pry
|
43
71
|
requirement: !ruby/object:Gem::Requirement
|
44
72
|
requirements:
|
45
73
|
- - ">="
|
46
74
|
- !ruby/object:Gem::Version
|
47
75
|
version: '0'
|
48
|
-
type: :
|
76
|
+
type: :development
|
49
77
|
prerelease: false
|
50
78
|
version_requirements: !ruby/object:Gem::Requirement
|
51
79
|
requirements:
|
@@ -60,22 +88,34 @@ extensions: []
|
|
60
88
|
extra_rdoc_files: []
|
61
89
|
files:
|
62
90
|
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- CODE_OF_CONDUCT.md
|
63
94
|
- Gemfile
|
64
|
-
-
|
65
|
-
-
|
66
|
-
- README.mkdn
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
67
97
|
- Rakefile
|
68
|
-
- app/controllers/br_zip_code/application_controller.rb
|
69
98
|
- app/controllers/br_zip_code/zip_code_controller.rb
|
99
|
+
- bin/console
|
100
|
+
- bin/setup
|
70
101
|
- br_zip_code.gemspec
|
102
|
+
- config/locales/pt-BR.yml
|
71
103
|
- config/routes.rb
|
72
104
|
- lib/br_zip_code.rb
|
105
|
+
- lib/br_zip_code/config.rb
|
73
106
|
- lib/br_zip_code/engine.rb
|
107
|
+
- lib/br_zip_code/service.rb
|
108
|
+
- lib/br_zip_code/service/cep_livre.rb
|
109
|
+
- lib/br_zip_code/service/correio_control.rb
|
110
|
+
- lib/br_zip_code/service/postmon.rb
|
111
|
+
- lib/br_zip_code/service/republica_virtual.rb
|
112
|
+
- lib/br_zip_code/service/viacep.rb
|
74
113
|
- lib/br_zip_code/version.rb
|
75
114
|
homepage: https://github.com/arielschvartz/br_zip_code
|
76
115
|
licenses:
|
77
116
|
- MIT
|
78
|
-
metadata:
|
117
|
+
metadata:
|
118
|
+
allowed_push_host: https://rubygems.org
|
79
119
|
post_install_message:
|
80
120
|
rdoc_options: []
|
81
121
|
require_paths:
|
@@ -92,8 +132,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
132
|
version: '0'
|
93
133
|
requirements: []
|
94
134
|
rubyforge_project:
|
95
|
-
rubygems_version: 2.
|
135
|
+
rubygems_version: 2.4.6
|
96
136
|
signing_key:
|
97
137
|
specification_version: 4
|
98
|
-
summary: BR Zip Code - 0.
|
138
|
+
summary: BR Zip Code - 1.0.0
|
99
139
|
test_files: []
|
data/Gemfile.lock
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
br_zip_code (0.2.3)
|
5
|
-
httparty
|
6
|
-
|
7
|
-
GEM
|
8
|
-
remote: https://rubygems.org/
|
9
|
-
specs:
|
10
|
-
httparty (0.12.0)
|
11
|
-
json (~> 1.8)
|
12
|
-
multi_xml (>= 0.5.2)
|
13
|
-
json (1.8.1)
|
14
|
-
multi_xml (0.5.5)
|
15
|
-
rake (10.3.1)
|
16
|
-
|
17
|
-
PLATFORMS
|
18
|
-
ruby
|
19
|
-
|
20
|
-
DEPENDENCIES
|
21
|
-
br_zip_code!
|
22
|
-
bundler (~> 1.5)
|
23
|
-
httparty
|
24
|
-
rake
|
data/MIT-LICENSE
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
Copyright (c) 2014 Ariel Schvartz
|
2
|
-
|
3
|
-
MIT License
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
-
a copy of this software and associated documentation files (the
|
7
|
-
"Software"), to deal in the Software without restriction, including
|
8
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
-
permit persons to whom the Software is furnished to do so, subject to
|
11
|
-
the following conditions:
|
12
|
-
|
13
|
-
The above copyright notice and this permission notice shall be
|
14
|
-
included in all copies or substantial portions of the Software.
|
15
|
-
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.mkdn
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
# BrZipCode
|
2
|
-
|
3
|
-
## What is this?
|
4
|
-
|
5
|
-
This gem helps you to find an address based on a brazilian zip code. For that, 3 webservices are used:
|
6
|
-
|
7
|
-
* [República Virutal](http://cep.republicavirtual.com.br "República Virtual")
|
8
|
-
* [Correio Control](http://cep.correiocontrol.com.br "Correio Control")
|
9
|
-
* [Via Virtual](http://viavirtual.com.br "Via Virtual")
|
10
|
-
|
11
|
-
The webservices are searched in order. If one is not available, the next is tested until one is available or none.
|
12
|
-
|
13
|
-
# Basic Usage
|
14
|
-
|
15
|
-
## Installation
|
16
|
-
gem 'br_zip_code'
|
17
|
-
|
18
|
-
## How to use?
|
19
|
-
|
20
|
-
### Getting the address
|
21
|
-
|
22
|
-
BrZipCode.get_address("22441090") ==> {:street_type=>"Rua", :street=>"Rua General Venâncio Flores", :state=>"RJ", :city=>"Rio de Janeiro", :neighborhood=>"Leblon"}
|
23
|
-
BrZipCode.get_address("22.441-090") ==> {:street_type=>"Rua", :street=>"Rua General Venâncio Flores", :state=>"RJ", :city=>"Rio de Janeiro", :neighborhood=>"Leblon"}
|
24
|
-
BrZipCode.get_address("22441-090") ==> {:street_type=>"Rua", :street=>"Rua General Venâncio Flores", :state=>"RJ", :city=>"Rio de Janeiro", :neighborhood=>"Leblon"}
|
25
|
-
|
26
|
-
BrZipCode.get_address("12345678") ==> {:error=>"CEP não encontrado"}
|
27
|
-
BrZipCode.get_address("1234567") ==> {:error=>"CEP inválido"}
|
28
|
-
|
29
|
-
### Formatting the number
|
30
|
-
|
31
|
-
BrZipCode.strip("22.441-090") ==> "22441090"
|
32
|
-
BrZipCode.strip("22441-090") ==> "22441090"
|
33
|
-
BrZipCode.strip("22.441090") ==> "22441090"
|
34
|
-
|
35
|
-
BrZipCode.strip("22.441---090") ==> nil
|
36
|
-
BrZipCode.strip("2222") ==> nil
|
37
|
-
|
38
|
-
BrZipCode.formatted("22441090") ==> "22.441-090"
|
39
|
-
BrZipCode.formatted("22.441-090") ==> "22.441-090"
|
40
|
-
|
41
|
-
|
42
|
-
# Zip Code Check
|
43
|
-
|
44
|
-
## Usage
|
45
|
-
Now the route /zip_code/xxxxxxxx.json is accessible.
|
46
|
-
Pass the zip_code and get the response, so you can create AJAX function to autocomplete addresses.
|
47
|
-
|
48
|
-
## Next steps
|
49
|
-
|
50
|
-
Need to build tests.
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
This project rocks and uses MIT-LICENSE.
|