Class: MundipaggInterface::Api

Inherits:
Object
  • Object
show all
Includes:
Custumers::Card, Custumers::Custumer, Subscriptions::Subscription
Defined in:
lib/mundipagg_interface.rb

Instance Method Summary collapse

Methods included from Subscriptions::Subscription

#cancel_subscription, #change_payment_card, #create_subscription

Methods included from Custumers::Card

#create_card, #get_cards

Methods included from Custumers::Custumer

#create_custumer, #get_custumers

Constructor Details

#initialize(args = {}) ⇒ Api

Returns a new instance of Api.



19
20
21
22
23
24
# File 'lib/mundipagg_interface.rb', line 19

def initialize(args={})
  @basic_auth_secret_key = args[:basic_auth_secret_key]
  @basic_auth_secret_key_encoded = Base64.strict_encode64("#{@basic_auth_secret_key}:")
  @endpoint_url = 'https://api.mundipagg.com/core/v1'

end

Instance Method Details

#delete_request(action, params = {}, headers = {}) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/mundipagg_interface.rb', line 82

def delete_request(action, params={}, headers={})
  begin
    headers = headers.merge({
      'Content-Type' => 'application/json',
      'Authorization' => "Basic #{@basic_auth_secret_key_encoded}"
    })

    api_response_kind = headers.delete('api_response_kind')
    api_response_kind = headers.delete(:api_response_kind) if api_response_kind.nil?
    api_response_kind = 'object' if api_response_kind.nil?

    parse_response(api_response_kind, RestClient.delete("#{@endpoint_url}#{action}", {params: params}.merge(headers)))
  rescue => e
    parse_response('object', e.response)
  end
end

#get_request(action, params = {}, headers = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mundipagg_interface.rb', line 26

def get_request(action, params={}, headers={})
  begin
    headers = headers.merge({
      'Content-Type' => 'application/json',
      'Authorization' => "Basic #{@basic_auth_secret_key_encoded}"
    })

    api_response_kind = headers.delete('api_response_kind')
    api_response_kind = headers.delete(:api_response_kind) if api_response_kind.nil?
    api_response_kind = 'object' if api_response_kind.nil?

    parse_response(api_response_kind, RestClient.get("#{@endpoint_url}#{action}", {params: params}.merge(headers)))
  rescue => e
    parse_response('object', e.response)
  end
end

#parse_response(api_response_kind, response) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/mundipagg_interface.rb', line 120

def parse_response(api_response_kind, response)
  result = OpenStruct.new
  result.status_code = response.code

  if api_response_kind == 'object'
    result.headers = (JSON.parse(response.headers.to_json, object_class: OpenStruct) rescue response.headers)
    result.body = (JSON.parse(response.body, object_class: OpenStruct) rescue response.body)
  elsif api_response_kind == 'hash'
    result.headers = response.headers
    result.body = (JSON.parse(response.body) rescue response.body)
  else
    result.headers = response.headers
    result.body = response.body
  end

  @last_result = result

  result
end

#patch_request(action, params = {}, headers = {}) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/mundipagg_interface.rb', line 100

def patch_request(action, params={}, headers={})
  begin
    headers = headers.merge({
      'accept' => 'application/json', 
      'Content-Type' => 'application/json',
      'Authorization' => "Basic #{@basic_auth_secret_key_encoded}"
    })

    api_response_kind = headers.delete('api_response_kind')
    api_response_kind = headers.delete(:api_response_kind) if api_response_kind.nil?
    api_response_kind = 'object' if api_response_kind.nil?

    params = params.to_json  unless params.kind_of? String

    parse_response(api_response_kind, RestClient.patch("#{@endpoint_url}#{action}", params, headers))
  rescue => e
    parse_response('object', e.response)
  end
end

#post_request(action, params = {}, headers = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mundipagg_interface.rb', line 63

def post_request(action, params={}, headers={})
  begin
    headers = headers.merge({
      'Content-Type' => 'application/json',
      'Authorization' => "Basic #{@basic_auth_secret_key_encoded}"
    })

    api_response_kind = headers.delete('api_response_kind')
    api_response_kind = headers.delete(:api_response_kind) if api_response_kind.nil?
    api_response_kind = 'object' if api_response_kind.nil?

    params = params.to_json  unless params.kind_of? String

    parse_response(api_response_kind, RestClient.post("#{@endpoint_url}#{action}", params , headers))
  rescue => e
    parse_response('object', e.response)
  end
end

#put_request(action, params = {}, headers = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mundipagg_interface.rb', line 43

def put_request(action, params={}, headers={})
  begin
    headers = headers.merge({
      'Content-Type' => 'application/json',
      'Authorization' => "Basic #{@basic_auth_secret_key_encoded}"
    })

    api_response_kind = headers.delete('api_response_kind')
    api_response_kind = headers.delete(:api_response_kind) if api_response_kind.nil?
    api_response_kind = 'object' if api_response_kind.nil?

    params = params.to_json  unless params.kind_of? String

    parse_response(api_response_kind, RestClient.put("#{@endpoint_url}#{action}", params, headers))
  rescue => e
    parse_response('object', e.response)
  end
end