Class: Mercadopago::MPBase Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/mercadopago/core/mp_base.rb

Overview

This class is abstract.

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.

Instance Method Summary collapse

Constructor Details

#initialize(request_options, http_client) ⇒ MPBase

Returns a new instance of MPBase.

Parameters:

  • request_options (RequestOptions)

    authentication and request configuration

  • http_client (HttpClient)

    transport layer for HTTP calls

Raises:



21
22
23
24
25
26
27
28
29
30
# File 'lib/mercadopago/core/mp_base.rb', line 21

def initialize(request_options, http_client)
  unless request_options.is_a?(RequestOptions)
    raise TypeError,
          'Param request_options must be a RequestOptions object'
  end

  @request_options = 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.

Parameters:

  • request_options (RequestOptions, nil) (defaults to: nil)

    source of base headers

  • extra_headers (Hash, nil) (defaults to: nil)

    additional headers (e.g. Content-Type)

Returns:

  • (Hash)

    merged headers ready for the HTTP call



58
59
60
61
62
63
64
# File 'lib/mercadopago/core/mp_base.rb', line 58

def _check_headers(request_options = nil, extra_headers = nil)
  headers = request_options.nil? ? @request_options.get_headers : request_options.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.

Parameters:

  • request_options (RequestOptions, nil) (defaults to: nil)

    per-call override

Returns:

Raises:



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mercadopago/core/mp_base.rb', line 41

def _check_request_options(request_options = nil)
  request_options = @request_options if request_options.nil?
  unless request_options.is_a?(RequestOptions)
    raise TypeError,
          'Param request_options must be a RequestOptions object'
  end

  request_options.access_token = @request_options.access_token if request_options.access_token.nil?

  request_options
end

#_delete(uri:, request_options: nil) ⇒ Hash{Symbol => Object}

Performs a DELETE request against the MercadoPago API.

Parameters:

  • uri (String)

    API path (e.g. "/v1/customers/123")

  • request_options (RequestOptions, nil) (defaults to: nil)

    per-call configuration override

Returns:

  • (Hash{Symbol => Object})

    :status and :response (response may be nil)



144
145
146
147
148
149
150
151
152
# File 'lib/mercadopago/core/mp_base.rb', line 144

def _delete(uri:, request_options: nil)
  request_options = _check_request_options(request_options)
  headers = _check_headers(request_options)

  MPResponse.new(
    @http_client.delete(url: @config.api_base_url + uri, headers: headers,
                        timeout: request_options.connection_timeout)
  )
end

#_get(uri:, filters: nil, request_options: nil) ⇒ Hash{Symbol => Object}

Performs a GET request against the MercadoPago API.

Parameters:

  • uri (String)

    API path (e.g. "/v1/payments/123")

  • filters (Hash, nil) (defaults to: nil)

    query-string parameters

  • request_options (RequestOptions, nil) (defaults to: nil)

    per-call configuration override

Returns:

  • (Hash{Symbol => Object})

    :status and :response

Raises:

  • (TypeError)

    if filters is not a Hash



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)

  request_options = _check_request_options(request_options)
  headers = _check_headers(request_options)

  MPResponse.new(
    @http_client.get(url: @config.api_base_url + uri, headers: headers, params: filters,
                     timeout: request_options.connection_timeout,
                     maxretries: request_options.max_retries,
                     retry_on: request_options.retry_on,
                     initial_delay_ms: request_options.initial_delay_ms,
                     max_delay_ms: request_options.max_delay_ms,
                     jitter: request_options.jitter,
                     on_retry: request_options.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.

Parameters:

  • uri (String)

    API path (e.g. "/v1/payments/")

  • data (Hash, nil)

    request body (will be JSON-encoded)

  • request_options (RequestOptions, nil) (defaults to: nil)

    per-call configuration override

Returns:

  • (Hash{Symbol => Object})

    :status and :response

Raises:

  • (TypeError)

    if data is not a Hash



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)

  request_options = _check_request_options(request_options)
  headers = _check_headers(request_options, { '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: request_options.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.

Parameters:

  • uri (String)

    API path (e.g. "/v1/payments/123")

  • data (Hash)

    request body (will be JSON-encoded)

  • request_options (RequestOptions, nil) (defaults to: nil)

    per-call configuration override

Returns:

  • (Hash{Symbol => Object})

    :status and :response

Raises:

  • (TypeError)

    if data is not a Hash



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)

  request_options = _check_request_options(request_options)
  headers = _check_headers(request_options, { 'Content-Type': @config.mime_json })

  MPResponse.new(
    @http_client.put(url: @config.api_base_url + uri, data: data.to_json, headers: headers,
                     timeout: request_options.connection_timeout)
  )
end