me_api 2.3.0 → 2.4.1
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/README.md +361 -30
- data/lib/me_api/client_production.rb +158 -0
- data/lib/me_api/client_sandbox.rb +5 -0
- data/lib/me_api/product.rb +19 -0
- data/lib/me_api/version.rb +1 -1
- data/lib/me_api/volume.rb +21 -0
- data/lib/me_api.rb +14 -2
- metadata +8 -5
- data/lib/me_api/client.rb +0 -117
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 60b1301cb0bb6f72cab902ef1b3f435e608013eda2f0044a70bc2b0e02ad0b94
|
4
|
+
data.tar.gz: b1bbec3335f49b905e4dadd983c39a4a6ef45824437037ad3d1395ad452776e4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 71dab8161feeb811e8348733c66526f4665d480a5209669d8237e579226114db8cd6685cc8ee115d19fed04f639c1272614c8ba248a48e9ae7d9b55acf108219
|
7
|
+
data.tar.gz: 40e87c3119e723c18fdc35af59f694066df5af5f1533cf39834dc31a84580d1bdcc9872b97f0ee6e6e872406dddb72ea703af44977ed154899a11939f24269f3
|
data/README.md
CHANGED
@@ -1,58 +1,389 @@
|
|
1
1
|
# MeApi
|
2
2
|
|
3
|
-
|
3
|
+
Uma gem Ruby para integração completa com a API do Melhor Envio, permitindo calcular fretes, criar etiquetas, gerenciar envios e muito mais de forma simples e eficiente.
|
4
|
+
|
5
|
+
[](https://badge.fury.io/rb/me_api)
|
4
6
|
|
5
7
|
[Documentação API Melhor Envio](https://docs.melhorenvio.com.br/reference/introducao-api-melhor-envio)
|
6
8
|
|
7
|
-
|
8
|
-
|
9
|
-
-
|
9
|
+
## Funcionalidades
|
10
|
+
|
11
|
+
- ✅ **Autenticação**: Authorization code e refresh token
|
12
|
+
- ✅ **Cotação de frete**: Calcular preços de envio
|
13
|
+
- ✅ **Gestão de etiquetas**: Criar, pagar, gerar e imprimir etiquetas
|
14
|
+
- ✅ **Consultas**: Dados da conta, saldo, pedidos
|
15
|
+
- ✅ **Suporte a sandbox**: Ambiente de testes
|
16
|
+
- ✅ **Classes auxiliares**: Volume e Product para facilitar o uso
|
10
17
|
|
11
18
|
## Instalação
|
12
19
|
|
13
|
-
|
20
|
+
Adicione esta linha ao Gemfile da sua aplicação:
|
14
21
|
|
15
22
|
```ruby
|
16
23
|
gem 'me_api'
|
17
24
|
```
|
18
|
-
ou
|
19
|
-
```shell
|
20
|
-
bundle add me_api
|
21
|
-
```
|
22
25
|
|
23
|
-
|
26
|
+
E então execute:
|
27
|
+
|
28
|
+
$ bundle install
|
29
|
+
|
30
|
+
Ou instale diretamente com:
|
31
|
+
|
32
|
+
$ gem install me_api
|
33
|
+
|
34
|
+
## Configuração
|
35
|
+
|
36
|
+
Para usar a gem, você precisa ter credenciais da API do Melhor Envio. Você pode obter essas credenciais criando uma aplicação no painel do desenvolvedor do Melhor Envio.
|
37
|
+
|
38
|
+
### Criando um cliente
|
39
|
+
|
40
|
+
A gem suporta dois modos de operação:
|
41
|
+
- **Produção**: Para uso em ambiente de produção
|
42
|
+
- **Sandbox**: Para testes (use `test_mode: true`)
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
# Cliente de produção
|
46
|
+
client = MeApi::Client.new(
|
47
|
+
access_token: "seu_access_token",
|
48
|
+
refresh_token: "seu_refresh_token",
|
49
|
+
client_id: "seu_client_id",
|
50
|
+
client_secret: "seu_client_secret",
|
51
|
+
token_expires_at: Time.now + 3600
|
52
|
+
)
|
24
53
|
|
25
|
-
|
26
|
-
|
54
|
+
# Cliente de sandbox/teste
|
55
|
+
client = MeApi::Client.new(
|
56
|
+
access_token: "seu_access_token",
|
57
|
+
refresh_token: "seu_refresh_token",
|
58
|
+
client_id: "seu_client_id",
|
59
|
+
client_secret: "seu_client_secret",
|
60
|
+
token_expires_at: Time.now + 3600,
|
61
|
+
test_mode: true
|
62
|
+
)
|
27
63
|
```
|
28
64
|
|
29
65
|
## Uso
|
30
66
|
|
31
|
-
###
|
32
|
-
|
67
|
+
### Formato das respostas
|
68
|
+
|
69
|
+
Todas as respostas dos métodos da API retornam objetos `AcObject` ou arrays de `AcObject`. Esses objetos permitem acesso aos dados de duas formas:
|
70
|
+
|
71
|
+
- **Notação de ponto**: `response.access_token`
|
72
|
+
- **Notação de hash**: `response["access_token"]`
|
73
|
+
- **Conversão para JSON**: `response.json` (retorna um hash Ruby)
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
# Exemplo com refresh_token
|
77
|
+
response = client.refresh_token(
|
78
|
+
client_id: "seu_client_id",
|
79
|
+
client_secret: "seu_client_secret",
|
80
|
+
refresh_token: "seu_refresh_token"
|
81
|
+
)
|
82
|
+
|
83
|
+
# Todas essas formas funcionam:
|
84
|
+
puts response.access_token # "novo_access_token"
|
85
|
+
puts response["access_token"] # "novo_access_token"
|
86
|
+
puts response.json["access_token"] # "novo_access_token"
|
87
|
+
|
88
|
+
# Para ver toda a resposta como hash
|
89
|
+
puts response.json
|
90
|
+
# => {"access_token"=>"...", "refresh_token"=>"...", "expires_in"=>21600, "token_type"=>"Bearer"}
|
91
|
+
```
|
92
|
+
|
93
|
+
### Autenticação
|
94
|
+
|
95
|
+
#### Autorizando uma aplicação
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
response = client.authorize(
|
99
|
+
client_id: "seu_client_id",
|
100
|
+
client_secret: "seu_client_secret",
|
101
|
+
code: "codigo_retornado_pela_api",
|
102
|
+
redirect_url: "sua_url_de_redirect"
|
103
|
+
)
|
104
|
+
|
105
|
+
# Acessando dados da resposta
|
106
|
+
puts response.access_token # "abcde12345"
|
107
|
+
puts response.refresh_token # "asdfghjk123456"
|
108
|
+
puts response.expires_in # 21600
|
109
|
+
puts response.token_type # "Bearer"
|
110
|
+
|
111
|
+
# Ou usando notação de hash
|
112
|
+
puts response["access_token"] # "abcde12345"
|
113
|
+
puts response["refresh_token"] # "asdfghjk123456"
|
114
|
+
```
|
115
|
+
|
116
|
+
#### Renovando token
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
response = client.refresh_token(
|
120
|
+
client_id: "seu_client_id",
|
121
|
+
client_secret: "seu_client_secret",
|
122
|
+
refresh_token: "seu_refresh_token"
|
123
|
+
)
|
124
|
+
|
125
|
+
# Acessando dados da resposta
|
126
|
+
puts response.access_token # "novo_access_token"
|
127
|
+
puts response.refresh_token # "novo_refresh_token"
|
128
|
+
puts response.token_type # "Bearer"
|
129
|
+
```
|
130
|
+
|
131
|
+
### Cotação de frete
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
# Criar objeto de volume/dimensões
|
135
|
+
volume = MeApi::Volume.new(
|
136
|
+
width_cm: 20,
|
137
|
+
height_cm: 15,
|
138
|
+
length_cm: 30,
|
139
|
+
weight_kg: 1.5
|
140
|
+
)
|
141
|
+
|
142
|
+
# Calcular frete (retorna array de AcObject)
|
143
|
+
rates = client.rates(
|
144
|
+
from_postal_code: "13870329",
|
145
|
+
to_postal_code: "88063500",
|
146
|
+
volume: volume,
|
147
|
+
contents_value_brl: 50.00
|
148
|
+
)
|
149
|
+
|
150
|
+
# Acessando dados do primeiro resultado
|
151
|
+
puts rates.first.price # Preço do frete mais barato
|
152
|
+
puts rates.first.service_name # Nome do serviço (ex: "PAC")
|
153
|
+
puts rates.first.carrier_name # Nome da transportadora (ex: "Correios")
|
154
|
+
|
155
|
+
# Ou usando notação de hash
|
156
|
+
puts rates.first["price"] # Preço do frete
|
157
|
+
puts rates.first["service_name"] # Nome do serviço
|
158
|
+
```
|
159
|
+
|
160
|
+
### Gestão de produtos e volumes
|
161
|
+
|
162
|
+
#### Criando produtos
|
163
|
+
|
164
|
+
```ruby
|
165
|
+
products = [
|
166
|
+
MeApi::Product.new(
|
167
|
+
name: "Produto A",
|
168
|
+
quantity: 2,
|
169
|
+
unitary_value: 25.00
|
170
|
+
),
|
171
|
+
MeApi::Product.new(
|
172
|
+
name: "Produto B",
|
173
|
+
quantity: 1,
|
174
|
+
unitary_value: 50.00
|
175
|
+
)
|
176
|
+
]
|
177
|
+
```
|
178
|
+
|
179
|
+
#### Criando volumes
|
180
|
+
|
181
|
+
```ruby
|
182
|
+
volumes = [
|
183
|
+
MeApi::Volume.new(
|
184
|
+
width_cm: 20,
|
185
|
+
height_cm: 15,
|
186
|
+
length_cm: 30,
|
187
|
+
weight_kg: 1.5
|
188
|
+
)
|
189
|
+
]
|
190
|
+
```
|
191
|
+
|
192
|
+
### Gestão de etiquetas
|
193
|
+
|
194
|
+
#### Adicionando etiqueta ao carrinho
|
195
|
+
|
196
|
+
```ruby
|
197
|
+
response = client.add_label_to_cart(
|
198
|
+
service_id: 1, # ID do serviço de entrega
|
199
|
+
from_name: "Remetente",
|
200
|
+
from_phone: "(11) 99999-9999",
|
201
|
+
from_email: "remetente@email.com",
|
202
|
+
from_address: "Rua do Remetente",
|
203
|
+
from_address_complement: "Apto 123",
|
204
|
+
from_address_number: "100",
|
205
|
+
from_address_district: "Centro",
|
206
|
+
from_address_city: "São Paulo",
|
207
|
+
from_postal_code: "01000000",
|
208
|
+
from_address_state_abbr: "SP",
|
209
|
+
from_tax_id: "12345678901", # CPF ou CNPJ
|
210
|
+
to_name: "Destinatário",
|
211
|
+
to_phone: "(11) 88888-8888",
|
212
|
+
to_email: "destinatario@email.com",
|
213
|
+
to_address: "Rua do Destinatário",
|
214
|
+
to_address_complement: "",
|
215
|
+
to_address_number: "200",
|
216
|
+
to_address_district: "Vila Nova",
|
217
|
+
to_address_city: "Rio de Janeiro",
|
218
|
+
to_postal_code: "20000000",
|
219
|
+
to_address_state_abbr: "RJ",
|
220
|
+
to_tax_id: "98765432109", # CPF ou CNPJ
|
221
|
+
products: products,
|
222
|
+
volumes: volumes,
|
223
|
+
insurance_value: 100.00
|
224
|
+
)
|
225
|
+
|
226
|
+
# Acessando dados da resposta
|
227
|
+
order_id = response.id # ID do pedido
|
228
|
+
puts response.protocol # Protocolo do pedido
|
229
|
+
puts response["status"] # Status do pedido
|
230
|
+
```
|
231
|
+
|
232
|
+
#### Pagando etiqueta
|
233
|
+
|
234
|
+
```ruby
|
235
|
+
payment_response = client.pay_label(order_id)
|
236
|
+
puts payment_response.purchase_id # ID da compra
|
237
|
+
puts payment_response["digitable_line"] # Linha digitável (se boleto)
|
238
|
+
```
|
239
|
+
|
240
|
+
#### Gerando etiqueta
|
241
|
+
|
33
242
|
```ruby
|
34
|
-
|
35
|
-
#
|
36
|
-
puts api_client["access_token"] # string -> "abcde12345"
|
37
|
-
puts api_client["refresh_token"] # string -> "asdfghjk123456"
|
38
|
-
puts api_client["expires_in"] # integer -> 216000
|
243
|
+
generate_response = client.generate_label(order_id)
|
244
|
+
puts generate_response["status"] # Status da geração
|
39
245
|
```
|
40
246
|
|
41
|
-
####
|
247
|
+
#### Imprimindo etiqueta
|
248
|
+
|
249
|
+
```ruby
|
250
|
+
pdf_response = client.print_label(order_id)
|
251
|
+
puts pdf_response.first # URL do PDF da etiqueta
|
252
|
+
```
|
253
|
+
|
254
|
+
### Consultas
|
255
|
+
|
256
|
+
#### Dados da conta
|
257
|
+
|
42
258
|
```ruby
|
43
|
-
|
44
|
-
#
|
45
|
-
puts
|
46
|
-
puts
|
47
|
-
puts
|
259
|
+
account = client.get_account_data
|
260
|
+
puts account.firstname # Nome do usuário
|
261
|
+
puts account.email # Email da conta
|
262
|
+
puts account["document"] # CPF/CNPJ da conta
|
263
|
+
puts account.json # Todos os dados como hash
|
48
264
|
```
|
49
265
|
|
50
|
-
####
|
266
|
+
#### Saldo disponível
|
267
|
+
|
268
|
+
```ruby
|
269
|
+
balance = client.get_balance
|
270
|
+
puts balance.current # Saldo atual
|
271
|
+
puts balance["currency"] # Moeda (BRL)
|
272
|
+
```
|
273
|
+
|
274
|
+
#### Detalhes de um pedido
|
275
|
+
|
51
276
|
```ruby
|
52
|
-
|
277
|
+
order = client.get_label(order_id)
|
278
|
+
puts order.status # Status do pedido
|
279
|
+
puts order.tracking # Código de rastreamento
|
280
|
+
puts order["service_name"] # Nome do serviço utilizado
|
281
|
+
puts order.json # Todos os dados do pedido
|
282
|
+
```
|
283
|
+
|
284
|
+
## Classes auxiliares
|
285
|
+
|
286
|
+
### MeApi::Volume
|
287
|
+
|
288
|
+
Representa as dimensões e peso de um pacote:
|
289
|
+
|
290
|
+
```ruby
|
291
|
+
volume = MeApi::Volume.new(
|
292
|
+
width_cm: 20, # Largura em centímetros
|
293
|
+
height_cm: 15, # Altura em centímetros
|
294
|
+
length_cm: 30, # Comprimento em centímetros
|
295
|
+
weight_kg: 1.5 # Peso em quilogramas
|
296
|
+
)
|
297
|
+
|
298
|
+
# Converte para formato da API
|
299
|
+
volume.to_api_format
|
300
|
+
# => { width: 20, height: 15, length: 30, weight: 1.5 }
|
301
|
+
```
|
302
|
+
|
303
|
+
### MeApi::Product
|
304
|
+
|
305
|
+
Representa um produto a ser enviado:
|
306
|
+
|
307
|
+
```ruby
|
308
|
+
product = MeApi::Product.new(
|
309
|
+
name: "Nome do produto", # Nome do produto
|
310
|
+
quantity: 2, # Quantidade
|
311
|
+
unitary_value: 25.00 # Valor unitário em reais
|
312
|
+
)
|
313
|
+
|
314
|
+
# Converte para formato da API
|
315
|
+
product.to_api_format
|
316
|
+
# => { name: "Nome do produto", quantity: 2, unitary_value: 25.0 }
|
317
|
+
```
|
318
|
+
|
319
|
+
## Exemplo completo
|
320
|
+
|
321
|
+
```ruby
|
322
|
+
require 'me_api'
|
323
|
+
|
324
|
+
# Configurar cliente
|
325
|
+
client = MeApi::Client.new(
|
326
|
+
access_token: "seu_access_token",
|
327
|
+
refresh_token: "seu_refresh_token",
|
328
|
+
client_id: "seu_client_id",
|
329
|
+
client_secret: "seu_client_secret",
|
330
|
+
token_expires_at: Time.now + 3600
|
331
|
+
)
|
332
|
+
|
333
|
+
# Calcular frete
|
334
|
+
volume = MeApi::Volume.new(width_cm: 20, height_cm: 15, length_cm: 30, weight_kg: 1.5)
|
335
|
+
rates = client.rates(
|
336
|
+
from_postal_code: "13870329",
|
337
|
+
to_postal_code: "88063500",
|
338
|
+
volume: volume,
|
339
|
+
contents_value_brl: 50.00
|
340
|
+
)
|
341
|
+
|
342
|
+
puts "Melhor preço: R$ #{rates.first.price}"
|
343
|
+
puts "Serviço: #{rates.first.service_name}"
|
344
|
+
|
345
|
+
# Criar pedido
|
346
|
+
products = [MeApi::Product.new(name: "Produto A", quantity: 1, unitary_value: 50.00)]
|
347
|
+
volumes = [volume]
|
348
|
+
|
349
|
+
response = client.add_label_to_cart(
|
350
|
+
service_id: rates.first.id,
|
351
|
+
from_name: "Loja Online",
|
352
|
+
from_phone: "(11) 99999-9999",
|
353
|
+
from_email: "loja@email.com",
|
354
|
+
from_address: "Rua da Loja",
|
355
|
+
from_address_number: "123",
|
356
|
+
from_address_district: "Centro",
|
357
|
+
from_address_city: "São Paulo",
|
358
|
+
from_postal_code: "13870329",
|
359
|
+
from_address_state_abbr: "SP",
|
360
|
+
from_tax_id: "12345678000123",
|
361
|
+
to_name: "João Silva",
|
362
|
+
to_phone: "(11) 88888-8888",
|
363
|
+
to_email: "joao@email.com",
|
364
|
+
to_address: "Rua do Cliente",
|
365
|
+
to_address_number: "456",
|
366
|
+
to_address_district: "Vila Nova",
|
367
|
+
to_address_city: "Florianópolis",
|
368
|
+
to_postal_code: "88063500",
|
369
|
+
to_address_state_abbr: "SC",
|
370
|
+
to_tax_id: "98765432109",
|
371
|
+
products: products,
|
372
|
+
volumes: volumes,
|
373
|
+
insurance_value: 50.00
|
374
|
+
)
|
375
|
+
|
376
|
+
order_id = response.id # Usando notação de ponto
|
377
|
+
puts "Pedido criado: #{response.protocol}"
|
378
|
+
|
379
|
+
# Pagar e gerar etiqueta
|
380
|
+
payment = client.pay_label(order_id)
|
381
|
+
puts "Pagamento: #{payment.purchase_id}"
|
53
382
|
|
54
|
-
|
55
|
-
puts
|
383
|
+
generate = client.generate_label(order_id)
|
384
|
+
puts "Status geração: #{generate.status}"
|
56
385
|
|
57
|
-
|
386
|
+
# Obter URL do PDF
|
387
|
+
pdf_response = client.print_label(order_id)
|
388
|
+
puts "Etiqueta: #{pdf_response.first}"
|
58
389
|
```
|
@@ -0,0 +1,158 @@
|
|
1
|
+
module MeApi
|
2
|
+
class ClientProduction < Ac::Base
|
3
|
+
BASE_URL = "https://melhorenvio.com.br"
|
4
|
+
SAVE_RESPONSES = true
|
5
|
+
|
6
|
+
attr_reader :refresh_token, :client_id, :client_secret, :test_mode, :token_expires_at
|
7
|
+
def initialize(access_token: nil, refresh_token: nil, client_id: , client_secret:, token_expires_at:, test_mode: false)
|
8
|
+
@access_token = access_token
|
9
|
+
@refresh_token = refresh_token
|
10
|
+
@client_id = client_id
|
11
|
+
@client_secret = client_secret
|
12
|
+
@test_mode = test_mode
|
13
|
+
@token_expires_at = token_expires_at
|
14
|
+
end
|
15
|
+
|
16
|
+
def refresh_token(client_id:, client_secret:, refresh_token:)
|
17
|
+
body = {
|
18
|
+
grant_type: "refresh_token",
|
19
|
+
client_id: client_id,
|
20
|
+
client_secret: client_secret,
|
21
|
+
refresh_token: refresh_token
|
22
|
+
}
|
23
|
+
post("/oauth/token", body:, skip_authentication: true)
|
24
|
+
end
|
25
|
+
|
26
|
+
def authorize client_id:, client_secret:, code:, redirect_url:
|
27
|
+
body = {
|
28
|
+
grant_type: "authorization_code",
|
29
|
+
client_id: client_id,
|
30
|
+
client_secret: client_secret,
|
31
|
+
code: code,
|
32
|
+
redirect_uri: redirect_url
|
33
|
+
}
|
34
|
+
post("/oauth/token", body:, skip_authentication: true)
|
35
|
+
end
|
36
|
+
|
37
|
+
def default_options
|
38
|
+
{
|
39
|
+
headers: {
|
40
|
+
"Accept" => "application/json",
|
41
|
+
"Content-Type" => "application/json"
|
42
|
+
}
|
43
|
+
}
|
44
|
+
end
|
45
|
+
|
46
|
+
# @param volume [Object<MeApi::Volume>] Volume object containing width_cm, height_cm, length_cm, and weight_kg
|
47
|
+
def rates(from_postal_code:, to_postal_code:, volume:, contents_value_brl:)
|
48
|
+
body = {
|
49
|
+
from: {
|
50
|
+
postal_code: from_postal_code
|
51
|
+
},
|
52
|
+
to: {
|
53
|
+
postal_code: to_postal_code
|
54
|
+
},
|
55
|
+
package: volume.to_api_format,
|
56
|
+
options: {
|
57
|
+
insurance_value: contents_value_brl,
|
58
|
+
receipt: false,
|
59
|
+
own_hand: false
|
60
|
+
},
|
61
|
+
services: "1,2,3,4,17,33"
|
62
|
+
}
|
63
|
+
post("api/v2/me/shipment/calculate", body:) { _1.first.name }
|
64
|
+
end
|
65
|
+
|
66
|
+
# @param products [Array<MeApi::Product>] Array of product objects containing name, quantity, and unitary_value
|
67
|
+
# @param volumes [Array<MeApi::Volume>] Array of volume objects containing width_cm, height_cm, length_cm, and weight_kg
|
68
|
+
def add_label_to_cart(service_id:, from_name:, from_phone:, from_email:, from_address:, from_address_complement:, from_address_number:, from_address_district:, from_address_city:, from_postal_code:, from_address_state_abbr:, from_tax_id:, to_name:, to_phone:, to_email:, to_address:, to_address_complement:, to_address_number:, to_address_district:, to_address_city:, to_postal_code:, to_address_state_abbr:, to_tax_id:, products:, volumes:, insurance_value:)
|
69
|
+
|
70
|
+
from_tax_id_clean = cleaned_tax_id(from_tax_id)
|
71
|
+
to_tax_id_clean = cleaned_tax_id(to_tax_id)
|
72
|
+
|
73
|
+
body = {
|
74
|
+
service: service_id,
|
75
|
+
from: {
|
76
|
+
name: from_name,
|
77
|
+
phone: from_phone,
|
78
|
+
email: from_email,
|
79
|
+
address: from_address,
|
80
|
+
complement: from_address_complement,
|
81
|
+
number: from_address_number,
|
82
|
+
district: from_address_district,
|
83
|
+
city: from_address_city,
|
84
|
+
postal_code: from_postal_code,
|
85
|
+
state_abbr: from_address_state_abbr
|
86
|
+
},
|
87
|
+
|
88
|
+
to: {
|
89
|
+
name: to_name,
|
90
|
+
phone: to_phone,
|
91
|
+
email: to_email,
|
92
|
+
address: to_address,
|
93
|
+
complement: to_address_complement,
|
94
|
+
number: to_address_number,
|
95
|
+
district: to_address_district,
|
96
|
+
city: to_address_city,
|
97
|
+
postal_code: to_postal_code,
|
98
|
+
state_abbr: to_address_state_abbr
|
99
|
+
},
|
100
|
+
products: products.map do |product|
|
101
|
+
product.to_api_format
|
102
|
+
end,
|
103
|
+
volumes: volumes.map do |volume|
|
104
|
+
volume.to_api_format
|
105
|
+
end,
|
106
|
+
options: {
|
107
|
+
insurance_value: insurance_value,
|
108
|
+
}
|
109
|
+
}
|
110
|
+
|
111
|
+
add_tax_document_to_address(body[:from], from_tax_id_clean)
|
112
|
+
add_tax_document_to_address(body[:to], to_tax_id_clean)
|
113
|
+
|
114
|
+
post("/api/v2/me/cart", body:)
|
115
|
+
end
|
116
|
+
|
117
|
+
def pay_label(order_id)
|
118
|
+
body = {
|
119
|
+
orders: [order_id.to_s]
|
120
|
+
}
|
121
|
+
post("/api/v2/me/shipment/checkout", body:)
|
122
|
+
end
|
123
|
+
|
124
|
+
def generate_label(order_id)
|
125
|
+
body = {
|
126
|
+
orders: [order_id.to_s]
|
127
|
+
}
|
128
|
+
post("/api/v2/me/shipment/generate", body:)
|
129
|
+
end
|
130
|
+
|
131
|
+
def print_label(order_id)
|
132
|
+
get("/api/v2/me/imprimir/pdf/#{order_id}") { _1[0].include? order_id }
|
133
|
+
end
|
134
|
+
|
135
|
+
def get_label(order_id)
|
136
|
+
get("/api/v2/me/orders/#{order_id}")
|
137
|
+
end
|
138
|
+
|
139
|
+
def get_account_data
|
140
|
+
get("/api/v2/me")
|
141
|
+
end
|
142
|
+
|
143
|
+
def get_balance
|
144
|
+
get("/api/v2/me/balance")
|
145
|
+
end
|
146
|
+
|
147
|
+
private
|
148
|
+
|
149
|
+
def cleaned_tax_id(tax_id)
|
150
|
+
tax_id.gsub(/\D/, "")
|
151
|
+
end
|
152
|
+
|
153
|
+
def add_tax_document_to_address(address_hash, tax_id)
|
154
|
+
document_key = tax_id.length == 11 ? :document : :company_document
|
155
|
+
address_hash[document_key] = tax_id
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module MeApi
|
2
|
+
class Product
|
3
|
+
attr_accessor :name, :quantity, :unitary_value
|
4
|
+
|
5
|
+
def initialize(name:, quantity:, unitary_value:)
|
6
|
+
@name = name
|
7
|
+
@quantity = quantity
|
8
|
+
@unitary_value = unitary_value
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_api_format
|
12
|
+
{
|
13
|
+
name: name,
|
14
|
+
quantity: quantity,
|
15
|
+
unitary_value: unitary_value
|
16
|
+
}
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/me_api/version.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
module MeApi
|
2
|
+
class Volume
|
3
|
+
attr_accessor :width_cm, :height_cm, :length_cm, :weight_kg
|
4
|
+
|
5
|
+
def initialize(width_cm:, height_cm:, length_cm:, weight_kg:)
|
6
|
+
@width_cm = width_cm
|
7
|
+
@height_cm = height_cm
|
8
|
+
@length_cm = length_cm
|
9
|
+
@weight_kg = weight_kg
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_api_format
|
13
|
+
{
|
14
|
+
width: width_cm,
|
15
|
+
height: height_cm,
|
16
|
+
length: length_cm,
|
17
|
+
weight: weight_kg
|
18
|
+
}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/me_api.rb
CHANGED
@@ -2,9 +2,21 @@
|
|
2
2
|
|
3
3
|
require "ac"
|
4
4
|
require_relative "me_api/version"
|
5
|
-
require_relative "me_api/
|
5
|
+
require_relative "me_api/client_production"
|
6
|
+
require_relative "me_api/client_sandbox"
|
7
|
+
require_relative "me_api/product"
|
8
|
+
require_relative "me_api/volume"
|
6
9
|
|
7
10
|
module MeApi
|
8
11
|
class Error < StandardError; end
|
9
|
-
|
12
|
+
|
13
|
+
class Client
|
14
|
+
def self.new **args
|
15
|
+
if args[:test_mode]
|
16
|
+
ClientSandbox.new(**args)
|
17
|
+
else
|
18
|
+
ClientProduction.new(**args)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
10
22
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: me_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Todas Essas Coisas
|
8
8
|
bindir: exe
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-
|
10
|
+
date: 2025-08-04 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: ac
|
@@ -15,14 +15,14 @@ dependencies:
|
|
15
15
|
requirements:
|
16
16
|
- - ">="
|
17
17
|
- !ruby/object:Gem::Version
|
18
|
-
version:
|
18
|
+
version: 1.1.0
|
19
19
|
type: :runtime
|
20
20
|
prerelease: false
|
21
21
|
version_requirements: !ruby/object:Gem::Requirement
|
22
22
|
requirements:
|
23
23
|
- - ">="
|
24
24
|
- !ruby/object:Gem::Version
|
25
|
-
version:
|
25
|
+
version: 1.1.0
|
26
26
|
email:
|
27
27
|
- contato7bulloleo@gmail.com
|
28
28
|
executables: []
|
@@ -36,8 +36,11 @@ files:
|
|
36
36
|
- README.md
|
37
37
|
- Rakefile
|
38
38
|
- lib/me_api.rb
|
39
|
-
- lib/me_api/
|
39
|
+
- lib/me_api/client_production.rb
|
40
|
+
- lib/me_api/client_sandbox.rb
|
41
|
+
- lib/me_api/product.rb
|
40
42
|
- lib/me_api/version.rb
|
43
|
+
- lib/me_api/volume.rb
|
41
44
|
homepage: https://github.com/todasessascoisas/me_api
|
42
45
|
licenses:
|
43
46
|
- MIT
|
data/lib/me_api/client.rb
DELETED
@@ -1,117 +0,0 @@
|
|
1
|
-
module MeApi
|
2
|
-
class Client < Ac::Base
|
3
|
-
BASE_URL = "https://melhorenvio.com.br"
|
4
|
-
SAVE_RESPONSES = true
|
5
|
-
Rate = Data.define(:id, :from, :to, :price, :min_delivery_time, :max_delivery_time, :service_name, :carrier_name, :carrier_logo_url)
|
6
|
-
|
7
|
-
def rates(from:, to:, weight_kg:, contents_value_brl:, height_cm:, width_cm:, length_cm:)
|
8
|
-
body = {
|
9
|
-
"from" => {
|
10
|
-
"postal_code" => from
|
11
|
-
},
|
12
|
-
"to" => {
|
13
|
-
"postal_code" => to
|
14
|
-
},
|
15
|
-
"package" => {
|
16
|
-
"weight" => weight_kg,
|
17
|
-
"height" => height_cm,
|
18
|
-
"width" => width_cm,
|
19
|
-
"length" => length_cm
|
20
|
-
},
|
21
|
-
"options" => {
|
22
|
-
"insurance_value" => contents_value_brl,
|
23
|
-
"receipt" => false,
|
24
|
-
"own_hand" => false
|
25
|
-
},
|
26
|
-
"services" => "1,2,3,4,17,33"
|
27
|
-
}
|
28
|
-
response = post("api/v2/me/shipment/calculate", body:) { _1.first.name }
|
29
|
-
response.map do |rate|
|
30
|
-
next unless rate["error"].blank? && rate["price"]
|
31
|
-
Rate.new(
|
32
|
-
id: rate.id,
|
33
|
-
from: from,
|
34
|
-
to: to,
|
35
|
-
price: rate.price.to_f,
|
36
|
-
min_delivery_time: rate.delivery_range.min,
|
37
|
-
max_delivery_time: rate.delivery_range.max,
|
38
|
-
service_name: rate.name,
|
39
|
-
carrier_name: rate.company.name,
|
40
|
-
carrier_logo_url: rate.company.picture
|
41
|
-
)
|
42
|
-
end.compact
|
43
|
-
end
|
44
|
-
|
45
|
-
def refresh_token(client_id:, client_secret:, refresh_token:)
|
46
|
-
body = {
|
47
|
-
grant_type: "refresh_token",
|
48
|
-
client_id: client_id,
|
49
|
-
client_secret: client_secret,
|
50
|
-
refresh_token: refresh_token
|
51
|
-
}
|
52
|
-
post("/oauth/token", body:)
|
53
|
-
end
|
54
|
-
|
55
|
-
def authorize client_id:, client_secret:, code:, redirect_url:
|
56
|
-
body = {
|
57
|
-
grant_type: "authorization_code",
|
58
|
-
client_id: client_id,
|
59
|
-
client_secret: client_secret,
|
60
|
-
code: code,
|
61
|
-
redirect_uri: redirect_url
|
62
|
-
}
|
63
|
-
post("/oauth/token", body:, skip_authentication: true)
|
64
|
-
end
|
65
|
-
|
66
|
-
def default_options
|
67
|
-
{
|
68
|
-
headers: {
|
69
|
-
"Accept" => "application/json",
|
70
|
-
"Content-Type" => "application/json"
|
71
|
-
}
|
72
|
-
}
|
73
|
-
end
|
74
|
-
|
75
|
-
def insert_package_into_cart(service_id:, from:, to:, products:, volumes:, options:)
|
76
|
-
body = {
|
77
|
-
service: service_id,
|
78
|
-
from: from,
|
79
|
-
to: to,
|
80
|
-
products: products,
|
81
|
-
volumes: volumes,
|
82
|
-
options: options
|
83
|
-
}
|
84
|
-
post("/api/v2/me/cart", body:)
|
85
|
-
end
|
86
|
-
|
87
|
-
def payment_ticket(order_id)
|
88
|
-
body = {
|
89
|
-
orders: [order_id.to_s]
|
90
|
-
}
|
91
|
-
post("/api/v2/me/shipment/checkout", body:)
|
92
|
-
end
|
93
|
-
|
94
|
-
def generate_ticket(order_id)
|
95
|
-
body = {
|
96
|
-
orders: [order_id.to_s]
|
97
|
-
}
|
98
|
-
post("/api/v2/me/shipment/generate", body:)
|
99
|
-
end
|
100
|
-
|
101
|
-
def print_ticket(order_id)
|
102
|
-
get("/api/v2/me/imprimir/pdf/#{order_id}") { _1[0].include? order_id }
|
103
|
-
end
|
104
|
-
|
105
|
-
def get_ticket(order_id)
|
106
|
-
get("/api/v2/me/orders/#{order_id}")
|
107
|
-
end
|
108
|
-
|
109
|
-
def get_account_data
|
110
|
-
get("/api/v2/me")
|
111
|
-
end
|
112
|
-
|
113
|
-
def get_balance
|
114
|
-
get("/api/v2/me/balance")
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|