module MercadoPago::Request

Constants

CONTENT_HEADERS
MERCADOPAGO_URL

This URL is the base for all API calls.

Public Class Methods

make_request(type, path, payload = nil, headers = {}) click to toggle source

Makes a HTTP request to a MercadoPago API.

  • type: the HTTP request type (:get, :post, :put, :delete).

  • path: the path of the API to be called.

  • payload: the data to be trasmitted to the API.

  • headers: the headers to be transmitted over the HTTP request.

# File lib/mercadopago/request.rb, line 65
def self.make_request(type, path, payload = nil, headers = {})
  args = [type, MERCADOPAGO_URL, path, payload, headers].compact

  connection = Faraday.new(MERCADOPAGO_URL, ssl: { version: :TLSv1_2 })

  response = connection.send(type) do |req|
    req.url path
    req.headers = headers
    req.body = payload
  end

  JSON.load(response.body)
rescue Exception => e
  if e.respond_to?(:response)
    JSON.load(e.response)
  else
    raise e
  end
end
wrap_get(path, headers = nil) click to toggle source

Makes a GET request to a MercadoPago API.

  • path: the path of the API to be called, including any query string parameters.

  • headers: the headers to be transmitted over the HTTP request.

# File lib/mercadopago/request.rb, line 52
def self.wrap_get(path, headers = nil)
  headers ||= CONTENT_HEADERS
  make_request(:get, path, nil, headers)
end
wrap_post(path, payload, headers = nil) click to toggle source

Makes a POST request to a MercadoPago API.

  • path: the path of the API to be called.

  • payload: the data to be trasmitted to the API.

  • headers: the headers to be transmitted over the HTTP request.

# File lib/mercadopago/request.rb, line 28
def self.wrap_post(path, payload, headers = nil)
  raise ClientError('No data given') if payload.nil? or payload.empty?
  headers ||= CONTENT_HEADERS
  make_request(:post, path, payload, headers)
end
wrap_put(path, payload, headers = {}) click to toggle source

Makes a PUT request to a MercadoPago API.

  • path: the path of the API to be called.

  • payload: the data to be trasmitted to the API.

  • headers: the headers to be transmitted over the HTTP request.

# File lib/mercadopago/request.rb, line 41
def self.wrap_put(path, payload, headers = {})
  raise ClientError('No data given') if payload.nil? or payload.empty?
  make_request(:put, path, payload, headers)
end