class Mercadolibre::Api

Attributes

access_token[RW]
site[RW]

Public Class Methods

new(args={}) click to toggle source
# File lib/mercadolibre/api.rb, line 5
def initialize(args={})
  @app_key = args[:app_key]
  @app_secret = args[:app_secret]
  @callback_url = args[:callback_url]
  @access_token = args[:access_token]

  if args[:endpoint_url].present?
    @endpoint_url = args[:endpoint_url]
  else
    @endpoint_url = 'https://api.mercadolibre.com'
  end

  if args[:auth_url].present?
    @auth_url = args[:auth_url]
  else
    @auth_url = 'https://auth.mercadolibre.com.ar'
  end

  @site = args[:site]
end

Public Instance Methods

get_last_response() click to toggle source
# File lib/mercadolibre/api.rb, line 52
def get_last_response
  @last_response
end
get_last_result() click to toggle source
# File lib/mercadolibre/api.rb, line 56
def get_last_result
  @last_result
end

Private Instance Methods

delete_request(action, params={}, headers={}) click to toggle source
# File lib/mercadolibre/api.rb, line 122
def delete_request(action, params={}, headers={})
  begin
    api_response_kind = headers.delete('api_response_kind')
    api_response_kind = headers.delete(:api_response_kind) if api_response_kind.nil?
    api_response_kind = 'object' if api_response_kind.nil?

    parse_response(api_response_kind, RestClient.delete("#{@endpoint_url}#{action}", params))
  rescue => e
    parse_response('object', e.response)
  end
end
get_request(action, params={}, headers={}) click to toggle source
# File lib/mercadolibre/api.rb, line 62
def get_request(action, params={}, headers={})
  begin
    api_response_kind = headers.delete('api_response_kind')
    api_response_kind = headers.delete(:api_response_kind) if api_response_kind.nil?
    api_response_kind = 'object' if api_response_kind.nil?

    parse_response(api_response_kind, RestClient.get("#{@endpoint_url}#{action}", {params: params}.merge(headers)))
  rescue => e
    parse_response('object', e.response)
  end
end
head_request(action, params={}, headers={}) click to toggle source
# File lib/mercadolibre/api.rb, line 110
def head_request(action, params={}, headers={})
  begin
    api_response_kind = headers.delete('api_response_kind')
    api_response_kind = headers.delete(:api_response_kind) if api_response_kind.nil?
    api_response_kind = 'object' if api_response_kind.nil?

    parse_response(api_response_kind, RestClient.head("#{@endpoint_url}#{action}", params))
  rescue => e
    parse_response('object', e.response)
  end
end
parse_response(api_response_kind, response) click to toggle source
# File lib/mercadolibre/api.rb, line 134
def parse_response(api_response_kind, response)
  @last_response = response

  result = OpenStruct.new
  result.status_code = response.code

  if api_response_kind == 'object'
    result.headers = (JSON.parse(response.headers.to_json, object_class: OpenStruct) rescue response.headers)
    result.body = (JSON.parse(response.body, object_class: OpenStruct) rescue response.body)
  elsif api_response_kind == 'hash'
    result.headers = response.headers
    result.body = (JSON.parse(response.body) rescue response.body)
  else
    result.headers = response.headers
    result.body = response.body
  end

  @last_result = result

  result
end
patch_request(action, params={}, headers={}) click to toggle source
# File lib/mercadolibre/api.rb, line 98
def patch_request(action, params={}, headers={})
  begin
    api_response_kind = headers.delete('api_response_kind')
    api_response_kind = headers.delete(:api_response_kind) if api_response_kind.nil?
    api_response_kind = 'object' if api_response_kind.nil?

    parse_response(api_response_kind, RestClient.patch("#{@endpoint_url}#{action}", params, headers))
  rescue => e
    parse_response('object', e.response)
  end
end
post_request(action, params={}, headers={}) click to toggle source
# File lib/mercadolibre/api.rb, line 74
def post_request(action, params={}, headers={})
  begin
    api_response_kind = headers.delete('api_response_kind')
    api_response_kind = headers.delete(:api_response_kind) if api_response_kind.nil?
    api_response_kind = 'object' if api_response_kind.nil?

    parse_response(api_response_kind, RestClient.post("#{@endpoint_url}#{action}", params, headers))
  rescue => e
    parse_response('object', e.response)
  end
end
put_request(action, params={}, headers={}) click to toggle source
# File lib/mercadolibre/api.rb, line 86
def put_request(action, params={}, headers={})
  begin
    api_response_kind = headers.delete('api_response_kind')
    api_response_kind = headers.delete(:api_response_kind) if api_response_kind.nil?
    api_response_kind = 'object' if api_response_kind.nil?

    parse_response(api_response_kind, RestClient.put("#{@endpoint_url}#{action}", params, headers))
  rescue => e
    parse_response('object', e.response)
  end
end