meli 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/config.yml +5 -0
- data/lib/meli.rb +160 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 79d6ac914a73d63f023fab405af5d2f3dcef8254
|
4
|
+
data.tar.gz: 5848a00574ae843da02a6541b80c90683aad1e5c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 361a0ecec366e78afce72bb99ee60322d425201617d3642d4b0094eb5b4a5621982bd5d07b93180ffbdf654254745f127e386d22126749832d884de3e67f70b7
|
7
|
+
data.tar.gz: ea4970a9a3ccf4fd86bf86f6c2342dfe4088bcfd4afd84d31e1ee132fd4d0ecc33b102f902930c2ccd89faecbba346b1324092caa8b7c86f14a5b21fb94bc568
|
data/lib/config.yml
ADDED
data/lib/meli.rb
ADDED
@@ -0,0 +1,160 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'rubygems'
|
3
|
+
require 'net/http'
|
4
|
+
require 'net/https'
|
5
|
+
require 'json'
|
6
|
+
require 'uri'
|
7
|
+
require 'yaml'
|
8
|
+
|
9
|
+
class Meli
|
10
|
+
attr_accessor :access_token, :refresh_token
|
11
|
+
attr_reader :secret, :app_id, :https
|
12
|
+
|
13
|
+
config = YAML.load_file(File.expand_path(File.dirname(__FILE__) + "/config.yml"))
|
14
|
+
SDK_VERSION = config["config"]["sdk_version"]
|
15
|
+
API_ROOT_URL = config["config"]["api_root_url"]
|
16
|
+
AUTH_URL = config["config"]["auth_url"]
|
17
|
+
OAUTH_URL = config["config"]["oauth_url"]
|
18
|
+
|
19
|
+
#constructor
|
20
|
+
def initialize(app_id = nil, secret = nil, access_token = nil, refresh_token = nil)
|
21
|
+
@access_token = access_token
|
22
|
+
@refresh_token = refresh_token
|
23
|
+
@app_id = app_id
|
24
|
+
@secret = secret
|
25
|
+
api_url = URI.parse API_ROOT_URL
|
26
|
+
@https = Net::HTTP.new(api_url.host, api_url.port)
|
27
|
+
@https.use_ssl = true
|
28
|
+
@https.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
29
|
+
@https.ssl_version = :TLSv1
|
30
|
+
end
|
31
|
+
|
32
|
+
#AUTH METHODS
|
33
|
+
def auth_url(redirect_URI)
|
34
|
+
params = {:client_id => @app_id, :response_type => 'code', :redirect_uri => redirect_URI}
|
35
|
+
url = "#{AUTH_URL}?#{to_url_params(params)}"
|
36
|
+
end
|
37
|
+
|
38
|
+
def authorize(code, redirect_URI)
|
39
|
+
params = { :grant_type => 'authorization_code', :client_id => @app_id, :client_secret => @secret, :code => code, :redirect_uri => redirect_URI}
|
40
|
+
|
41
|
+
uri = make_path(OAUTH_URL, params)
|
42
|
+
|
43
|
+
req = Net::HTTP::Post.new(uri.path)
|
44
|
+
req['Accept'] = 'application/json'
|
45
|
+
req['User-Agent'] = SDK_VERSION
|
46
|
+
req['Content-Type'] = "application/x-www-form-urlencoded"
|
47
|
+
req.set_form_data(params)
|
48
|
+
response = @https.request(req)
|
49
|
+
|
50
|
+
case response
|
51
|
+
when Net::HTTPSuccess
|
52
|
+
response_info = JSON.parse response.body
|
53
|
+
#convert hash keys to symbol
|
54
|
+
response_info = Hash[response_info.map{ |k, v| [k.to_sym, v] }]
|
55
|
+
|
56
|
+
@access_token = response_info[:access_token]
|
57
|
+
if response_info.has_key?(:refresh_token)
|
58
|
+
@refresh_token = response_info[:refresh_token]
|
59
|
+
else
|
60
|
+
@refresh_token = '' # offline_access not set up
|
61
|
+
end
|
62
|
+
@access_token
|
63
|
+
else
|
64
|
+
# response code isn't a 200; raise an exception
|
65
|
+
response.error!
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
def get_refresh_token()
|
71
|
+
if !@refresh_token.nil? and !@refresh_token.empty?
|
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
|
99
|
+
end
|
100
|
+
|
101
|
+
|
102
|
+
#REQUEST METHODS
|
103
|
+
def execute(req)
|
104
|
+
req['Accept'] = 'application/json'
|
105
|
+
req['User-Agent'] = SDK_VERSION
|
106
|
+
req['Content-Type'] = 'application/json'
|
107
|
+
response = @https.request(req)
|
108
|
+
end
|
109
|
+
|
110
|
+
def get(path, params = {})
|
111
|
+
uri = make_path(path, params)
|
112
|
+
req = Net::HTTP::Get.new("#{uri.path}?#{uri.query}")
|
113
|
+
execute req
|
114
|
+
end
|
115
|
+
|
116
|
+
def post(path, body, params = {})
|
117
|
+
uri = make_path(path, params)
|
118
|
+
req = Net::HTTP::Post.new("#{uri.path}?#{uri.query}")
|
119
|
+
req.set_form_data(params)
|
120
|
+
req.body = body.to_json unless body.nil?
|
121
|
+
execute req
|
122
|
+
end
|
123
|
+
|
124
|
+
def put(path, body, params = {})
|
125
|
+
uri = make_path(path, params)
|
126
|
+
req = Net::HTTP::Put.new("#{uri.path}?#{uri.query}")
|
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
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: meli
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gastón Corrao
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-12 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Gem for accesing MercadoLibre API
|
14
|
+
email: gaston.corrao@mercadolibre.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/config.yml
|
20
|
+
- lib/meli.rb
|
21
|
+
homepage: http://developers.mercadolibre.com/ruby-sdk/
|
22
|
+
licenses:
|
23
|
+
- MIT
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 2.4.5.1
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: meli
|
45
|
+
test_files: []
|