class Mercadopago::RestClient

Constants

API_BASE_URL
MERCADO_PAGO_VERSION
MIME_FORM
MIME_JSON

Attributes

access_token[R]
api_base_uri[RW]
client_secret[R]
http[RW]
public_key[R]

Public Class Methods

new(opts = {}) click to toggle source
# File lib/mercadopago/rest_client.rb, line 17
def initialize(opts = {})
  @access_token = opts[:access_token] || Config.access_token
  @public_key = opts[:public_key] || Config.public_key
  @http = set_http
end

Public Instance Methods

delete(uri, params = {}, content_type = MIME_JSON) click to toggle source
# File lib/mercadopago/rest_client.rb, line 42
def delete(uri, params = {}, content_type = MIME_JSON)
  request(Net::HTTP::Delete::METHOD, uri, params, content_type)
end
get(uri, params = {}, content_type = MIME_JSON) click to toggle source
# File lib/mercadopago/rest_client.rb, line 30
def get(uri, params = {}, content_type = MIME_JSON)
  request(Net::HTTP::Get::METHOD, uri, params, content_type)
end
headers(content_type) click to toggle source
# File lib/mercadopago/rest_client.rb, line 55
def headers(content_type)
  {
    'User-Agent' => "MercadoPago Ruby SDK v" + MERCADO_PAGO_VERSION,
    'Content-type' => content_type,
    'Accept' => MIME_JSON
  }
end
post(uri, data, params = {}, content_type = MIME_JSON) click to toggle source
# File lib/mercadopago/rest_client.rb, line 34
def post(uri, data, params = {}, content_type = MIME_JSON)
  request(Net::HTTP::Post::METHOD, uri, params, content_type, data)
end
put(uri, data = nil, params = {}, content_type = MIME_JSON) click to toggle source
# File lib/mercadopago/rest_client.rb, line 38
def put(uri, data = nil, params = {}, content_type = MIME_JSON)
  request(Net::HTTP::Put::METHOD, uri, params, content_type, data)
end
request(method, uri, params, content_type, data = {}) click to toggle source
# File lib/mercadopago/rest_client.rb, line 50
def request(method, uri, params, content_type, data = {})
  api_result = http.send_request(method, request_uri(uri, params), data.to_json, headers(content_type))
  Response.new(api_result.code, JSON.parse(api_result.body))
end
request_uri(path, params = {}) click to toggle source
# File lib/mercadopago/rest_client.rb, line 23
def request_uri(path, params = {})
  params['access_token'] = access_token
  uri = api_base_uri + path
  uri.query = [uri.query, URI.encode_www_form(params)].compact.join('&')
  uri.request_uri
end

Private Instance Methods

set_http() click to toggle source
# File lib/mercadopago/rest_client.rb, line 64
def set_http
  http = Net::HTTP.new(api_base_uri.host, api_base_uri.port)
  if api_base_uri.scheme == "https"
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER
    # TODO: review this option
    # @http.ssl_version = OpenSSL::SSL::OP_NO_SSLv3
  end
  http
end