brazil-cep 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/brazil_cep/adapters/base.rb +2 -1
- data/lib/brazil_cep/adapters/viacep.rb +3 -1
- data/lib/brazil_cep/version.rb +1 -1
- data/lib/brazil_cep.rb +17 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a4a5033b0b8e49dbd7d48f464c76e857a30140c6e29be85ca3440ed45bec589
|
4
|
+
data.tar.gz: ea457dfc8a836178131f231002907c5f6ae165d0f9124bbb7faa711a7e88784c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d632a23cd2e78c93392d3c405a9d9023efc4d639e69ddf3c7bc334b0ebaa48bf42a968067c18fea8e2cae8e7541b17ea56f6265ddfccf1c9bbe829e298b462ca
|
7
|
+
data.tar.gz: 01e72027e7660a8b6f67a1730df08824bbfc72bcbed978d2c7ee2a87fd13901b62f7130ec0576d5b00fe0aeb3101afdb2196abec7d6df1f610288989bcff9f34
|
@@ -31,7 +31,8 @@ module Brazil
|
|
31
31
|
@cep = value unless value.nil?
|
32
32
|
|
33
33
|
http_request(provider_url) do |http_response|
|
34
|
-
raise Brazil::Cep::
|
34
|
+
raise Brazil::Cep::ZipcodeNotFound, http_response if http_response.is_a?(Net::HTTPNotFound)
|
35
|
+
raise Brazil::Cep::RequestError, http_response if http_response.code.to_i >= 400
|
35
36
|
end
|
36
37
|
|
37
38
|
deserialization!
|
@@ -11,6 +11,8 @@ module Brazil
|
|
11
11
|
private
|
12
12
|
|
13
13
|
def transformation!
|
14
|
+
raise Brazil::Cep::ZipcodeNotFound, @response if @payload[:erro] == true
|
15
|
+
|
14
16
|
address_params = {
|
15
17
|
zipcode: @payload[:cep],
|
16
18
|
state: @payload[:uf],
|
@@ -18,7 +20,7 @@ module Brazil
|
|
18
20
|
neighborhood: @payload[:bairro],
|
19
21
|
street: @payload[:logradouro],
|
20
22
|
complement: @payload[:complemento],
|
21
|
-
meta: @payload.update(provider: :
|
23
|
+
meta: @payload.update(provider: :viacep)
|
22
24
|
}
|
23
25
|
|
24
26
|
Brazil::Cep::Address.new(**address_params)
|
data/lib/brazil_cep/version.rb
CHANGED
data/lib/brazil_cep.rb
CHANGED
@@ -8,8 +8,24 @@ module Brazil
|
|
8
8
|
# Brazil::Cep providers a simple way to fetch address information from a Brazilian CEP
|
9
9
|
# @public
|
10
10
|
module Cep
|
11
|
+
# generic error class
|
11
12
|
class Error < StandardError; end
|
12
|
-
|
13
|
+
|
14
|
+
# request error class
|
15
|
+
class RequestError < Error
|
16
|
+
attr_reader :status, :code, :response
|
17
|
+
|
18
|
+
def initialize(response)
|
19
|
+
@response = response
|
20
|
+
@status = response.message || "Undefined"
|
21
|
+
@code = response.code.to_i
|
22
|
+
|
23
|
+
super("Request Error: #{code} #{status} - #{response.body[0..1000].inspect}")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# zipcode not found error class
|
28
|
+
class ZipcodeNotFound < RequestError; end
|
13
29
|
|
14
30
|
# fetch address information from a Brazilian CEP
|
15
31
|
# @param [String] cep the CEP to fetch
|