brazil-cep 0.3.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0a4a5033b0b8e49dbd7d48f464c76e857a30140c6e29be85ca3440ed45bec589
4
- data.tar.gz: ea457dfc8a836178131f231002907c5f6ae165d0f9124bbb7faa711a7e88784c
3
+ metadata.gz: edf951cca19b548050bc06fa7e535be37dd2b9da172805a329401df5e621b609
4
+ data.tar.gz: d9311ce3cdf198acdd3daaef44fb80f0c7cbf0d318e08efc8445d18bb4adb00a
5
5
  SHA512:
6
- metadata.gz: d632a23cd2e78c93392d3c405a9d9023efc4d639e69ddf3c7bc334b0ebaa48bf42a968067c18fea8e2cae8e7541b17ea56f6265ddfccf1c9bbe829e298b462ca
7
- data.tar.gz: 01e72027e7660a8b6f67a1730df08824bbfc72bcbed978d2c7ee2a87fd13901b62f7130ec0576d5b00fe0aeb3101afdb2196abec7d6df1f610288989bcff9f34
6
+ metadata.gz: 3d1cf60e8aeceefe738a7ca974deb8c00cf33226c3e54327a1b9a17169adf0ff7ed0576d35a45ae24c7a20b511c934cb97a97fd3c553b7727122ec6192b7be54
7
+ data.tar.gz: 623509c7060ee2699e107e8ee6d488e09eb25baa546abf6e6a00959c079b9be51ea371cc1b5ac91094f0f2481b71fbf7541d7e44dcc64effed435ea0401e4b25
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Brazil
4
+ module Cep
5
+ module Adapters
6
+ # Cep Aberto provider adapter class
7
+ # This class is responsible for fetching the address information from the Cep Aberto provider
8
+ class CepAberto < Base
9
+ provider base_url: "https://www.cepaberto.com/ceps/{{cep}}.json"
10
+
11
+ private
12
+
13
+ def transformation!
14
+ raise Brazil::Cep::ZipcodeNotFound, @response if @payload[:erro] == true
15
+
16
+ address_params = {
17
+ zipcode: @payload[:cep],
18
+ state: @payload[:estado][:sigla],
19
+ city: @payload[:cidade][:nome],
20
+ neighborhood: @payload[:bairro],
21
+ street: @payload[:logradouro],
22
+ complement: @payload[:complemento],
23
+ meta: @payload.update(provider: :cep_aberto)
24
+ }
25
+
26
+ Brazil::Cep::Address.new(**address_params)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -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
@@ -9,12 +9,16 @@ 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"
13
+ autoload :CepAberto, "brazil_cep/adapters/cep_aberto"
12
14
 
13
15
  # list of provider keys and classes
14
16
  PROVIDERS = {
15
17
  viacep: Viacep,
16
18
  postmon: Postmon,
17
- republica_virtual: RepublicaVirtual
19
+ republica_virtual: RepublicaVirtual,
20
+ correios_web: CorreiosWeb,
21
+ cep_aberto: CepAberto
18
22
  }
19
23
 
20
24
  private_constant :PROVIDERS
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Brazil
4
4
  module Cep
5
- VERSION = "0.3.0"
5
+ VERSION = "0.5.0"
6
6
  end
7
7
  end
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.3.0
4
+ version: 0.5.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,8 @@ 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/cep_aberto.rb
32
+ - lib/brazil_cep/adapters/correios_web.rb
31
33
  - lib/brazil_cep/adapters/postmon.rb
32
34
  - lib/brazil_cep/adapters/republica_virtual.rb
33
35
  - lib/brazil_cep/adapters/viacep.rb