meli 1.0.2 → 1.0.3.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/lib/meli.rb +171 -148
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 85035bfc6784b27072cb4d430c34d1807f358cfa
|
4
|
+
data.tar.gz: 963f24c0dba2828c226a1e180537f2db9d180c76
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 39f96436bfb2d8a404b1163fc80ac64c59c0216569f3f5692146f5255f84c72f5f88fea908bb4126f8f5e29c91b8b7ddd7c1c58a7d8cce10ae99378be3a5d4ea
|
7
|
+
data.tar.gz: 11868c7fa36b2ca2d59750e49a01f3a514843564bc18b8650beea34cf255d54dabea1efc4941514775b1ac75217dfbb8a201de96291769d7c164b9543b206958
|
data/lib/meli.rb
CHANGED
@@ -1,160 +1,183 @@
|
|
1
1
|
# encoding: UTF-8
|
2
|
+
|
2
3
|
require 'rubygems'
|
4
|
+
require 'bundler/setup'
|
3
5
|
require 'net/http'
|
4
6
|
require 'net/https'
|
5
7
|
require 'json'
|
6
8
|
require 'uri'
|
7
9
|
require 'yaml'
|
8
10
|
|
11
|
+
# This class is the actual library
|
9
12
|
class Meli
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
13
|
+
attr_accessor :access_token, :refresh_token
|
14
|
+
attr_reader :secret, :app_id, :https
|
15
|
+
|
16
|
+
config = YAML.load_file(
|
17
|
+
File.expand_path(File.dirname(__FILE__) + '/config.yml')
|
18
|
+
)
|
19
|
+
SDK_VERSION = config['config']['sdk_version']
|
20
|
+
API_ROOT_URL = config['config']['api_root_url']
|
21
|
+
AUTH_URL = config['config']['auth_url']
|
22
|
+
OAUTH_URL = config['config']['oauth_url']
|
23
|
+
|
24
|
+
# constructor
|
25
|
+
def initialize(
|
26
|
+
app_id = nil,
|
27
|
+
secret = nil,
|
28
|
+
access_token = nil,
|
29
|
+
refresh_token = nil
|
30
|
+
)
|
31
|
+
@access_token = access_token
|
32
|
+
@refresh_token = refresh_token
|
33
|
+
@app_id = app_id
|
34
|
+
@secret = secret
|
35
|
+
api_url = URI.parse API_ROOT_URL
|
36
|
+
@https = Net::HTTP.new(api_url.host, api_url.port)
|
37
|
+
@https.use_ssl = true
|
38
|
+
@https.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
39
|
+
@https.ssl_version = :TLSv1
|
40
|
+
end
|
41
|
+
|
42
|
+
# AUTH METHODS
|
43
|
+
def auth_url(redirect_uri)
|
44
|
+
params = {
|
45
|
+
client_id: @app_id,
|
46
|
+
response_type: 'code',
|
47
|
+
redirect_uri: redirect_uri
|
48
|
+
}
|
49
|
+
|
50
|
+
url = "#{AUTH_URL}?#{to_url_params(params)}"
|
51
|
+
end
|
52
|
+
|
53
|
+
def authorize(code, redirect_uri)
|
54
|
+
params = {
|
55
|
+
grant_type: 'authorization_code',
|
56
|
+
client_id: @app_id,
|
57
|
+
client_secret: @secret,
|
58
|
+
code: code,
|
59
|
+
redirect_uri: redirect_uri
|
60
|
+
}
|
61
|
+
|
62
|
+
uri = make_path(OAUTH_URL, params)
|
63
|
+
|
64
|
+
req = Net::HTTP::Post.new(uri.path)
|
65
|
+
req['Accept'] = 'application/json'
|
66
|
+
req['User-Agent'] = SDK_VERSION
|
67
|
+
req['Content-Type'] = 'application/x-www-form-urlencoded'
|
68
|
+
req.set_form_data(params)
|
69
|
+
response = @https.request(req)
|
70
|
+
|
71
|
+
case response
|
72
|
+
when Net::HTTPSuccess
|
73
|
+
response_info = JSON.parse response.body
|
74
|
+
# convert hash keys to symbol
|
75
|
+
response_info = Hash[response_info.map { |k, v| [k.to_sym, v] }]
|
76
|
+
|
77
|
+
@access_token = response_info[:access_token]
|
78
|
+
@refresh_token = if response_info.key?(:refresh_token)
|
79
|
+
response_info[:refresh_token]
|
80
|
+
else
|
81
|
+
'' # offline_access not set up
|
82
|
+
end
|
83
|
+
@access_token
|
84
|
+
else
|
85
|
+
# response code isn't a 200; raise an exception
|
86
|
+
response.error!
|
36
87
|
end
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
params = {:grant_type => 'refresh_token', :client_id => @app_id, :client_secret => @secret, :refresh_token => @refresh_token}
|
73
|
-
|
74
|
-
uri = make_path(OAUTH_URL, params)
|
75
|
-
|
76
|
-
req = Net::HTTP::Post.new(uri.path)
|
77
|
-
req['Accept'] = 'application/json'
|
78
|
-
req['User-Agent'] = SDK_VERSION
|
79
|
-
req['Content-Type'] = "application/x-www-form-urlencoded"
|
80
|
-
req.set_form_data(params)
|
81
|
-
response = @https.request(req)
|
82
|
-
|
83
|
-
case response
|
84
|
-
when Net::HTTPSuccess
|
85
|
-
response_info = JSON.parse response.body
|
86
|
-
|
87
|
-
#convert hash keys to symbol
|
88
|
-
response_info = Hash[response_info.map{ |k, v| [k.to_sym, v] }]
|
89
|
-
|
90
|
-
@access_token = response_info[:access_token]
|
91
|
-
@refresh_token = response_info[:refresh_token]
|
92
|
-
else
|
93
|
-
# response code isn't a 200; raise an exception
|
94
|
-
response.error!
|
95
|
-
end
|
96
|
-
else
|
97
|
-
raise "Offline-Access is not allowed."
|
98
|
-
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def refresh_token
|
91
|
+
if !@refresh_token.nil? && !@refresh_token.empty?
|
92
|
+
params = {
|
93
|
+
grant_type: 'refresh_token',
|
94
|
+
client_id: @app_id,
|
95
|
+
client_secret: @secret,
|
96
|
+
refresh_token: @refresh_token
|
97
|
+
}
|
98
|
+
|
99
|
+
uri = make_path(OAUTH_URL, params)
|
100
|
+
|
101
|
+
req = Net::HTTP::Post.new(uri.path)
|
102
|
+
req['Accept'] = 'application/json'
|
103
|
+
req['User-Agent'] = SDK_VERSION
|
104
|
+
req['Content-Type'] = 'application/x-www-form-urlencoded'
|
105
|
+
req.set_form_data(params)
|
106
|
+
response = @https.request(req)
|
107
|
+
|
108
|
+
case response
|
109
|
+
when Net::HTTPSuccess
|
110
|
+
response_info = JSON.parse response.body
|
111
|
+
|
112
|
+
# convert hash keys to symbol
|
113
|
+
response_info = Hash[response_info.map { |k, v| [k.to_sym, v] }]
|
114
|
+
|
115
|
+
@access_token = response_info[:access_token]
|
116
|
+
@refresh_token = response_info[:refresh_token]
|
117
|
+
else
|
118
|
+
# response code isn't a 200; raise an exception
|
119
|
+
response.error!
|
120
|
+
end
|
121
|
+
else
|
122
|
+
raise 'Offline-Access is not allowed.'
|
99
123
|
end
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
124
|
+
end
|
125
|
+
|
126
|
+
# REQUEST METHODS
|
127
|
+
def execute(req)
|
128
|
+
req['Accept'] = 'application/json'
|
129
|
+
req['User-Agent'] = SDK_VERSION
|
130
|
+
req['Content-Type'] = 'application/json'
|
131
|
+
response = @https.request(req)
|
132
|
+
end
|
133
|
+
|
134
|
+
def get(path, params = {})
|
135
|
+
uri = make_path(path, params)
|
136
|
+
req = Net::HTTP::Get.new("#{uri.path}?#{uri.query}")
|
137
|
+
execute req
|
138
|
+
end
|
139
|
+
|
140
|
+
def post(path, body, params = {})
|
141
|
+
uri = make_path(path, params)
|
142
|
+
req = Net::HTTP::Post.new("#{uri.path}?#{uri.query}")
|
143
|
+
req.set_form_data(params)
|
144
|
+
req.body = body.to_json unless body.nil?
|
145
|
+
execute req
|
146
|
+
end
|
147
|
+
|
148
|
+
def put(path, body, params = {})
|
149
|
+
uri = make_path(path, params)
|
150
|
+
req = Net::HTTP::Put.new("#{uri.path}?#{uri.query}")
|
151
|
+
req.set_form_data(params)
|
152
|
+
req.body = body.to_json unless body.nil?
|
153
|
+
execute req
|
154
|
+
end
|
155
|
+
|
156
|
+
def delete(path, params = {})
|
157
|
+
uri = make_path(path, params)
|
158
|
+
req = Net::HTTP::Delete.new("#{uri.path}?#{uri.query}")
|
159
|
+
execute req
|
160
|
+
end
|
161
|
+
|
162
|
+
def options(path, params = {})
|
163
|
+
uri = make_path(path, params)
|
164
|
+
req = Net::HTTP::Options.new("#{uri.path}?#{uri.query}")
|
165
|
+
execute req
|
166
|
+
end
|
167
|
+
|
168
|
+
private
|
169
|
+
|
170
|
+
def to_url_params(params)
|
171
|
+
URI.escape(params.collect { |k, v| "#{k}=#{v}" }.join('&'))
|
172
|
+
end
|
173
|
+
|
174
|
+
def make_path(path, params = {})
|
175
|
+
# Making Path and add a leading / if not exist
|
176
|
+
unless path =~ /^http/
|
177
|
+
path = "/#{path}" unless path =~ %r{ /^\// }
|
178
|
+
path = "#{API_ROOT_URL}#{path}"
|
122
179
|
end
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
req.set_form_data(params)
|
128
|
-
req.body = body.to_json unless body.nil?
|
129
|
-
execute req
|
130
|
-
end
|
131
|
-
|
132
|
-
def delete(path, params = {})
|
133
|
-
uri = make_path(path, params)
|
134
|
-
req = Net::HTTP::Delete.new("#{uri.path}?#{uri.query}")
|
135
|
-
execute req
|
136
|
-
end
|
137
|
-
|
138
|
-
def options(path, params = {})
|
139
|
-
uri = make_path(path, params)
|
140
|
-
req = Net::HTTP::Options.new("#{uri.path}?#{uri.query}")
|
141
|
-
execute req
|
142
|
-
end
|
143
|
-
|
144
|
-
private
|
145
|
-
def to_url_params(params)
|
146
|
-
URI.escape(params.collect{|k,v| "#{k}=#{v}"}.join('&'))
|
147
|
-
end
|
148
|
-
|
149
|
-
def make_path(path, params = {})
|
150
|
-
# Making Path and add a leading / if not exist
|
151
|
-
unless path =~ /^http/
|
152
|
-
path = "/#{path}" unless path =~ /^\//
|
153
|
-
path = "#{API_ROOT_URL}#{path}"
|
154
|
-
end
|
155
|
-
path = "#{path}?#{to_url_params(params)}" if params.keys.size > 0
|
156
|
-
uri = URI.parse path
|
157
|
-
end
|
158
|
-
|
159
|
-
|
160
|
-
end #class
|
180
|
+
path = "#{path}?#{to_url_params(params)}" unless params.keys.empty?
|
181
|
+
uri = URI.parse path
|
182
|
+
end
|
183
|
+
end
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: meli
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- Alfredo Bejarano
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-04-11 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Gem for accesing MercadoLibre API
|
14
|
-
email:
|
14
|
+
email: alfredo.corona@mercadolibre.com.mx
|
15
15
|
executables: []
|
16
16
|
extensions: []
|
17
17
|
extra_rdoc_files: []
|
@@ -20,7 +20,7 @@ files:
|
|
20
20
|
- lib/meli.rb
|
21
21
|
homepage: http://developers.mercadolibre.com/ruby-sdk/
|
22
22
|
licenses:
|
23
|
-
-
|
23
|
+
- MIT
|
24
24
|
metadata: {}
|
25
25
|
post_install_message:
|
26
26
|
rdoc_options: []
|
@@ -28,7 +28,7 @@ require_paths:
|
|
28
28
|
- lib
|
29
29
|
required_ruby_version: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '2.2'
|
34
34
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
@@ -38,7 +38,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
38
38
|
version: '0'
|
39
39
|
requirements: []
|
40
40
|
rubyforge_project:
|
41
|
-
rubygems_version: 2.
|
41
|
+
rubygems_version: 2.6.2
|
42
42
|
signing_key:
|
43
43
|
specification_version: 4
|
44
44
|
summary: meli
|