correios_api 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/correios_api/client.rb +42 -36
- data/lib/correios_api/pre_shipping.rb +11 -11
- data/lib/correios_api/rate.rb +2 -2
- data/lib/correios_api/shipping_quotes.rb +21 -0
- data/lib/correios_api/tracking.rb +20 -0
- data/lib/correios_api/version.rb +1 -1
- data/lib/correios_api.rb +3 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6231f36d0629eb37a315f601efd989c9deef75f17293c88cf17d345ac473efbf
|
4
|
+
data.tar.gz: 31cc6e528e9a5ef6f549f350a0a8708ad31c1d55f7b4af3ea1a48c3a8fede1a5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f79d16d786de5e94e57ccf784bf372e87d5387dfabd27d48bc298ad416f281238c7fdb827731778aefafbf86e79aa5e3f551ce4e86c43245f8cbee4ba286369
|
7
|
+
data.tar.gz: a7403032b2701c0ea50182645ff71597580f8993a78fb19abb427fba172ffca04157fc3d2ee5b53dc0a939f55f7b8f2ef39ab9bbb05ac636988509f473c52695
|
data/lib/correios_api/client.rb
CHANGED
@@ -51,46 +51,51 @@ module CorreiosApi
|
|
51
51
|
@expires_in = Time.new(response.json.dig("expiraEm"))
|
52
52
|
end
|
53
53
|
|
54
|
-
def
|
54
|
+
def tracking_package tracking_code, result_type = "T"
|
55
|
+
response = get("/srorastro/v1/objetos/#{tracking_code}?resultado=#{result_type}")
|
56
|
+
Tracking.new(response.json)
|
57
|
+
end
|
58
|
+
|
59
|
+
def create_pre_shipping shipper_name:, shipper_tax_id:, shipper_zipcode:, shipper_street:, shipper_address_number:, shipper_district:, shipper_city:, shipper_state_code:, receiver_name:, receiver_tax_id:, receiver_zipcode:, receiver_street:, receiver_address_number:, receiver_district:, receiver_city:, receiver_state_code:, service_code:, price:, items:, weight_g:, height_cm: 15, width_cm: 15, length_cm: 15
|
55
60
|
body = {
|
56
61
|
idCorreios: @username,
|
57
62
|
remetente: {
|
58
|
-
nome:
|
59
|
-
cpfCnpj:
|
63
|
+
nome: shipper_name,
|
64
|
+
cpfCnpj: shipper_tax_id,
|
60
65
|
endereco: {
|
61
|
-
cep:
|
62
|
-
logradouro:
|
63
|
-
numero:
|
64
|
-
bairro:
|
65
|
-
cidade:
|
66
|
-
uf:
|
66
|
+
cep: shipper_zipcode,
|
67
|
+
logradouro: shipper_street,
|
68
|
+
numero: shipper_address_number,
|
69
|
+
bairro: shipper_district,
|
70
|
+
cidade: shipper_city,
|
71
|
+
uf: shipper_state_code
|
67
72
|
}
|
68
73
|
},
|
69
74
|
destinatario: {
|
70
|
-
nome:
|
71
|
-
cpfCnpj:
|
75
|
+
nome: receiver_name,
|
76
|
+
cpfCnpj: receiver_tax_id,
|
72
77
|
endereco: {
|
73
|
-
cep:
|
74
|
-
logradouro:
|
75
|
-
numero:
|
76
|
-
bairro:
|
77
|
-
cidade:
|
78
|
-
uf:
|
78
|
+
cep: receiver_zipcode,
|
79
|
+
logradouro: receiver_street,
|
80
|
+
numero: receiver_address_number,
|
81
|
+
bairro: receiver_district,
|
82
|
+
cidade: receiver_city,
|
83
|
+
uf: receiver_state_code
|
79
84
|
}
|
80
85
|
},
|
81
|
-
codigoServico:
|
82
|
-
precoPrePostagem:
|
86
|
+
codigoServico: service_code,
|
87
|
+
precoPrePostagem: price,
|
83
88
|
numeroCartaoPostagem: @post_card,
|
84
|
-
itensDeclaracaoConteudo:
|
85
|
-
pesoInformado:
|
89
|
+
itensDeclaracaoConteudo: items, # conteudo, quantidade, valor
|
90
|
+
pesoInformado: weight_g,
|
86
91
|
codigoFormatoObjetoInformado: "2",
|
87
|
-
alturaInformada:
|
88
|
-
larguraInformada:
|
89
|
-
comprimentoInformado:
|
92
|
+
alturaInformada: height_cm,
|
93
|
+
larguraInformada: width_cm,
|
94
|
+
comprimentoInformado: length_cm,
|
90
95
|
cienteObjetoNaoProibido: "1",
|
91
|
-
modalidadePagamento: "2" # 1-à vista, 2-à faturar, 3- à avista e à afaturar e 4- Prestação de contas recebimento ou pagamento
|
96
|
+
modalidadePagamento: "2", # 1-à vista, 2-à faturar, 3- à avista e à afaturar e 4- Prestação de contas recebimento ou pagamento
|
92
97
|
# solicitarColeta: "N",
|
93
|
-
# dataPrevistaPostagem: "
|
98
|
+
# dataPrevistaPostagem: "29/09/2024"
|
94
99
|
}
|
95
100
|
|
96
101
|
response = post(
|
@@ -104,7 +109,7 @@ module CorreiosApi
|
|
104
109
|
def create_label pre_shipping
|
105
110
|
body = {
|
106
111
|
codigosObjeto: [
|
107
|
-
pre_shipping.
|
112
|
+
pre_shipping.tracking_code
|
108
113
|
],
|
109
114
|
idCorreios: @username,
|
110
115
|
numeroCartaoPostagem: @post_card,
|
@@ -131,11 +136,11 @@ module CorreiosApi
|
|
131
136
|
Label.new(response.json)
|
132
137
|
end
|
133
138
|
|
134
|
-
def create_rates
|
135
|
-
services_with_time = delivery_time(
|
136
|
-
services_with_cost = delivery_cost(
|
139
|
+
def create_rates zipcode_from:, zipcode_to:, weight_g: 1000, height_cm: 15, width_cm: 15, length_cm: 15
|
140
|
+
services_with_time = delivery_time(zipcode_from, zipcode_to)
|
141
|
+
services_with_cost = delivery_cost(zipcode_from, zipcode_to, weight_g, height_cm, width_cm, length_cm)
|
137
142
|
|
138
|
-
services_with_time.map do |service_time|
|
143
|
+
rates = services_with_time.map do |service_time|
|
139
144
|
service_cost = services_with_cost.find { |service| service["coProduto"] == service_time["coProduto"] }
|
140
145
|
Rate.new(
|
141
146
|
service_name: SERVICE_NAMES[service_time["coProduto"]],
|
@@ -150,12 +155,13 @@ module CorreiosApi
|
|
150
155
|
].compact
|
151
156
|
)
|
152
157
|
end
|
158
|
+
ShippingQuotes.new(rates)
|
153
159
|
end
|
154
160
|
|
155
|
-
def delivery_time
|
161
|
+
def delivery_time zipcode_from, zipcode_to
|
156
162
|
body = {
|
157
163
|
idLote: "1",
|
158
|
-
parametrosPrazo: SERVICES.map { |service| { coProduto: service[:code], nuRequisicao: 1, cepOrigem:
|
164
|
+
parametrosPrazo: SERVICES.map { |service| { coProduto: service[:code], nuRequisicao: 1, cepOrigem: zipcode_from, cepDestino: zipcode_to } }
|
159
165
|
}
|
160
166
|
response = post(
|
161
167
|
"/prazo/v1/nacional",
|
@@ -165,13 +171,13 @@ module CorreiosApi
|
|
165
171
|
response.json
|
166
172
|
end
|
167
173
|
|
168
|
-
def delivery_cost
|
174
|
+
def delivery_cost zipcode_from, zipcode_to, weight_g, height_cm, width_cm, length_cm
|
169
175
|
params_produto = SERVICES.map do |service|
|
170
176
|
{
|
171
177
|
coProduto: service[:code],
|
172
178
|
nuRequisicao: "1",
|
173
|
-
cepOrigem:
|
174
|
-
cepDestino:
|
179
|
+
cepOrigem: zipcode_from,
|
180
|
+
cepDestino: zipcode_to,
|
175
181
|
psObjeto: weight_g,
|
176
182
|
altura: height_cm,
|
177
183
|
largura: width_cm,
|
@@ -1,21 +1,21 @@
|
|
1
1
|
module CorreiosApi
|
2
2
|
class PreShipping
|
3
3
|
|
4
|
-
attr_reader :response, :external_id, :
|
4
|
+
attr_reader :response, :external_id, :tracking_code, :service_code, :payment_method_id, :price, :items, :post_prevision, :posting_deadline, :status, :status_detail, :label_type, :package_type
|
5
5
|
def initialize response
|
6
6
|
@response = response
|
7
7
|
@external_id = response.dig("id")
|
8
|
-
@
|
9
|
-
@
|
10
|
-
@
|
11
|
-
@
|
12
|
-
@
|
13
|
-
@
|
14
|
-
@
|
8
|
+
@tracking_code = response.dig("codigoObjeto")
|
9
|
+
@service_code = response.dig("codigoServico")
|
10
|
+
@payment_method_id = response.dig("modalidadePagamento")
|
11
|
+
@price = response.dig("precoPrePostagem")
|
12
|
+
@items = response.dig("itensDeclaracaoConteudo")
|
13
|
+
@post_prevision = response.dig("dataPrevistaPostagem")
|
14
|
+
@posting_deadline = response.dig("prazoPostagem")
|
15
15
|
@status = response.dig("statusAtual")
|
16
|
-
@
|
17
|
-
@
|
18
|
-
@
|
16
|
+
@status_detail = response.dig("descStatusAtual")
|
17
|
+
@label_type = response.dig("tipoRotulo")
|
18
|
+
@package_type = response.dig("tipoObjeto")
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
data/lib/correios_api/rate.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module CorreiosApi
|
2
2
|
class Rate
|
3
3
|
attr_reader :service_name, :service_code, :price, :delivery_time_in_days, :max_delivery_time, :message, :errors
|
4
|
-
def initialize service_name:, service_code:, price:, delivery_time_in_days:, max_delivery_time:, message: nil, errors:
|
4
|
+
def initialize service_name:, service_code:, price:, delivery_time_in_days:, max_delivery_time:, message: nil, errors: []
|
5
5
|
@service_name = service_name
|
6
6
|
@service_code = service_code
|
7
7
|
@price = transform_price(price)
|
@@ -14,7 +14,7 @@ module CorreiosApi
|
|
14
14
|
private
|
15
15
|
|
16
16
|
def transform_price price
|
17
|
-
return
|
17
|
+
return 0.0 unless price
|
18
18
|
price.gsub(",", ".").to_f
|
19
19
|
end
|
20
20
|
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module CorreiosApi
|
2
|
+
class ShippingQuotes
|
3
|
+
|
4
|
+
attr_reader :rates
|
5
|
+
def initialize rates
|
6
|
+
@rates = rates
|
7
|
+
end
|
8
|
+
|
9
|
+
def cheaper_rate
|
10
|
+
valid_rates.sort_by(&:price).first
|
11
|
+
end
|
12
|
+
|
13
|
+
def faster_rate
|
14
|
+
valid_rates.sort_by(&:delivery_time_in_days).first
|
15
|
+
end
|
16
|
+
|
17
|
+
def valid_rates
|
18
|
+
@rates.select { |rate| rate.price > 0 && rate.errors.empty? }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module CorreiosApi
|
2
|
+
class Tracking
|
3
|
+
|
4
|
+
attr_reader :response, :tracking_code, :sigla, :descricao, :categoria, :data_prevista, :weight_g, :height_cm, :width_cm, :length_cm, :formato, :modalidade, :eventos
|
5
|
+
def initialize response
|
6
|
+
@tracking_code = response.dig("objetos", 0, "codObjeto")
|
7
|
+
@sigla = response.dig("objetos", 0, "tipoPostal", "sigla")
|
8
|
+
@descricao = response.dig("objetos", 0, "tipoPostal", "descricao")
|
9
|
+
@categoria = response.dig("objetos", 0, "tipoPostal", "categoria")
|
10
|
+
@data_prevista = response.dig("objetos", 0, "dtPrevista")
|
11
|
+
@weight_g = response.dig("objetos", 0, "peso") * 1000
|
12
|
+
@height_cm = response.dig("objetos", 0, "altura")
|
13
|
+
@width_cm = response.dig("objetos", 0, "largura")
|
14
|
+
@length_cm = response.dig("objetos", 0, "comprimento")
|
15
|
+
@formato = response.dig("objetos", 0, "formato")
|
16
|
+
@modalidade = response.dig("objetos", 0, "modalidade")
|
17
|
+
@eventos = response.dig("objetos", 0, "eventos")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/correios_api/version.rb
CHANGED
data/lib/correios_api.rb
CHANGED
@@ -2,11 +2,14 @@ require_relative "correios_api/version"
|
|
2
2
|
|
3
3
|
require "base64"
|
4
4
|
require "ac"
|
5
|
+
require "dotenv"
|
5
6
|
|
6
7
|
require "correios_api/client"
|
7
8
|
require "correios_api/rate"
|
8
9
|
require "correios_api/pre_shipping"
|
9
10
|
require "correios_api/label"
|
11
|
+
require "correios_api/shipping_quotes"
|
12
|
+
require "correios_api/tracking"
|
10
13
|
|
11
14
|
module CorreiosApi
|
12
15
|
class Error < StandardError; end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: correios_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Todas Essas Coisas
|
@@ -56,6 +56,8 @@ files:
|
|
56
56
|
- lib/correios_api/label.rb
|
57
57
|
- lib/correios_api/pre_shipping.rb
|
58
58
|
- lib/correios_api/rate.rb
|
59
|
+
- lib/correios_api/shipping_quotes.rb
|
60
|
+
- lib/correios_api/tracking.rb
|
59
61
|
- lib/correios_api/version.rb
|
60
62
|
- sig/correios_api.rbs
|
61
63
|
homepage: https://github.com/todasessascoisas/correios_api
|