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:



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

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



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

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:



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

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)



124
125
126
127
128
129
130
# File 'lib/mercadopago/core/mp_base.rb', line 124

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

  @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



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)

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

  @http_client.get(url: @config.api_base_url + uri, headers: headers, params: filters,
                   timeout: request_options.connection_timeout, maxretries: request_options.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.

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



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)

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

  @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



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)

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

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