Class: Mercadopago::MPBase Abstract
- Inherits:
-
Object
- Object
- Mercadopago::MPBase
- Defined in:
- lib/mercadopago/core/mp_base.rb
Overview
Subclass and use the _get/_post/_put/_delete helpers.
Abstract base class for every API resource (Payment, Customer, Order, etc.).
Provides internal HTTP helper methods (_get, _post, _put, _delete) that prepend the API base URL, inject authentication headers, serialise payloads to JSON, and delegate to HttpClient.
Subclasses only need to call these helpers with the appropriate URI and data; they should never interact with HttpClient directly.
Direct Known Subclasses
AdvancedPayment, Card, CardToken, Customer, DisbursementRefund, IdentificationType, MerchantOrder, Order, OrderTransaction, Payment, PaymentMethods, Preapproval, PreapprovalPlan, Preference, Refund, User
Instance Method Summary collapse
-
#_check_headers(request_options = nil, extra_headers = nil) ⇒ Hash
Builds the HTTP headers hash, merging any extra headers on top.
-
#_check_request_options(request_options = nil) ⇒ RequestOptions
Validates and resolves the request options for a single call.
-
#_delete(uri:, request_options: nil) ⇒ Hash{Symbol => Object}
Performs a DELETE request against the MercadoPago API.
-
#_get(uri:, filters: nil, request_options: nil) ⇒ Hash{Symbol => Object}
Performs a GET request against the MercadoPago API.
-
#_post(uri:, data:, request_options: nil) ⇒ Hash{Symbol => Object}
Performs a POST request against the MercadoPago API.
-
#_put(uri:, data:, request_options: nil) ⇒ Hash{Symbol => Object}
Performs a PUT request against the MercadoPago API.
-
#initialize(request_options, http_client) ⇒ MPBase
constructor
A new instance of MPBase.
Constructor Details
#initialize(request_options, http_client) ⇒ MPBase
Returns a new instance of MPBase.
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/mercadopago/core/mp_base.rb', line 19 def initialize(, http_client) unless .is_a?(RequestOptions) raise TypeError, 'Param request_options must be a RequestOptions object' end @request_options = @http_client = http_client @config = Config.new end |
Instance Method Details
#_check_headers(request_options = nil, extra_headers = nil) ⇒ Hash
Builds the HTTP headers hash, merging any extra headers on top.
56 57 58 59 60 61 62 |
# File 'lib/mercadopago/core/mp_base.rb', line 56 def _check_headers( = nil, extra_headers = nil) headers = .nil? ? @request_options.get_headers : .get_headers headers.merge!(extra_headers) unless extra_headers.nil? headers end |
#_check_request_options(request_options = nil) ⇒ RequestOptions
Validates and resolves the request options for a single call.
Falls back to the instance-level options when none are provided, and inherits the access token from the instance options when the override does not include one.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/mercadopago/core/mp_base.rb', line 39 def ( = nil) = @request_options if .nil? unless .is_a?(RequestOptions) raise TypeError, 'Param request_options must be a RequestOptions object' end .access_token = @request_options.access_token if .access_token.nil? end |
#_delete(uri:, request_options: nil) ⇒ Hash{Symbol => Object}
Performs a DELETE request against the MercadoPago API.
124 125 126 127 128 129 130 |
# File 'lib/mercadopago/core/mp_base.rb', line 124 def _delete(uri:, request_options: nil) = () headers = _check_headers() @http_client.delete(url: @config.api_base_url + uri, headers: headers, timeout: .connection_timeout) end |
#_get(uri:, filters: nil, request_options: nil) ⇒ Hash{Symbol => Object}
Performs a GET request against the MercadoPago API.
71 72 73 74 75 76 77 78 79 |
# File 'lib/mercadopago/core/mp_base.rb', line 71 def _get(uri:, filters: nil, request_options: nil) raise TypeError, 'Filters must be a Hash' unless filters.nil? || filters.is_a?(Hash) = () headers = _check_headers() @http_client.get(url: @config.api_base_url + uri, headers: headers, params: filters, timeout: .connection_timeout, maxretries: .max_retries) end |
#_post(uri:, data:, request_options: nil) ⇒ Hash{Symbol => Object}
Performs a POST request against the MercadoPago API.
Serialises data to JSON before sending.
90 91 92 93 94 95 96 97 98 |
# File 'lib/mercadopago/core/mp_base.rb', line 90 def _post(uri:, data:, request_options: nil) raise TypeError, 'Data must be a Hash' unless data.nil? || data.is_a?(Hash) = () headers = _check_headers(, { 'Content-Type': @config.mime_json }) payload = data&.to_json @http_client.post(url: @config.api_base_url + uri, data: payload, headers: headers, timeout: .connection_timeout) end |
#_put(uri:, data:, request_options: nil) ⇒ Hash{Symbol => Object}
Performs a PUT request against the MercadoPago API.
Serialises data to JSON before sending.
109 110 111 112 113 114 115 116 117 |
# File 'lib/mercadopago/core/mp_base.rb', line 109 def _put(uri:, data:, request_options: nil) raise TypeError, 'Data must be a Hash' unless data.nil? || data.is_a?(Hash) = () headers = _check_headers(, { 'Content-Type': @config.mime_json }) @http_client.put(url: @config.api_base_url + uri, data: data.to_json, headers: headers, timeout: .connection_timeout) end |