via_cep 2.0.1 → 3.1.4
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 +4 -4
- data/.codeclimate.yml +8 -5
- data/.github/dependabot.yml +18 -0
- data/.github/pull_request_template.md +19 -0
- data/.gitignore +0 -1
- data/.rubocop.yml +11 -8
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +13 -5
- data/FUNDING.yml +3 -0
- data/Gemfile +0 -7
- data/Gemfile.lock +65 -0
- data/Makefile +2 -0
- data/README.md +15 -9
- data/Rakefile +0 -1
- data/bin/console +0 -2
- data/bin/zipcode +2 -4
- data/lib/via_cep/address.rb +37 -17
- data/lib/via_cep/http.rb +25 -0
- data/lib/via_cep/instance.rb +21 -0
- data/lib/via_cep/methods.rb +16 -10
- data/lib/via_cep/search_by_address.rb +37 -16
- data/lib/via_cep/validators.rb +49 -0
- data/lib/via_cep/version.rb +1 -2
- data/lib/via_cep.rb +20 -13
- data/via_cep.gemspec +18 -15
- metadata +64 -20
- data/lib/via_cep/errors/address_not_found.rb +0 -15
- data/lib/via_cep/errors/invalid_state_format.rb +0 -15
- data/lib/via_cep/errors/invalid_zipcode_format.rb +0 -15
- data/lib/via_cep/errors/zipcode_not_found.rb +0 -15
- data/lib/via_cep/utils/utils.rb +0 -24
- data/lib/via_cep/validators/state.rb +0 -23
- data/lib/via_cep/validators/zipcode.rb +0 -22
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a5b87542eabffdf88cf0c29abc14f443096126c90ebbe62cca399977cd32ddf6
|
|
4
|
+
data.tar.gz: 5aab5e128777294d118d7b79547f2b220b11551f74bf39eddb8e6367758ccf91
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 165e86cfe16bbdcbeeac1f279969fe347aaa5b5988cf28f1b049a7bdc50102e1998822ccf7f14dd01446a9fb9c70385e75527bb26b601e21af2a658f2be39f42
|
|
7
|
+
data.tar.gz: cb3130b542ef1b02b5f1345f1205c4c30c587d14d674b1e8043b015e49c314d5a77580293c5052381e43a6daf49239bc0d978f4bd1d75d3aa700b1d09a31e587
|
data/.codeclimate.yml
CHANGED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
version: 2
|
|
2
|
+
updates:
|
|
3
|
+
- package-ecosystem: bundler
|
|
4
|
+
directory: "/"
|
|
5
|
+
schedule:
|
|
6
|
+
interval: weekly
|
|
7
|
+
time: "09:00"
|
|
8
|
+
open-pull-requests-limit: 10
|
|
9
|
+
ignore:
|
|
10
|
+
- dependency-name: rspec
|
|
11
|
+
versions:
|
|
12
|
+
- 3.10.0
|
|
13
|
+
- dependency-name: rubocop
|
|
14
|
+
versions:
|
|
15
|
+
- 1.10.0
|
|
16
|
+
- 1.11.0
|
|
17
|
+
- 1.8.1
|
|
18
|
+
- 1.9.1
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
AllCops:
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
Enabled:
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
2
|
+
SuggestExtensions: false
|
|
3
|
+
Exclude:
|
|
4
|
+
- 'spec/**/*.rb'
|
|
5
|
+
Style/Documentation:
|
|
6
|
+
Enabled: false
|
|
7
|
+
Style/HashEachMethods:
|
|
8
|
+
Enabled: false
|
|
9
|
+
Lint/MissingSuper:
|
|
10
|
+
Enabled: false
|
|
11
|
+
Lint/UriEscapeUnescape:
|
|
12
|
+
Enabled: false
|
data/.ruby-gemset
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
via_cep
|
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
2.6.3
|
data/.travis.yml
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
|
+
env:
|
|
2
|
+
global:
|
|
3
|
+
- CC_TEST_REPORTER_ID=$CODE_CLIMATE_TOKEN
|
|
1
4
|
language: ruby
|
|
2
5
|
rvm:
|
|
3
|
-
- 2.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
- 2.6.3
|
|
7
|
+
before_script:
|
|
8
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
|
9
|
+
- chmod +x ./cc-test-reporter
|
|
10
|
+
- ./cc-test-reporter before-build
|
|
11
|
+
- gem install bundler:2.1.4
|
|
12
|
+
script:
|
|
13
|
+
- bundle exec rspec
|
|
14
|
+
after_script:
|
|
15
|
+
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
|
data/FUNDING.yml
ADDED
data/Gemfile
CHANGED
|
@@ -1,13 +1,6 @@
|
|
|
1
|
-
# encoding: utf-8
|
|
2
1
|
# frozen_string_literal: true
|
|
3
2
|
|
|
4
3
|
source 'https://rubygems.org'
|
|
5
4
|
|
|
6
5
|
# Specify your gem's dependencies in via_cep.gemspec
|
|
7
6
|
gemspec
|
|
8
|
-
|
|
9
|
-
group :test do
|
|
10
|
-
gem 'codeclimate-test-reporter', '~> 0.6'
|
|
11
|
-
gem 'rubocop', '~> 0.49.0'
|
|
12
|
-
gem 'rspec', '~> 3.5'
|
|
13
|
-
end
|
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
via_cep (3.1.4)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
ast (2.4.2)
|
|
10
|
+
diff-lcs (1.4.4)
|
|
11
|
+
docile (1.4.1)
|
|
12
|
+
json (2.6.3)
|
|
13
|
+
parallel (1.22.1)
|
|
14
|
+
parser (3.2.0.0)
|
|
15
|
+
ast (~> 2.4.1)
|
|
16
|
+
rainbow (3.1.1)
|
|
17
|
+
rake (13.0.3)
|
|
18
|
+
regexp_parser (2.6.1)
|
|
19
|
+
rexml (3.2.5)
|
|
20
|
+
rspec (3.9.0)
|
|
21
|
+
rspec-core (~> 3.9.0)
|
|
22
|
+
rspec-expectations (~> 3.9.0)
|
|
23
|
+
rspec-mocks (~> 3.9.0)
|
|
24
|
+
rspec-core (3.9.3)
|
|
25
|
+
rspec-support (~> 3.9.3)
|
|
26
|
+
rspec-expectations (3.9.2)
|
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
28
|
+
rspec-support (~> 3.9.0)
|
|
29
|
+
rspec-mocks (3.9.1)
|
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
31
|
+
rspec-support (~> 3.9.0)
|
|
32
|
+
rspec-support (3.9.3)
|
|
33
|
+
rubocop (1.43.0)
|
|
34
|
+
json (~> 2.3)
|
|
35
|
+
parallel (~> 1.10)
|
|
36
|
+
parser (>= 3.2.0.0)
|
|
37
|
+
rainbow (>= 2.2.2, < 4.0)
|
|
38
|
+
regexp_parser (>= 1.8, < 3.0)
|
|
39
|
+
rexml (>= 3.2.5, < 4.0)
|
|
40
|
+
rubocop-ast (>= 1.24.1, < 2.0)
|
|
41
|
+
ruby-progressbar (~> 1.7)
|
|
42
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
|
43
|
+
rubocop-ast (1.24.1)
|
|
44
|
+
parser (>= 3.1.1.0)
|
|
45
|
+
ruby-progressbar (1.11.0)
|
|
46
|
+
simplecov (0.22.0)
|
|
47
|
+
docile (~> 1.1)
|
|
48
|
+
simplecov-html (~> 0.11)
|
|
49
|
+
simplecov_json_formatter (~> 0.1)
|
|
50
|
+
simplecov-html (0.13.0)
|
|
51
|
+
simplecov_json_formatter (0.1.4)
|
|
52
|
+
unicode-display_width (2.4.2)
|
|
53
|
+
|
|
54
|
+
PLATFORMS
|
|
55
|
+
ruby
|
|
56
|
+
|
|
57
|
+
DEPENDENCIES
|
|
58
|
+
rake (~> 13.0.3)
|
|
59
|
+
rspec (~> 3.5)
|
|
60
|
+
rubocop (~> 1.43.0)
|
|
61
|
+
simplecov (~> 0.22.0)
|
|
62
|
+
via_cep!
|
|
63
|
+
|
|
64
|
+
BUNDLED WITH
|
|
65
|
+
2.1.4
|
data/Makefile
ADDED
data/README.md
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
# ViaCep
|
|
2
2
|
|
|
3
3
|
## Status
|
|
4
|
-
|
|
4
|
+
|
|
5
|
+
[](https://travis-ci.org/marcelobarreto/via_cep)
|
|
6
|
+
[](https://codeclimate.com/github/marcelobareto/via_cep)
|
|
7
|
+
[](https://codeclimate.com/github/marcelobarreto/via_cep)
|
|
8
|
+
[](https://github.com/rubocop/rubocop)
|
|
9
|
+
[](http://rubygems.org/gems/via_cep)
|
|
5
10
|
|
|
6
11
|
|
|
7
12
|
## Installation
|
|
@@ -14,28 +19,29 @@ gem 'via_cep'
|
|
|
14
19
|
|
|
15
20
|
And then execute:
|
|
16
21
|
|
|
17
|
-
|
|
22
|
+
```$ bundle```
|
|
18
23
|
|
|
19
24
|
Or install it yourself as:
|
|
20
25
|
|
|
21
|
-
|
|
26
|
+
```$ gem install via_cep```
|
|
22
27
|
|
|
23
28
|
## Usage
|
|
24
29
|
|
|
25
30
|
On terminal you can use via_cep like this:
|
|
31
|
+
|
|
26
32
|
```ruby
|
|
27
|
-
|
|
33
|
+
require 'via_cep'
|
|
28
34
|
|
|
29
|
-
|
|
30
|
-
|
|
35
|
+
address = ViaCep::Address.new('01001-000')
|
|
36
|
+
address = ViaCep::Address.new(01001000)
|
|
31
37
|
|
|
32
|
-
|
|
38
|
+
address.street # Returns "Praça da Sé"
|
|
33
39
|
```
|
|
34
40
|
|
|
35
41
|
You can use from terminal either
|
|
36
42
|
|
|
37
43
|
```bash
|
|
38
|
-
|
|
44
|
+
$ zipcode 01001-000 01406000
|
|
39
45
|
```
|
|
40
46
|
It will return 2 address with its information
|
|
41
47
|
|
|
@@ -64,7 +70,7 @@ search.zipcode # => "01001-000"
|
|
|
64
70
|
|
|
65
71
|
### Available methods
|
|
66
72
|
|
|
67
|
-
|
|
73
|
+
zipcode, street, complement, neighborhood, city, state, ibge, gia.
|
|
68
74
|
|
|
69
75
|
## Development
|
|
70
76
|
|
data/Rakefile
CHANGED
data/bin/console
CHANGED
data/bin/zipcode
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env ruby
|
|
2
|
-
# encoding: utf-8
|
|
3
2
|
# frozen_string_literal: true
|
|
4
|
-
# encoding: utf-8
|
|
5
3
|
|
|
6
|
-
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)
|
|
4
|
+
$LOAD_PATH.unshift(File.expand_path("#{File.dirname(__FILE__)}/../lib"))
|
|
7
5
|
require 'via_cep'
|
|
8
6
|
|
|
9
|
-
if ARGV.count
|
|
7
|
+
if ARGV.count.positive?
|
|
10
8
|
ARGV.each do |zipcode|
|
|
11
9
|
address = ViaCep::Address.new(zipcode)
|
|
12
10
|
puts "#{zipcode}:"
|
data/lib/via_cep/address.rb
CHANGED
|
@@ -1,29 +1,49 @@
|
|
|
1
|
-
# encoding: utf-8
|
|
2
1
|
# frozen_string_literal: true
|
|
3
2
|
|
|
4
|
-
require_relative 'methods'
|
|
5
|
-
|
|
6
3
|
module ViaCep
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
class Address < Instance
|
|
5
|
+
attr_reader :zipcode
|
|
6
|
+
|
|
7
|
+
#
|
|
8
|
+
# Initialize an instance of ViaCep::Address and retrieve the CEP using the ViaCep WS API.
|
|
9
|
+
#
|
|
10
|
+
# Retrieve a zipcode
|
|
11
|
+
# ViaCep::Address.new('01001000')
|
|
12
|
+
# #=> #<ViaCep::Address:0x0000558da60b3770 @zipcode="01001-000",
|
|
13
|
+
# @street="Praca da Se", @complement="lado impar", @neighborhood="Se", @city="Sao Paulo", @state="SP",
|
|
14
|
+
# @ibge="3550308", @gia="1004", @error=nil>
|
|
15
|
+
#
|
|
16
|
+
# Retrieve a zipcode with a formatted string
|
|
17
|
+
# ViaCep::Address.new('01001-000')
|
|
18
|
+
# #=> #<ViaCep::Address:0x000055e5429aef98 @zipcode="01001-000", @street="Praca da Se", @complement="lado impar",
|
|
19
|
+
# @neighborhood="Se", @city="Sao Paulo", @state="SP", @ibge="3550308", @gia="1004", @error=nil>
|
|
20
|
+
#
|
|
21
|
+
# raise [ArgumentError] Error raised when zipcode is nil.
|
|
22
|
+
# raise [ViaCep::Errors::InvalidZipcodeFormat] Errorraised when zipcode format is invalid.
|
|
23
|
+
# raise [ViaCep::Errors::ZipcodeNotFound] Error raised when the external API is down or the CEP does not exist.
|
|
24
|
+
#
|
|
25
|
+
# return [ViaCep::Address]
|
|
26
|
+
#
|
|
27
|
+
|
|
9
28
|
def initialize(zipcode)
|
|
10
|
-
|
|
11
|
-
|
|
29
|
+
@zipcode = zipcode
|
|
30
|
+
valid?
|
|
31
|
+
call_service
|
|
32
|
+
rescue JSON::ParserError, Net::HTTPBadRequest
|
|
33
|
+
raise ViaCep::Errors::ZipcodeNotFound
|
|
34
|
+
end
|
|
12
35
|
|
|
13
|
-
|
|
14
|
-
Zipcode.valid?(zipcode)
|
|
36
|
+
private
|
|
15
37
|
|
|
16
|
-
|
|
17
|
-
|
|
38
|
+
def call_service
|
|
39
|
+
response = JSON.parse(ViaCep::HTTP.get(path: zipcode).body)
|
|
40
|
+
raise ViaCep::Errors::ZipcodeNotFound if response['erro']
|
|
18
41
|
|
|
19
|
-
|
|
20
|
-
end
|
|
42
|
+
define_attributes(response)
|
|
21
43
|
end
|
|
22
44
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
@response[response_method_name]
|
|
26
|
-
end
|
|
45
|
+
def valid?
|
|
46
|
+
raise ViaCep::Errors::InvalidZipcodeFormat unless ViaCep::Validators::Zipcode.valid?(zipcode)
|
|
27
47
|
end
|
|
28
48
|
end
|
|
29
49
|
end
|
data/lib/via_cep/http.rb
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ViaCep
|
|
4
|
+
module HTTP
|
|
5
|
+
#
|
|
6
|
+
# Main HTTP module using Net::HTTP
|
|
7
|
+
#
|
|
8
|
+
# return [Net::HTTPOK] | [Net::HTTPBadRequest]
|
|
9
|
+
#
|
|
10
|
+
def self.get(path:, query: {})
|
|
11
|
+
Net::HTTP.get_response(ViaCep::HTTP.uri(path: path, query: query))
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def self.was_successful?(request)
|
|
15
|
+
request.code.eql?('200')
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.uri(path:, query: {})
|
|
19
|
+
base_uri = URI(BASE_URL)
|
|
20
|
+
base_uri.path = "/ws/#{URI::Parser.new.escape(path)}/json"
|
|
21
|
+
base_uri.query = URI.encode_www_form(query)
|
|
22
|
+
base_uri
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ViaCep
|
|
4
|
+
class Instance
|
|
5
|
+
#
|
|
6
|
+
# Default class to define methods based on the response
|
|
7
|
+
#
|
|
8
|
+
# return [ViaCep::Instance]
|
|
9
|
+
#
|
|
10
|
+
def define_attributes(response)
|
|
11
|
+
ViaCep::METHODS.each do |method_name, response_key|
|
|
12
|
+
value = response[response_key.to_s]
|
|
13
|
+
|
|
14
|
+
instance_variable_set("@#{method_name}", value)
|
|
15
|
+
self.class.define_method(method_name) do
|
|
16
|
+
instance_variable_get("@#{method_name}")
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
data/lib/via_cep/methods.rb
CHANGED
|
@@ -1,16 +1,22 @@
|
|
|
1
|
-
# encoding: utf-8
|
|
2
1
|
# frozen_string_literal: true
|
|
3
2
|
|
|
4
3
|
module ViaCep
|
|
5
|
-
#
|
|
4
|
+
#
|
|
5
|
+
# Constant with the params mapped
|
|
6
|
+
#
|
|
7
|
+
# return Hash
|
|
8
|
+
#
|
|
6
9
|
METHODS = {
|
|
7
|
-
zipcode:
|
|
8
|
-
street:
|
|
9
|
-
complement:
|
|
10
|
-
neighborhood:
|
|
11
|
-
city:
|
|
12
|
-
state:
|
|
13
|
-
ibge:
|
|
14
|
-
gia:
|
|
10
|
+
zipcode: :cep,
|
|
11
|
+
street: :logradouro,
|
|
12
|
+
complement: :complemento,
|
|
13
|
+
neighborhood: :bairro,
|
|
14
|
+
city: :localidade,
|
|
15
|
+
state: :uf,
|
|
16
|
+
ibge: :ibge,
|
|
17
|
+
gia: :gia,
|
|
18
|
+
error: :erro,
|
|
19
|
+
ddd: :ddd,
|
|
20
|
+
siafi: :siafi
|
|
15
21
|
}.freeze
|
|
16
22
|
end
|
|
@@ -1,28 +1,49 @@
|
|
|
1
|
-
# encoding: utf-8
|
|
2
1
|
# frozen_string_literal: true
|
|
3
2
|
|
|
4
|
-
require_relative 'methods'
|
|
5
|
-
|
|
6
3
|
module ViaCep
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
class SearchByAddress < Instance
|
|
5
|
+
attr_reader :state, :city, :street
|
|
6
|
+
|
|
7
|
+
#
|
|
8
|
+
# Initialize an instance of ViaCep::SearchByAddress and retrieve the CEP using the ViaCep WS API.
|
|
9
|
+
#
|
|
10
|
+
# Search for a zipcode through the address
|
|
11
|
+
# ViaCep::SearchByAddress.new(state: 'SP', city: 'Sao Paulo', street: 'Praca da Se')
|
|
12
|
+
# #=> #<ViaCep::SearchByAddress:0x000055e542d90210 @state="SP", @city="Sao Paulo", @street="Praca da Se",
|
|
13
|
+
# @zipcode="01001-000", @complement="lado impar", @neighborhood="Se", @ibge="3550308", @gia="1004", @error=nil>
|
|
14
|
+
#
|
|
15
|
+
# raise [ViaCep::Errors::InvalidAddressFormat] Error raised when there is any address field empty.
|
|
16
|
+
# raise [ViaCep::Errors::InvalidStateFormat] Error raised when the state is not a valid one
|
|
17
|
+
#
|
|
18
|
+
# return [ViaCep::SearchByAddress]
|
|
19
|
+
#
|
|
20
|
+
|
|
9
21
|
def initialize(state:, city:, street:)
|
|
10
|
-
|
|
11
|
-
|
|
22
|
+
@state = state
|
|
23
|
+
@city = city
|
|
24
|
+
@street = street
|
|
12
25
|
|
|
13
|
-
|
|
26
|
+
valid?
|
|
27
|
+
call_service
|
|
28
|
+
rescue JSON::ParserError, Net::HTTPBadRequest
|
|
29
|
+
raise ViaCep::Errors::AddressNotFound
|
|
30
|
+
end
|
|
14
31
|
|
|
15
|
-
|
|
16
|
-
request = Net::HTTP.get_response(uri)
|
|
32
|
+
private
|
|
17
33
|
|
|
18
|
-
|
|
19
|
-
raise ViaCep::Errors::
|
|
34
|
+
def valid?
|
|
35
|
+
raise ViaCep::Errors::InvalidAddressFormat unless state && city && street
|
|
36
|
+
raise ViaCep::Errors::InvalidStateFormat unless ViaCep::Validators::State.valid?(state)
|
|
20
37
|
end
|
|
21
38
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
39
|
+
def call_service
|
|
40
|
+
request = HTTP.get(path: "#{state}/#{city}/#{street}")
|
|
41
|
+
raise ViaCep::Errors::AddressNotFound unless HTTP.was_successful?(request)
|
|
42
|
+
|
|
43
|
+
response = JSON.parse(request.body)
|
|
44
|
+
raise ViaCep::Errors::AddressNotFound if response.length.eql?(0)
|
|
45
|
+
|
|
46
|
+
define_attributes(response[0])
|
|
26
47
|
end
|
|
27
48
|
end
|
|
28
49
|
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ViaCep
|
|
4
|
+
module Validators
|
|
5
|
+
module State
|
|
6
|
+
class << self
|
|
7
|
+
#
|
|
8
|
+
# Validates if the state passed is a valid one.
|
|
9
|
+
#
|
|
10
|
+
# Return if is valid or not
|
|
11
|
+
# ViaCep::Validators::State.valid?('foo')
|
|
12
|
+
# #=> false
|
|
13
|
+
# ViaCep::Validators::State.valid?('RJ')
|
|
14
|
+
# #=> true
|
|
15
|
+
#
|
|
16
|
+
# return Boolean
|
|
17
|
+
#
|
|
18
|
+
def valid?(state)
|
|
19
|
+
%w[
|
|
20
|
+
AC AL AP AM BA CE DF ES GO
|
|
21
|
+
MA MT MS MG PR PB PA PE PI
|
|
22
|
+
RJ RN RS RO RR SC SE SP TO
|
|
23
|
+
].include?(state.to_s)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Zipcode validator
|
|
29
|
+
module Zipcode
|
|
30
|
+
class << self
|
|
31
|
+
#
|
|
32
|
+
# Validates if the zipcode passed is a valid one.
|
|
33
|
+
#
|
|
34
|
+
# Return if is valid or not
|
|
35
|
+
# ViaCep::Validators::Zipcode.valid?('0')
|
|
36
|
+
# #=> false
|
|
37
|
+
# ViaCep::Validators::Zipcode.valid?('01001000')
|
|
38
|
+
# #=> true
|
|
39
|
+
#
|
|
40
|
+
# return Boolean
|
|
41
|
+
#
|
|
42
|
+
def valid?(zipcode)
|
|
43
|
+
zipcode = zipcode.to_s
|
|
44
|
+
!!zipcode.match(/^[0-9]{5}(-)[0-9]{3}$/) || !!zipcode.match(/^[0-9]{8}$/)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
data/lib/via_cep/version.rb
CHANGED
data/lib/via_cep.rb
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
# encoding: utf-8
|
|
2
1
|
# frozen_string_literal: true
|
|
3
2
|
|
|
4
3
|
require 'via_cep/version'
|
|
@@ -6,22 +5,30 @@ require 'net/http'
|
|
|
6
5
|
require 'uri'
|
|
7
6
|
require 'json'
|
|
8
7
|
|
|
8
|
+
# Validators
|
|
9
|
+
require 'via_cep/validators'
|
|
10
|
+
|
|
11
|
+
# HTTP
|
|
12
|
+
require 'via_cep/http'
|
|
13
|
+
|
|
9
14
|
# Core
|
|
10
|
-
require 'via_cep/address'
|
|
11
15
|
require 'via_cep/methods'
|
|
16
|
+
require 'via_cep/instance'
|
|
17
|
+
require 'via_cep/address'
|
|
12
18
|
require 'via_cep/search_by_address'
|
|
13
19
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
20
|
+
module ViaCep
|
|
21
|
+
BASE_URL = 'https://viacep.com.br'
|
|
22
|
+
|
|
23
|
+
module Errors
|
|
24
|
+
class ZipcodeNotFound < StandardError; end
|
|
25
|
+
|
|
26
|
+
class InvalidZipcodeFormat < StandardError; end
|
|
17
27
|
|
|
18
|
-
|
|
19
|
-
require 'via_cep/errors/invalid_zipcode_format'
|
|
20
|
-
require 'via_cep/errors/invalid_state_format'
|
|
21
|
-
require 'via_cep/errors/zipcode_not_found'
|
|
22
|
-
require 'via_cep/errors/address_not_found'
|
|
28
|
+
class InvalidStateFormat < StandardError; end
|
|
23
29
|
|
|
24
|
-
|
|
25
|
-
require 'via_cep/utils/utils'
|
|
30
|
+
class InvalidAddressFormat < StandardError; end
|
|
26
31
|
|
|
27
|
-
|
|
32
|
+
class AddressNotFound < StandardError; end
|
|
33
|
+
end
|
|
34
|
+
end
|
data/via_cep.gemspec
CHANGED
|
@@ -1,26 +1,29 @@
|
|
|
1
|
-
# coding: utf-8
|
|
2
1
|
# frozen_string_literal: true
|
|
3
2
|
|
|
4
|
-
lib = File.expand_path('
|
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
|
5
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
6
5
|
require 'via_cep/version'
|
|
7
6
|
|
|
8
|
-
Gem::Specification.new do |
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
7
|
+
Gem::Specification.new do |spec|
|
|
8
|
+
spec.name = 'via_cep'
|
|
9
|
+
spec.version = ViaCep::VERSION
|
|
10
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.6')
|
|
11
|
+
spec.authors = ['Marcelo Barreto (@marcelobarreto)']
|
|
12
|
+
spec.email = ['marcelobarretojunior@gmail.com']
|
|
13
|
+
spec.summary = 'Brazillian zip-code information'
|
|
14
|
+
spec.homepage = 'http://www.github.com/marcelobarreto/via_cep'
|
|
15
|
+
spec.license = 'MIT'
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
18
18
|
f.match(%r{^(test|spec|features)/})
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
spec.bindir = 'bin'
|
|
22
|
+
spec.executables = ['zipcode']
|
|
23
|
+
spec.require_paths = ['lib']
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
spec.add_development_dependency 'rake', '~> 13.0.3'
|
|
26
|
+
spec.add_development_dependency 'rspec', '~> 3.5'
|
|
27
|
+
spec.add_development_dependency 'rubocop', '~> 1.43.0'
|
|
28
|
+
spec.add_development_dependency 'simplecov', '~> 0.22.0'
|
|
26
29
|
end
|
metadata
CHANGED
|
@@ -1,30 +1,72 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: via_cep
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 3.1.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Marcelo Barreto (@marcelobarreto)
|
|
8
|
-
autorequire:
|
|
8
|
+
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2024-09-08 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
|
-
name:
|
|
14
|
+
name: rake
|
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
|
16
16
|
requirements:
|
|
17
17
|
- - "~>"
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
|
-
version:
|
|
20
|
-
type: :
|
|
19
|
+
version: 13.0.3
|
|
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:
|
|
27
|
-
|
|
26
|
+
version: 13.0.3
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rspec
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '3.5'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '3.5'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rubocop
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: 1.43.0
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: 1.43.0
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: simplecov
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: 0.22.0
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: 0.22.0
|
|
69
|
+
description:
|
|
28
70
|
email:
|
|
29
71
|
- marcelobarretojunior@gmail.com
|
|
30
72
|
executables:
|
|
@@ -33,13 +75,20 @@ extensions: []
|
|
|
33
75
|
extra_rdoc_files: []
|
|
34
76
|
files:
|
|
35
77
|
- ".codeclimate.yml"
|
|
78
|
+
- ".github/dependabot.yml"
|
|
79
|
+
- ".github/pull_request_template.md"
|
|
36
80
|
- ".gitignore"
|
|
37
81
|
- ".rspec"
|
|
38
82
|
- ".rubocop.yml"
|
|
83
|
+
- ".ruby-gemset"
|
|
84
|
+
- ".ruby-version"
|
|
39
85
|
- ".travis.yml"
|
|
40
86
|
- CODE_OF_CONDUCT.md
|
|
87
|
+
- FUNDING.yml
|
|
41
88
|
- Gemfile
|
|
89
|
+
- Gemfile.lock
|
|
42
90
|
- LICENSE.txt
|
|
91
|
+
- Makefile
|
|
43
92
|
- README.md
|
|
44
93
|
- Rakefile
|
|
45
94
|
- bin/console
|
|
@@ -47,22 +96,18 @@ files:
|
|
|
47
96
|
- bin/zipcode
|
|
48
97
|
- lib/via_cep.rb
|
|
49
98
|
- lib/via_cep/address.rb
|
|
50
|
-
- lib/via_cep/
|
|
51
|
-
- lib/via_cep/
|
|
52
|
-
- lib/via_cep/errors/invalid_zipcode_format.rb
|
|
53
|
-
- lib/via_cep/errors/zipcode_not_found.rb
|
|
99
|
+
- lib/via_cep/http.rb
|
|
100
|
+
- lib/via_cep/instance.rb
|
|
54
101
|
- lib/via_cep/methods.rb
|
|
55
102
|
- lib/via_cep/search_by_address.rb
|
|
56
|
-
- lib/via_cep/
|
|
57
|
-
- lib/via_cep/validators/state.rb
|
|
58
|
-
- lib/via_cep/validators/zipcode.rb
|
|
103
|
+
- lib/via_cep/validators.rb
|
|
59
104
|
- lib/via_cep/version.rb
|
|
60
105
|
- via_cep.gemspec
|
|
61
106
|
homepage: http://www.github.com/marcelobarreto/via_cep
|
|
62
107
|
licenses:
|
|
63
108
|
- MIT
|
|
64
109
|
metadata: {}
|
|
65
|
-
post_install_message:
|
|
110
|
+
post_install_message:
|
|
66
111
|
rdoc_options: []
|
|
67
112
|
require_paths:
|
|
68
113
|
- lib
|
|
@@ -70,16 +115,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
70
115
|
requirements:
|
|
71
116
|
- - ">="
|
|
72
117
|
- !ruby/object:Gem::Version
|
|
73
|
-
version: '
|
|
118
|
+
version: '2.6'
|
|
74
119
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
75
120
|
requirements:
|
|
76
121
|
- - ">="
|
|
77
122
|
- !ruby/object:Gem::Version
|
|
78
123
|
version: '0'
|
|
79
124
|
requirements: []
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
signing_key:
|
|
125
|
+
rubygems_version: 3.4.6
|
|
126
|
+
signing_key:
|
|
83
127
|
specification_version: 4
|
|
84
128
|
summary: Brazillian zip-code information
|
|
85
129
|
test_files: []
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
# encoding: utf-8
|
|
2
|
-
# frozen_string_literal: true
|
|
3
|
-
|
|
4
|
-
module ViaCep
|
|
5
|
-
module Errors
|
|
6
|
-
# This class is responsible to show an error
|
|
7
|
-
class AddressNotFound < ArgumentError
|
|
8
|
-
attr_reader :message
|
|
9
|
-
|
|
10
|
-
def initialize
|
|
11
|
-
@message = 'Not found anything for that address'
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
end
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
# encoding: utf-8
|
|
2
|
-
# frozen_string_literal: true
|
|
3
|
-
|
|
4
|
-
module ViaCep
|
|
5
|
-
module Errors
|
|
6
|
-
# This class is responsible to show an error
|
|
7
|
-
class InvalidStateFormat < ArgumentError
|
|
8
|
-
attr_reader :message
|
|
9
|
-
|
|
10
|
-
def initialize
|
|
11
|
-
@message = 'State has an invalid format'
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
end
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
# encoding: utf-8
|
|
2
|
-
# frozen_string_literal: true
|
|
3
|
-
|
|
4
|
-
module ViaCep
|
|
5
|
-
module Errors
|
|
6
|
-
# This class is responsible to show an error
|
|
7
|
-
class InvalidZipcodeFormat < ArgumentError
|
|
8
|
-
attr_reader :message
|
|
9
|
-
|
|
10
|
-
def initialize
|
|
11
|
-
@message = 'Zipcode has an invalid format'
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
end
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
# encoding: utf-8
|
|
2
|
-
# frozen_string_literal: true
|
|
3
|
-
|
|
4
|
-
module ViaCep
|
|
5
|
-
module Errors
|
|
6
|
-
# This class is responsible to show an error
|
|
7
|
-
class ZipcodeNotFound < StandardError
|
|
8
|
-
attr_reader :message
|
|
9
|
-
|
|
10
|
-
def initialize
|
|
11
|
-
@message = 'Not found anything for that zipcode'
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
end
|
data/lib/via_cep/utils/utils.rb
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
# encoding: utf-8
|
|
2
|
-
# frozen_string_literal: true
|
|
3
|
-
|
|
4
|
-
require 'active_support/all'
|
|
5
|
-
|
|
6
|
-
module ViaCep
|
|
7
|
-
# Utils module
|
|
8
|
-
class Utils
|
|
9
|
-
class << self
|
|
10
|
-
def handle_whitespaces(string)
|
|
11
|
-
string.split(' ').join('%20')
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
def handle_accents(string)
|
|
15
|
-
ActiveSupport::Inflector.transliterate(string)
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
def parameterize(string)
|
|
19
|
-
string = handle_accents(string)
|
|
20
|
-
handle_whitespaces(string)
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
end
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
# encoding: utf-8
|
|
2
|
-
# frozen_string_literal: true
|
|
3
|
-
|
|
4
|
-
module ViaCep
|
|
5
|
-
module Validators
|
|
6
|
-
# State validator
|
|
7
|
-
module State
|
|
8
|
-
class << self
|
|
9
|
-
def valid?(state)
|
|
10
|
-
states.include?(state.to_s)
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
def states
|
|
14
|
-
%w[
|
|
15
|
-
AC AL AP AM BA CE DF ES GO
|
|
16
|
-
MA MT MS MG PR PB PA PE PI
|
|
17
|
-
RJ RN RS RO RR SC SE SP TO
|
|
18
|
-
]
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
end
|
|
22
|
-
end
|
|
23
|
-
end
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
# encoding: utf-8
|
|
2
|
-
# frozen_string_literal: true
|
|
3
|
-
|
|
4
|
-
module ViaCep
|
|
5
|
-
module Validators
|
|
6
|
-
# Zipcode validator
|
|
7
|
-
module Zipcode
|
|
8
|
-
class << self
|
|
9
|
-
def valid?(zipcode)
|
|
10
|
-
zipcode = zipcode.to_s
|
|
11
|
-
|
|
12
|
-
return true if match_regex?(zipcode)
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
def match_regex?(zipcode)
|
|
16
|
-
zipcode = zipcode.to_s
|
|
17
|
-
zipcode.match(/^[0-9]{5}(-)[0-9]{3}$/) || zipcode.match(/^[0-9]{8}$/)
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
end
|
|
22
|
-
end
|