Class: MercadoLibre::API
- Inherits:
-
Object
- Object
- MercadoLibre::API
- Defined in:
- lib/mercado_libre/api.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
Instance Method Summary collapse
- #authenticated_request(verb, url, data = {}) ⇒ Object
- #close_item(item_id) ⇒ Object
- #delete_item(item_id) ⇒ Object
- #get_item(item_id) ⇒ Object
-
#initialize(params) ⇒ API
constructor
A new instance of API.
- #pause_item(item_id) ⇒ Object
- #publish_item(item) ⇒ Object
- #relist_item(item_id, params) ⇒ Object
- #request(verb, url, data, authenticated: false) ⇒ Object
- #request_access_token ⇒ Object
- #unpause_item(item_id) ⇒ Object
- #update_item(item_id, params) ⇒ Object
- #url_with_token(url) ⇒ Object
Constructor Details
#initialize(params) ⇒ API
Returns a new instance of API.
7 8 9 10 11 12 |
# File 'lib/mercado_libre/api.rb', line 7 def initialize(params) @app_key = params.fetch(:app_key) @app_secret = params.fetch(:app_secret) @host = params.fetch(:host) { ENV.fetch('MERCADOLIBRE_API_HOST') } @retries_num = Integer( params.fetch(:retries) { ENV.fetch('MERCADOLIBRE_HTTP_RETRIES') { 10 } } ) end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
5 6 7 |
# File 'lib/mercado_libre/api.rb', line 5 def access_token @access_token end |
Instance Method Details
#authenticated_request(verb, url, data = {}) ⇒ Object
67 68 69 |
# File 'lib/mercado_libre/api.rb', line 67 def authenticated_request(verb, url, data = {}) request(verb, url, data, authenticated: true) end |
#close_item(item_id) ⇒ Object
38 39 40 41 |
# File 'lib/mercado_libre/api.rb', line 38 def close_item(item_id) url = "/items/#{item_id}" authenticated_request(:put, url, {status: :closed}.to_json) end |
#delete_item(item_id) ⇒ Object
43 44 45 46 |
# File 'lib/mercado_libre/api.rb', line 43 def delete_item(item_id) url = "/items/#{item_id}" authenticated_request(:put, url, {deleted: true}.to_json) end |
#get_item(item_id) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/mercado_libre/api.rb', line 60 def get_item(item_id) url = "/items/#{item_id}" response = authenticated_request(:get, url) Item.new(response) end |
#pause_item(item_id) ⇒ Object
28 29 30 31 |
# File 'lib/mercado_libre/api.rb', line 28 def pause_item(item_id) url = "/items/#{item_id}" authenticated_request(:put, url, {status: :paused}.to_json) end |
#publish_item(item) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/mercado_libre/api.rb', line 14 def publish_item(item) url = "/items" response = authenticated_request(:post, url, item.publishable_hash.to_json) Item.new(response) end |
#relist_item(item_id, params) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/mercado_libre/api.rb', line 48 def relist_item(item_id, params) url = "/items/#{item_id}/relist" payload = { price: params.fetch(:price), quantity: params.fetch(:quantity), listing_type_id: params.fetch(:listing_type_id) } response = authenticated_request(:post, url, payload.to_json) Item.new response end |
#request(verb, url, data, authenticated: false) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/mercado_libre/api.rb', line 71 def request(verb, url, data, authenticated: false) full_url = "#{@host}#{url}" full_url = url_with_token(full_url) if authenticated verb = verb.to_s.upcase tries ||= @retries_num response = Requests.request(verb, full_url, data: data) JSON.parse(response.body) rescue *HTTPErrors retry unless (tries -=1).zero? raise rescue Requests::Error @access_token = nil if $!..strip == 'Forbidden' # Release access token retry unless (tries -=1).zero? raise $!, [$!..strip,$!.response.body].join("\n"), $!.backtrace #Verbose errors end |
#request_access_token ⇒ Object
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/mercado_libre/api.rb', line 93 def request_access_token url = "/oauth/token" data = { client_id: @app_key, client_secret: @app_secret, grant_type: 'client_credentials' } request(:post, url, data).fetch('access_token') end |
#unpause_item(item_id) ⇒ Object
33 34 35 36 |
# File 'lib/mercado_libre/api.rb', line 33 def unpause_item(item_id) url = "/items/#{item_id}" authenticated_request(:put, url, {status: :active}.to_json) end |
#update_item(item_id, params) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/mercado_libre/api.rb', line 21 def update_item(item_id, params) url = "/items/#{item_id}" response = authenticated_request(:put, url, params.to_json) Item.new(response) end |
#url_with_token(url) ⇒ Object
104 105 106 |
# File 'lib/mercado_libre/api.rb', line 104 def url_with_token(url) "#{url}#{url.include?('?') ? '&' : '?'}access_token=#{access_token}" end |