mercado_livre_api 0.1.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 +7 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +10 -0
- data/LICENSE.txt +21 -0
- data/README.md +103 -0
- data/Rakefile +4 -0
- data/lib/mercado_livre_api/client.rb +149 -0
- data/lib/mercado_livre_api/version.rb +5 -0
- data/lib/mercado_livre_api.rb +9 -0
- data/sig/mercado_livre_api.rbs +4 -0
- metadata +70 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: d4c78b8b5d387198744fca75d8c77d372bde101b2c1a5d6117ee3b7d56e3d4cc
|
|
4
|
+
data.tar.gz: 74a9a1fe28494f6525ff0210b2dc0cde0682e97fe9f85c1fe0b3f7e9afa6545a
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 31cd6e0091c48a92d84e75b15692167d4893f7ef29de0fd6370a6b572c383b687b7477f05affbe59dca10e80936acb15c4000dcb098b8d221c3e790924a1b630
|
|
7
|
+
data.tar.gz: e1dc377c3fac7093308805d22a511de11fc1c05cb43847bb3097a700e3516e30bd734e74275579b35ce8ec4afc4e5138417241463e1a6bfa543adbaa0b280abb
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Code of Conduct
|
|
2
|
+
|
|
3
|
+
"mercado_livre_api" follows [The Ruby Community Conduct Guideline](https://www.ruby-lang.org/en/conduct) in all "collaborative space", which is defined as community communications channels (such as mailing lists, submitted patches, commit comments, etc.):
|
|
4
|
+
|
|
5
|
+
* Participants will be tolerant of opposing views.
|
|
6
|
+
* Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks.
|
|
7
|
+
* When interpreting the words and actions of others, participants should always assume good intentions.
|
|
8
|
+
* Behaviour which can be reasonably considered harassment will not be tolerated.
|
|
9
|
+
|
|
10
|
+
If you have any concerns about behaviour within this project, please contact us at ["marialuiza.nerypinto@yahoo.com.br"](mailto:"marialuiza.nerypinto@yahoo.com.br").
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 marialuiza1324
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# MercadoLivreApi
|
|
2
|
+
|
|
3
|
+
Cliente Ruby para a API do Mercado Livre: autenticação OAuth (geração de
|
|
4
|
+
código, troca por token, refresh) e endpoints de pedidos, itens, informações
|
|
5
|
+
de faturamento, envios (incluindo etiqueta), nota fiscal e packs. Construído
|
|
6
|
+
em cima da gem [`ac`](https://rubygems.org/gems/ac) (`Ac::Base`), que cuida
|
|
7
|
+
das requisições HTTP, parsing da resposta e do fluxo de autenticação.
|
|
8
|
+
|
|
9
|
+
Usado pelo [`solidus_mercado_livre`](https://github.com/todasessascoisas/solidus_mercado_livre)
|
|
10
|
+
para integrar o Mercado Livre como canal de marketplace no Solidus, mas não
|
|
11
|
+
depende de Rails/Solidus — pode ser usado isoladamente.
|
|
12
|
+
|
|
13
|
+
## Requisitos
|
|
14
|
+
|
|
15
|
+
- Ruby >= 3.2.0
|
|
16
|
+
- `ac`
|
|
17
|
+
|
|
18
|
+
## Instalação
|
|
19
|
+
|
|
20
|
+
Adicione ao `Gemfile` da aplicação:
|
|
21
|
+
|
|
22
|
+
```ruby
|
|
23
|
+
gem "mercado_livre_api"
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
E então execute:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
bundle install
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Ou instale diretamente:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
gem install mercado_livre_api
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Uso
|
|
39
|
+
|
|
40
|
+
### Autenticação (OAuth)
|
|
41
|
+
|
|
42
|
+
```ruby
|
|
43
|
+
client = MercadoLivreApi::Client.new(
|
|
44
|
+
client_id: "...",
|
|
45
|
+
client_secret: "...",
|
|
46
|
+
redirect_uri: "https://seuapp.com/mercado_livre/authorize"
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
# 1. Redirecione o usuário para client.generate_code
|
|
50
|
+
# 2. Depois do callback, troque o `code` recebido pelo token:
|
|
51
|
+
client.authorize(params[:code])
|
|
52
|
+
client.access_token # => token de acesso
|
|
53
|
+
client.user_id # => id do vendedor autenticado
|
|
54
|
+
|
|
55
|
+
# Persista o necessário pra reconstruir o client depois:
|
|
56
|
+
client.token_attributes # => { access_token:, refresh_token:, token_expires_at: }
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Para reconstruir um client já autenticado a partir de tokens salvos:
|
|
60
|
+
|
|
61
|
+
```ruby
|
|
62
|
+
client = MercadoLivreApi::Client.new(
|
|
63
|
+
client_id: "...",
|
|
64
|
+
client_secret: "...",
|
|
65
|
+
redirect_uri: "...",
|
|
66
|
+
access_token: saved_access_token,
|
|
67
|
+
refresh_token: saved_refresh_token,
|
|
68
|
+
token_expires_at: saved_token_expires_at
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
client.refresh_token if client.should_refresh?
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Pedidos e envios
|
|
75
|
+
|
|
76
|
+
```ruby
|
|
77
|
+
client.get_order(order_id)
|
|
78
|
+
client.get_orders(user_id: seller_id, from: 1.day.ago, to: Time.current) # pagina automaticamente
|
|
79
|
+
client.get_items(order_id)
|
|
80
|
+
client.get_billing_info(billing_info_id)
|
|
81
|
+
|
|
82
|
+
client.get_shipment(shipment_id)
|
|
83
|
+
client.get_shipment_label(shipment_id)
|
|
84
|
+
client.get_shipment_costs(shipment_id)
|
|
85
|
+
|
|
86
|
+
client.get_pack(pack_id)
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Nota fiscal
|
|
90
|
+
|
|
91
|
+
```ruby
|
|
92
|
+
client.send_invoice_xml(shipment_id, xml) # importa a NF-e, libera a etiqueta
|
|
93
|
+
client.update_invoice_xml(invoice_id, xml) # atualiza uma NF-e já enviada
|
|
94
|
+
client.get_invoice_data(shipment_id)
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Contributing
|
|
98
|
+
|
|
99
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/todasessascoisas/mercado_livre_api.
|
|
100
|
+
|
|
101
|
+
## License
|
|
102
|
+
|
|
103
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
module MercadoLivreApi
|
|
2
|
+
class Client < Ac::Base
|
|
3
|
+
BASE_URL = "https://api.mercadolibre.com"
|
|
4
|
+
SAVE_RESPONSES = true
|
|
5
|
+
|
|
6
|
+
attr_reader :user_id, :client_id, :client_secret, :redirect_uri
|
|
7
|
+
|
|
8
|
+
def initialize(client_id:, client_secret:, redirect_uri:, access_token: nil, refresh_token: nil, token_expires_at: nil)
|
|
9
|
+
@client_id = client_id
|
|
10
|
+
@client_secret = client_secret
|
|
11
|
+
@redirect_uri = redirect_uri
|
|
12
|
+
@access_token = access_token
|
|
13
|
+
@refresh_token = refresh_token
|
|
14
|
+
@token_expires_at = token_expires_at
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def generate_code
|
|
18
|
+
body = URI.encode_www_form(
|
|
19
|
+
response_type: "code",
|
|
20
|
+
client_id: client_id,
|
|
21
|
+
redirect_uri: redirect_uri
|
|
22
|
+
)
|
|
23
|
+
"https://auth.mercadolivre.com.br/authorization?" + body
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def authorize(code)
|
|
27
|
+
body = {
|
|
28
|
+
grant_type: "authorization_code",
|
|
29
|
+
client_id: client_id,
|
|
30
|
+
client_secret: client_secret,
|
|
31
|
+
code: code,
|
|
32
|
+
redirect_uri: redirect_uri
|
|
33
|
+
}
|
|
34
|
+
response = post("/oauth/token", body:, skip_authentication: true) { _1["access_token"] }
|
|
35
|
+
@access_token = response.access_token
|
|
36
|
+
@refresh_token = response.refresh_token
|
|
37
|
+
@token_expires_at = Time.now + response.expires_in.to_i
|
|
38
|
+
@user_id = response.user_id
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def refresh_token
|
|
42
|
+
body = {
|
|
43
|
+
grant_type: "refresh_token",
|
|
44
|
+
client_id: client_id,
|
|
45
|
+
client_secret: client_secret,
|
|
46
|
+
refresh_token: @refresh_token
|
|
47
|
+
}
|
|
48
|
+
response = post("/oauth/token", body:, skip_authentication: true) { _1["access_token"] }
|
|
49
|
+
@access_token = response.access_token
|
|
50
|
+
@refresh_token = response.refresh_token
|
|
51
|
+
@token_expires_at = Time.now + response.expires_in.to_i
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def get_refresh_token
|
|
55
|
+
@refresh_token
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def should_refresh?
|
|
59
|
+
@access_token.blank? || (@token_expires_at && @token_expires_at < 1.hour.from_now)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def token_attributes
|
|
63
|
+
{
|
|
64
|
+
access_token: access_token,
|
|
65
|
+
refresh_token: @refresh_token,
|
|
66
|
+
token_expires_at: token_expires_at
|
|
67
|
+
}
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def fetch_resource(path)
|
|
71
|
+
get(path)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def get_order(order_id)
|
|
75
|
+
get("/orders/#{order_id}") { _1.id }
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def get_orders(user_id:, from: nil, to: nil, offset: 0)
|
|
79
|
+
params = {
|
|
80
|
+
seller: user_id,
|
|
81
|
+
sort: "date_asc"
|
|
82
|
+
}
|
|
83
|
+
params["order.date_created.from"] = from.iso8601 if from
|
|
84
|
+
params["order.date_created.to"] = to.iso8601 if to
|
|
85
|
+
|
|
86
|
+
results = []
|
|
87
|
+
total = nil
|
|
88
|
+
|
|
89
|
+
while total.nil? || offset < total
|
|
90
|
+
params.merge!(offset:)
|
|
91
|
+
page = get("/orders/search", params:) { _1.results.is_a?(Array) }.json
|
|
92
|
+
page_results = page.fetch("results", [])
|
|
93
|
+
break if page_results.empty?
|
|
94
|
+
|
|
95
|
+
results.concat(page_results)
|
|
96
|
+
offset += page_results.size
|
|
97
|
+
total = page.dig("paging", "total") || offset
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
results
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def get_items(order_id)
|
|
104
|
+
get("/orders/#{order_id}/items")
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def get_billing_info(billing_info_id)
|
|
108
|
+
get("/orders/billing-info/MLB/#{billing_info_id}")
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def get_shipment(shipment_id)
|
|
112
|
+
get("/shipments/#{shipment_id}")
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def get_shipment_label(shipment_id)
|
|
116
|
+
params = {
|
|
117
|
+
shipment_ids: shipment_id,
|
|
118
|
+
response_type: "pdf"
|
|
119
|
+
}
|
|
120
|
+
get("/shipment_labels", params:)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# importação de NF-e — desbloqueia o substatus invoice_pending do envio e libera a geração da etiqueta
|
|
124
|
+
def send_invoice_xml(shipment_id, xml)
|
|
125
|
+
headers = {"Content-Type" => "application/xml"}
|
|
126
|
+
params = {siteId: "MLB"}
|
|
127
|
+
post("/shipments/#{shipment_id}/invoice_data/", headers:, params:, body: xml)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def update_invoice_xml(invoice_id, xml)
|
|
131
|
+
headers = {"Content-Type" => "application/xml"}
|
|
132
|
+
params = {siteId: "MLB"}
|
|
133
|
+
put("/shipment_invoice/#{invoice_id}", headers:, params:, body: xml)
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def get_invoice_data(shipment_id)
|
|
137
|
+
params = {siteId: "MLB"}
|
|
138
|
+
get("/shipments/#{shipment_id}/invoice_data", params:)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def get_shipment_costs(shipment_id)
|
|
142
|
+
get("/shipments/#{shipment_id}/costs")
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def get_pack(pack_id)
|
|
146
|
+
get("/packs/#{pack_id}") { _1.id }
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: mercado_livre_api
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- marialuiza1324
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-10-28 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: ac
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '0'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '0'
|
|
27
|
+
description:
|
|
28
|
+
email:
|
|
29
|
+
- marialuiza.nerypinto@yahoo.com.br
|
|
30
|
+
executables: []
|
|
31
|
+
extensions: []
|
|
32
|
+
extra_rdoc_files: []
|
|
33
|
+
files:
|
|
34
|
+
- CHANGELOG.md
|
|
35
|
+
- CODE_OF_CONDUCT.md
|
|
36
|
+
- LICENSE.txt
|
|
37
|
+
- README.md
|
|
38
|
+
- Rakefile
|
|
39
|
+
- lib/mercado_livre_api.rb
|
|
40
|
+
- lib/mercado_livre_api/client.rb
|
|
41
|
+
- lib/mercado_livre_api/version.rb
|
|
42
|
+
- sig/mercado_livre_api.rbs
|
|
43
|
+
homepage: https://github.com/todasessascoisas/mercado_livre_api
|
|
44
|
+
licenses:
|
|
45
|
+
- MIT
|
|
46
|
+
metadata:
|
|
47
|
+
homepage_uri: https://github.com/todasessascoisas/mercado_livre_api
|
|
48
|
+
source_code_uri: https://github.com/todasessascoisas/mercado_livre_api
|
|
49
|
+
changelog_uri: https://github.com/todasessascoisas/mercado_livre_api
|
|
50
|
+
post_install_message:
|
|
51
|
+
rdoc_options: []
|
|
52
|
+
require_paths:
|
|
53
|
+
- lib
|
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
55
|
+
requirements:
|
|
56
|
+
- - ">="
|
|
57
|
+
- !ruby/object:Gem::Version
|
|
58
|
+
version: 3.2.0
|
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
|
+
requirements:
|
|
61
|
+
- - ">="
|
|
62
|
+
- !ruby/object:Gem::Version
|
|
63
|
+
version: '0'
|
|
64
|
+
requirements: []
|
|
65
|
+
rubygems_version: 3.5.3
|
|
66
|
+
signing_key:
|
|
67
|
+
specification_version: 4
|
|
68
|
+
summary: Ruby client for the Mercado Livre — OAuth authentication and endpoints for
|
|
69
|
+
orders, shipments, shipping labels and invoices.
|
|
70
|
+
test_files: []
|