Module: K2ConnectRuby::K2Utilities::K2Connection

Extended by:
K2Connection
Included in:
K2Connection
Defined in:
lib/k2-connect-ruby/k2_utilities/k2_connection.rb

Instance Method Summary collapse

Instance Method Details

#make_request(connection_hash) ⇒ Object

Method for sending the request to K2 sandbox or Mock Server



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/k2-connect-ruby/k2_utilities/k2_connection.rb', line 6

def make_request(connection_hash)
  access_token = connection_hash[:access_token]
  class_type = connection_hash[:class_type]
  path_url = connection_hash[:path_url]
  request_type = connection_hash[:request_type]
  requires_token = connection_hash[:requires_token]

  unless class_type.eql?('Access Token') || access_token.present?
    raise ArgumentError, 'No Access Token in Arguments!'
  end

  # Set up Headers
  headers = if requires_token
    { 'Content-Type': 'application/json', Accept: 'application/json', Authorization: "Bearer #{access_token}" }
  else
    { 'Content-Type': 'application/json' }
  end

  k2_response = RestClient::Request.execute(
    method: request_type,
    url: path_url,
    headers: headers,
    payload: connection_hash[:params].to_json
  )

  # Response Body
  response_body = JSON.parse(k2_response.body, symbolize_names: true) if k2_response.body.present?
  response_headers = JSON.parse(k2_response.headers.to_json, symbolize_names: true)
  response_code = k2_response.code.to_s
  raise K2ConnectRuby::K2ConnectionError.new(response_code) unless response_code.starts_with?("2")

  if request_type.eql?('get')
    response_body
  else
    # Returns the access token for authorization
    return response_body[:access_token] if path_url.include?('oauth/token')

    response_headers[:location]
  end
end