brazil-cep 0.2.0 → 0.4.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7f79497f3008ab411781e49694a291d7e68b6aa13e9f26e98cdc4dd9bc16a346
4
- data.tar.gz: a17b09d8556f278136becb604b8db054cf5e4b0cb4ab16c848485a6611667a45
3
+ metadata.gz: 846ef72872222fae21a466e025f9366f3194e03330c350f82a2e7241e95b2680
4
+ data.tar.gz: 0a2df924f5d863cb398c027ec4b1c97686b3a966ca35a9e2202665f7e64a1133
5
5
  SHA512:
6
- metadata.gz: bf50938524c7564d0aefa7a29e4b5f20898842f2b2e3f9067ea897d0516e9e81db5683586dd0c67de28b3b626bab773d2b1cc0ae1bb25c4c644c6314e34e99cc
7
- data.tar.gz: 53f25c7996df1aac9121a66ea28657badcac31c6866befcfc1c4ef6e6c523b0628925b45a83f99eb77169cd0360eb1ce4a36ed54365930e4744a525545cb9def
6
+ metadata.gz: 12722468d5c1e0187bee7d8c152239017f0c0e7ef256b3b34d46594028d5dbc84fb79db3d6402a8c677c1eacfeeb56ebfd47102e616108804b50f993a198bd4c
7
+ data.tar.gz: 98492724593be83d12735da04a735722108f435b50219c60ad7616bb0aecea70c28e22a8cfe9fc32385e14a1bbf98ab138a26c43a5ea32f40a2ac19a57fc59e7
@@ -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::RequestError, http_response unless http_response.is_a?(Net::HTTPSuccess)
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!
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Brazil
4
+ module Cep
5
+ module Adapters
6
+ # Correios Web provider adapter class
7
+ # This class is responsible for fetching the address information from the Correios Web provider
8
+ class CorreiosWeb < Base
9
+ provider base_url: "https://buscacepinter.correios.com.br/app/endereco/carrega-cep-endereco.php"
10
+
11
+ private
12
+
13
+ def http_request(uri, **options, &block)
14
+ http_options =
15
+ { open_timeout: open_timeout, read_timeout: read_timeout, write_timeout: write_timeout }
16
+ .update(**options)
17
+
18
+ http_options[:use_ssl] = uri.scheme == "https"
19
+
20
+ Net::HTTP.start(uri.host, uri.port, **http_options) do |http|
21
+ request = Net::HTTP::Post.new(uri)
22
+ request.set_form_data(params)
23
+ headers.each { |key, value| request[key] = value }
24
+
25
+ @response = http.request(request)
26
+
27
+ block.call(@response)
28
+ end
29
+ end
30
+
31
+ def headers
32
+ { "Referer" => "https://buscacepinter.correios.com.br/app/endereco/index.php" }
33
+ end
34
+
35
+ def params
36
+ { pagina: "/app/endereco/index.php", cepaux: nil, endereco: @cep, tipoCEP: "ALL" }
37
+ end
38
+
39
+ def transformation!
40
+ data = Array(@payload[:dados]).first
41
+ raise Brazil::Cep::ZipcodeNotFound, @response if data.nil?
42
+
43
+ street, *complement = data[:logradouroDNEC].split(" - ")
44
+
45
+ raise Brazil::Cep::ZipcodeNotFound, @response if data[:cep] != @cep.sub("-", "")
46
+
47
+ address_params = {
48
+ zipcode: data[:cep],
49
+ state: data[:uf],
50
+ city: data[:localidade],
51
+ neighborhood: data[:bairro],
52
+ street: street,
53
+ complement: complement.join(" - "),
54
+ meta: @payload.update(provider: :correios_web)
55
+ }
56
+
57
+ Brazil::Cep::Address.new(**address_params)
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -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: :postmon)
23
+ meta: @payload.update(provider: :viacep)
22
24
  }
23
25
 
24
26
  Brazil::Cep::Address.new(**address_params)
@@ -9,12 +9,14 @@ module Brazil
9
9
  autoload :Viacep, "brazil_cep/adapters/viacep"
10
10
  autoload :Postmon, "brazil_cep/adapters/postmon"
11
11
  autoload :RepublicaVirtual, "brazil_cep/adapters/republica_virtual"
12
+ autoload :CorreiosWeb, "brazil_cep/adapters/correios_web"
12
13
 
13
14
  # list of provider keys and classes
14
15
  PROVIDERS = {
15
16
  viacep: Viacep,
16
17
  postmon: Postmon,
17
- republica_virtual: RepublicaVirtual
18
+ republica_virtual: RepublicaVirtual,
19
+ correios_web: CorreiosWeb
18
20
  }
19
21
 
20
22
  private_constant :PROVIDERS
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Brazil
4
4
  module Cep
5
- VERSION = "0.2.0"
5
+ VERSION = "0.4.0"
6
6
  end
7
7
  end
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
- class RequestError < Error; end
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
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brazil-cep
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Vinciguerra
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-03-28 00:00:00.000000000 Z
11
+ date: 2024-03-29 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Brazil::CEP is a gem to provide interface to use common CEP services
14
14
  email:
@@ -28,6 +28,7 @@ files:
28
28
  - lib/brazil_cep.rb
29
29
  - lib/brazil_cep/adapters.rb
30
30
  - lib/brazil_cep/adapters/base.rb
31
+ - lib/brazil_cep/adapters/correios_web.rb
31
32
  - lib/brazil_cep/adapters/postmon.rb
32
33
  - lib/brazil_cep/adapters/republica_virtual.rb
33
34
  - lib/brazil_cep/adapters/viacep.rb