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, Chargeback, Customer, DisbursementRefund, IdentificationType, Invoice, MerchantOrder, OAuth, Order, OrderTransaction, Payment, PaymentMethods, Point, Preapproval, PreapprovalPlan, Preference, Refund, Subscription, 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.
-
#_path_param(value) ⇒ Object
Encodes a dynamic URL path segment before interpolation.
-
#_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.
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/mercadopago/core/mp_base.rb', line 21 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.
58 59 60 61 62 63 64 |
# File 'lib/mercadopago/core/mp_base.rb', line 58 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.
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/mercadopago/core/mp_base.rb', line 41 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.
144 145 146 147 148 149 150 151 152 |
# File 'lib/mercadopago/core/mp_base.rb', line 144 def _delete(uri:, request_options: nil) = () headers = _check_headers() MPResponse.new( @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.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/mercadopago/core/mp_base.rb', line 78 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() MPResponse.new( @http_client.get(url: @config.api_base_url + uri, headers: headers, params: filters, timeout: .connection_timeout, maxretries: .max_retries, retry_on: .retry_on, initial_delay_ms: .initial_delay_ms, max_delay_ms: .max_delay_ms, jitter: .jitter, on_retry: .on_retry) ) end |
#_path_param(value) ⇒ Object
Encodes a dynamic URL path segment before interpolation.
67 68 69 |
# File 'lib/mercadopago/core/mp_base.rb', line 67 def _path_param(value) CGI.escape(value.to_s).gsub('+', '%20') end |
#_post(uri:, data:, request_options: nil) ⇒ Hash{Symbol => Object}
Performs a POST request against the MercadoPago API.
Serialises data to JSON before sending.
105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/mercadopago/core/mp_base.rb', line 105 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 MPResponse.new( @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.
127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/mercadopago/core/mp_base.rb', line 127 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 }) MPResponse.new( @http_client.put(url: @config.api_base_url + uri, data: data.to_json, headers: headers, timeout: .connection_timeout) ) end |