Module: MercadoPago::Request
- Defined in:
- lib/mercadopago/request.rb
Defined Under Namespace
Classes: ClientError
Constant Summary collapse
- MERCADOPAGO_URL =
This URL is the base for all API calls.
'https://api.mercadolibre.com'
Class Method Summary collapse
-
.make_request(type, path, payload = nil, headers = {}) ⇒ Object
Makes a HTTP request to a MercadoPago API.
-
.wrap_get(path, headers = {}) ⇒ Object
Makes a GET request to a MercaPago API.
-
.wrap_post(path, payload, headers = {}) ⇒ Object
Makes a POST request to a MercaPago API.
Class Method Details
.make_request(type, path, payload = nil, headers = {}) ⇒ Object
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.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/mercadopago/request.rb', line 46 def self.make_request(type, path, payload = nil, headers = {}) args = [type, MERCADOPAGO_URL, path, payload, headers].compact connection = Faraday.new(MERCADOPAGO_URL, ssl: { version: :SSLv3 }) 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 = {}) ⇒ Object
Makes a GET request to a MercaPago 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.
34 35 36 |
# File 'lib/mercadopago/request.rb', line 34 def self.wrap_get(path, headers = {}) make_request(:get, path, nil, headers) end |
.wrap_post(path, payload, headers = {}) ⇒ Object
Makes a POST request to a MercaPago 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.
23 24 25 26 |
# File 'lib/mercadopago/request.rb', line 23 def self.wrap_post(path, payload, headers = {}) raise ClientError('No data given') if payload.nil? or payload.empty? make_request(:post, path, payload, headers) end |