via_cep 0.2.4.1 → 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/.rubocop.yml +9 -0
- data/Gemfile +2 -0
- data/README.md +11 -4
- data/Rakefile +5 -3
- data/bin/console +6 -3
- data/bin/zipcode +4 -1
- data/lib/via_cep.rb +5 -1
- data/lib/via_cep/address.rb +6 -5
- data/lib/via_cep/errors/address_not_found.rb +14 -0
- data/lib/via_cep/errors/invalid_state_format.rb +3 -0
- data/lib/via_cep/errors/invalid_zipcode_format.rb +3 -0
- data/lib/via_cep/errors/zipcode_not_found.rb +4 -1
- data/lib/via_cep/methods.rb +4 -1
- data/lib/via_cep/{search_by_zipcode.rb → search_by_address.rb} +7 -7
- data/lib/via_cep/utils/utils.rb +3 -0
- data/lib/via_cep/validators/state.rb +22 -0
- data/lib/via_cep/validators/zipcode.rb +4 -1
- data/lib/via_cep/version.rb +3 -1
- data/via_cep.gemspec +21 -16
- metadata +33 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0bfebba5a73a67cffa9d1784375e8cdeb9e68e22
|
4
|
+
data.tar.gz: 3b037be00bf894557952433a442e16328fbcf953
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a55d2404351cc6870708a9aee357cd4547a10fe34956a5a16a6a54717fbdc054476566578393d0ff60f83adb1b86a775741bd3d2b2c7fdb3e33309c1fa7b3ffe
|
7
|
+
data.tar.gz: 216fb6603769db7e3a701b19a6a65bb704dd578100663d01276384416a8dd7f2f1ec5f3cc567fdb67fdc51b0d073f0db29b613d80dac8f88e6f6aac58a793c18
|
data/.rubocop.yml
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# ViaCep
|
2
2
|
|
3
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/via_cep`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
-
|
5
3
|
## Status
|
6
|
-
[![Build Status](https://api.travis-ci.org/marcelobarreto/via_cep.svg?branch=master)](https://travis-ci.org/marcelobarreto/via_cep) [![Code Climate](https://codeclimate.com/github/marcelobarreto/via_cep.svg)](https://codeclimate.com/github/marcelobareto/via_cep) [![Code Climate](https://codeclimate.com/github/marcelobarreto/via_cep/coverage.svg)](https://codeclimate.com/github/marcelobarreto/via_cep)
|
4
|
+
[![Build Status](https://api.travis-ci.org/marcelobarreto/via_cep.svg?branch=master)](https://travis-ci.org/marcelobarreto/via_cep) [![Code Climate](https://codeclimate.com/github/marcelobarreto/via_cep.svg)](https://codeclimate.com/github/marcelobareto/via_cep) [![Code Climate](https://codeclimate.com/github/marcelobarreto/via_cep/coverage.svg)](https://codeclimate.com/github/marcelobarreto/via_cep) [![Ebert](https://ebertapp.io/badges/N68FgrgxUFWYFU43eLvEK4qb.svg)](https://ebertapp.io/repos/85)
|
5
|
+
[![RubyGems](http://img.shields.io/gem/dt/via_cep.svg?style=flat)](http://rubygems.org/gems/via_cep)
|
6
|
+
|
7
7
|
|
8
8
|
## Installation
|
9
9
|
|
@@ -55,6 +55,13 @@ It will return 2 address with its information
|
|
55
55
|
SP
|
56
56
|
```
|
57
57
|
|
58
|
+
And also search for a zipcode by an address
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
search = ViaCep::SearchByAddress.new(state: 'SP', city: 'São Paulo', street: 'Praça da Sé')
|
62
|
+
|
63
|
+
search.zipcode # => "01001-000"
|
64
|
+
```
|
58
65
|
|
59
66
|
### Available methods
|
60
67
|
|
@@ -68,7 +75,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
68
75
|
|
69
76
|
## Contributing
|
70
77
|
|
71
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/marcelobarreto/via_cep. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](
|
78
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/marcelobarreto/via_cep. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](CODE_OF_CONDUCT.md) code of conduct.
|
72
79
|
|
73
80
|
|
74
81
|
## License
|
data/Rakefile
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rspec/core/rake_task'
|
3
5
|
|
4
6
|
RSpec::Core::RakeTask.new(:spec)
|
5
7
|
|
6
|
-
task :
|
8
|
+
task default: :spec
|
data/bin/console
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
# frozen_string_literal: true
|
4
|
+
# encoding: utf-8
|
2
5
|
|
3
|
-
require
|
4
|
-
require
|
6
|
+
require 'bundler/setup'
|
7
|
+
require 'via_cep'
|
5
8
|
|
6
9
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
10
|
# with your gem easier. You can also use a different console, if you like.
|
@@ -10,5 +13,5 @@ require "via_cep"
|
|
10
13
|
# require "pry"
|
11
14
|
# Pry.start
|
12
15
|
|
13
|
-
require
|
16
|
+
require 'irb'
|
14
17
|
IRB.start
|
data/bin/zipcode
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
# frozen_string_literal: true
|
4
|
+
# encoding: utf-8
|
2
5
|
|
3
|
-
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) +
|
6
|
+
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + '/../lib'))
|
4
7
|
require 'via_cep'
|
5
8
|
|
6
9
|
if ARGV.count > 0
|
data/lib/via_cep.rb
CHANGED
@@ -1,18 +1,22 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
1
3
|
require 'httparty'
|
2
4
|
require 'via_cep/version'
|
3
5
|
|
4
6
|
# Core
|
5
7
|
require 'via_cep/address'
|
6
8
|
require 'via_cep/methods'
|
7
|
-
require 'via_cep/
|
9
|
+
require 'via_cep/search_by_address'
|
8
10
|
|
9
11
|
# Validators
|
10
12
|
require 'via_cep/validators/zipcode'
|
13
|
+
require 'via_cep/validators/state'
|
11
14
|
|
12
15
|
# Errors
|
13
16
|
require 'via_cep/errors/invalid_zipcode_format'
|
14
17
|
require 'via_cep/errors/invalid_state_format'
|
15
18
|
require 'via_cep/errors/zipcode_not_found'
|
19
|
+
require 'via_cep/errors/address_not_found'
|
16
20
|
|
17
21
|
# Utils
|
18
22
|
require 'via_cep/utils/utils'
|
data/lib/via_cep/address.rb
CHANGED
@@ -1,15 +1,16 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
1
3
|
require_relative 'methods'
|
2
4
|
|
3
5
|
module ViaCep
|
6
|
+
# Address class
|
4
7
|
class Address
|
5
8
|
def initialize(zipcode)
|
6
9
|
@response = HTTParty.get("https://viacep.com.br/ws/#{zipcode}/json/")
|
7
10
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
raise ViaCep::Errors::ZipcodeNotFound
|
12
|
-
end
|
11
|
+
raise ViaCep::Errors::ZipcodeNotFound if @response['erro']
|
12
|
+
raise ViaCep::Errors::InvalidZipcodeFormat unless ViaCep::Validators::
|
13
|
+
Zipcode.valid?(zipcode)
|
13
14
|
end
|
14
15
|
|
15
16
|
ViaCep::METHODS.each do |method_name, response_method_name|
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
module ViaCep
|
4
|
+
module Errors
|
5
|
+
# This class is responsible to show an error
|
6
|
+
class AddressNotFound < ArgumentError
|
7
|
+
attr_reader :message
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@message = 'Not found anything for that address'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -1,10 +1,13 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
1
3
|
module ViaCep
|
2
4
|
module Errors
|
5
|
+
# This class is responsible to show an error
|
3
6
|
class ZipcodeNotFound < StandardError
|
4
7
|
attr_reader :message
|
5
8
|
|
6
9
|
def initialize
|
7
|
-
@message =
|
10
|
+
@message = 'Not found anything for that zipcode'
|
8
11
|
end
|
9
12
|
end
|
10
13
|
end
|
data/lib/via_cep/methods.rb
CHANGED
@@ -1,4 +1,7 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
1
3
|
module ViaCep
|
4
|
+
# Translate methods allows us to use metaprogramming.
|
2
5
|
METHODS = {
|
3
6
|
zipcode: 'cep',
|
4
7
|
street: 'logradouro',
|
@@ -8,5 +11,5 @@ module ViaCep
|
|
8
11
|
state: 'uf',
|
9
12
|
ibge: 'ibge',
|
10
13
|
gia: 'gia'
|
11
|
-
}
|
14
|
+
}.freeze
|
12
15
|
end
|
@@ -1,20 +1,20 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
1
3
|
require_relative 'methods'
|
2
4
|
|
3
5
|
module ViaCep
|
6
|
+
# Search an address
|
4
7
|
class SearchByAddress
|
5
8
|
def initialize(state:, city:, street:)
|
6
9
|
city = ViaCep::Utils.parameterize(city)
|
7
10
|
street = ViaCep::Utils.parameterize(street)
|
8
11
|
|
9
|
-
|
10
|
-
|
11
|
-
end
|
12
|
+
raise ViaCep::Errors::InvalidStateFormat unless ViaCep::Validators::State
|
13
|
+
.valid?(state)
|
12
14
|
|
13
15
|
@response = HTTParty.get("#{base_url}/#{state}/#{city}/#{street}/json")
|
14
16
|
|
15
|
-
if @response.include?('Bad Request')
|
16
|
-
raise ViaCep::Errors::ZipcodeNotFound
|
17
|
-
end
|
17
|
+
raise ViaCep::Errors::AddressNotFound if @response.include?('Bad Request')
|
18
18
|
end
|
19
19
|
|
20
20
|
ViaCep::METHODS.each do |method_name, response_method_name|
|
@@ -26,7 +26,7 @@ module ViaCep
|
|
26
26
|
private
|
27
27
|
|
28
28
|
def base_url
|
29
|
-
|
29
|
+
'https://viacep.com.br/ws'
|
30
30
|
end
|
31
31
|
end
|
32
32
|
end
|
data/lib/via_cep/utils/utils.rb
CHANGED
@@ -0,0 +1,22 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
module ViaCep
|
4
|
+
module Validators
|
5
|
+
# State validator
|
6
|
+
module State
|
7
|
+
class << self
|
8
|
+
def valid?(state)
|
9
|
+
states.include?(state.to_s)
|
10
|
+
end
|
11
|
+
|
12
|
+
def states
|
13
|
+
%w(
|
14
|
+
AC AL AP AM BA CE DF ES GO
|
15
|
+
MA MT MS MG PR PB PA PE PI
|
16
|
+
RJ RN RS RO RR SC SE SP TO
|
17
|
+
)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -1,11 +1,14 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
1
3
|
module ViaCep
|
2
4
|
module Validators
|
5
|
+
# Zipcode validator
|
3
6
|
module Zipcode
|
4
7
|
class << self
|
5
8
|
def valid?(zipcode)
|
6
9
|
zipcode = zipcode.to_s
|
7
10
|
|
8
|
-
return true if
|
11
|
+
return true if match_regex?(zipcode)
|
9
12
|
end
|
10
13
|
|
11
14
|
def match_regex?(zipcode)
|
data/lib/via_cep/version.rb
CHANGED
data/via_cep.gemspec
CHANGED
@@ -1,27 +1,32 @@
|
|
1
1
|
# coding: utf-8
|
2
|
+
# frozen_string_literal: true
|
2
3
|
lib = File.expand_path('../lib', __FILE__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
5
|
require 'via_cep/version'
|
5
6
|
|
6
7
|
Gem::Specification.new do |s|
|
7
|
-
s.name =
|
8
|
+
s.name = 'via_cep'
|
8
9
|
s.version = ViaCep::VERSION
|
9
|
-
s.authors = [
|
10
|
-
s.email = [
|
11
|
-
s.summary =
|
12
|
-
s.homepage =
|
13
|
-
s.license =
|
10
|
+
s.authors = ['Marcelo Barreto']
|
11
|
+
s.email = ['marcelobarretojunior@gmail.com']
|
12
|
+
s.summary = 'Brazillian zip-code information'
|
13
|
+
s.homepage = 'http://www.github.com/marcelobarreto/via_cep'
|
14
|
+
s.license = 'MIT'
|
14
15
|
|
15
|
-
s.files = `git ls-files -z`.split("\x0").reject
|
16
|
-
|
17
|
-
|
18
|
-
s.require_paths = ["lib"]
|
16
|
+
s.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
19
|
|
20
|
-
s.
|
21
|
-
s.
|
22
|
-
s.
|
23
|
-
|
24
|
-
s.
|
25
|
-
s.
|
20
|
+
s.bindir = 'bin'
|
21
|
+
s.executables = ['zipcode']
|
22
|
+
s.require_paths = ['lib']
|
23
|
+
|
24
|
+
s.add_development_dependency 'bundler', '~> 1.13'
|
25
|
+
s.add_development_dependency 'rake', '~> 11.2'
|
26
|
+
s.add_development_dependency 'rspec', '~> 3.5'
|
27
|
+
s.add_development_dependency 'codeclimate-test-reporter', '~> 0.6'
|
28
|
+
s.add_development_dependency 'rubocop', '~> 0.49.0'
|
29
|
+
s.add_dependency 'httparty', '~> 0.14'
|
30
|
+
s.add_dependency 'activesupport', '~> 5.0'
|
26
31
|
s.add_dependency 'i18n', '~> 0.7'
|
27
32
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: via_cep
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marcelo Barreto
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-05-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,84 +16,98 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '1.
|
19
|
+
version: '1.13'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '1.
|
26
|
+
version: '1.13'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '11.2'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '11.2'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '3.
|
47
|
+
version: '3.5'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '3.
|
54
|
+
version: '3.5'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: codeclimate-test-reporter
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '0.
|
61
|
+
version: '0.6'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '0.
|
68
|
+
version: '0.6'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.49.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.49.0
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
84
|
name: httparty
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
72
86
|
requirements:
|
73
87
|
- - "~>"
|
74
88
|
- !ruby/object:Gem::Version
|
75
|
-
version: '0.
|
89
|
+
version: '0.14'
|
76
90
|
type: :runtime
|
77
91
|
prerelease: false
|
78
92
|
version_requirements: !ruby/object:Gem::Requirement
|
79
93
|
requirements:
|
80
94
|
- - "~>"
|
81
95
|
- !ruby/object:Gem::Version
|
82
|
-
version: '0.
|
96
|
+
version: '0.14'
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: activesupport
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
86
100
|
requirements:
|
87
101
|
- - "~>"
|
88
102
|
- !ruby/object:Gem::Version
|
89
|
-
version: '
|
103
|
+
version: '5.0'
|
90
104
|
type: :runtime
|
91
105
|
prerelease: false
|
92
106
|
version_requirements: !ruby/object:Gem::Requirement
|
93
107
|
requirements:
|
94
108
|
- - "~>"
|
95
109
|
- !ruby/object:Gem::Version
|
96
|
-
version: '
|
110
|
+
version: '5.0'
|
97
111
|
- !ruby/object:Gem::Dependency
|
98
112
|
name: i18n
|
99
113
|
requirement: !ruby/object:Gem::Requirement
|
@@ -119,6 +133,7 @@ files:
|
|
119
133
|
- ".codeclimate.yml"
|
120
134
|
- ".gitignore"
|
121
135
|
- ".rspec"
|
136
|
+
- ".rubocop.yml"
|
122
137
|
- ".travis.yml"
|
123
138
|
- CODE_OF_CONDUCT.md
|
124
139
|
- Gemfile
|
@@ -130,12 +145,14 @@ files:
|
|
130
145
|
- bin/zipcode
|
131
146
|
- lib/via_cep.rb
|
132
147
|
- lib/via_cep/address.rb
|
148
|
+
- lib/via_cep/errors/address_not_found.rb
|
133
149
|
- lib/via_cep/errors/invalid_state_format.rb
|
134
150
|
- lib/via_cep/errors/invalid_zipcode_format.rb
|
135
151
|
- lib/via_cep/errors/zipcode_not_found.rb
|
136
152
|
- lib/via_cep/methods.rb
|
137
|
-
- lib/via_cep/
|
153
|
+
- lib/via_cep/search_by_address.rb
|
138
154
|
- lib/via_cep/utils/utils.rb
|
155
|
+
- lib/via_cep/validators/state.rb
|
139
156
|
- lib/via_cep/validators/zipcode.rb
|
140
157
|
- lib/via_cep/version.rb
|
141
158
|
- via_cep.gemspec
|
@@ -159,7 +176,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
159
176
|
version: '0'
|
160
177
|
requirements: []
|
161
178
|
rubyforge_project:
|
162
|
-
rubygems_version: 2.
|
179
|
+
rubygems_version: 2.6.14
|
163
180
|
signing_key:
|
164
181
|
specification_version: 4
|
165
182
|
summary: Brazillian zip-code information
|