Class: GoPay::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/gopay/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



5
6
7
# File 'lib/gopay/client.rb', line 5

def initialize(config)
  @config = config
end

Instance Method Details

#request(method, path, body_parameters: {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/gopay/client.rb', line 9

def request(method, path, body_parameters: {})
  token = token get_token_scope(method, path)
  content_type = get_content_type(path)
  body_parameters = content_type == 'application/json' ? body_parameters.to_json : body_parameters
  begin
    response = RestClient::Request.execute(method: method, url: @config[:gate]+path, payload: body_parameters, headers: { "Accept" => "application/json", "Content-Type" => content_type, "Authorization" => "Bearer #{token}" })
  rescue RestClient::ExceptionWithResponse => e
    raise Error.handle_gopay_error(e.response)
  end

  unless response.code == 200
    raise Error.handle_gopay_error(response)
  end

  JSON.parse response.body
end