Class: Mercadopago::MPBase

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

Overview

Module Base

Instance Method Summary collapse

Constructor Details

#initialize(request_options, http_client) ⇒ MPBase

Returns a new instance of MPBase.



8
9
10
11
12
13
14
15
16
17
# File 'lib/mercadopago/core/mp_base.rb', line 8

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) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/mercadopago/core/mp_base.rb', line 31

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) ⇒ Object



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

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) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/mercadopago/core/mp_base.rb', line 69

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) ⇒ Object

Raises:

  • (TypeError)


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

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) ⇒ Object

Raises:

  • (TypeError)


49
50
51
52
53
54
55
56
57
# File 'lib/mercadopago/core/mp_base.rb', line 49

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 })

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

#_put(uri:, data:, request_options: nil) ⇒ Object

Raises:

  • (TypeError)


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

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