Sha256: 857891227b393d559e903bc2f59876b8b670cf4e8bbfcd1679c335c69c667034

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

module K2Connect
  # Method for sending the request to K2 sandbox or Mock Server
  def self.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]

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

    # Set up Headers
    headers = { 'Content-Type': 'application/json', Accept: 'application/vnd.kopokopo.v1.hal+json', Authorization: "Bearer #{access_token}" }
    if path_url.include?('oauth/token')
      headers = { '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 = Yajl::Parser.parse(k2_response.body)
    response_headers = Yajl::Parser.parse(k2_response.headers.to_json)
    response_code = k2_response.code.to_s
    raise K2ConnectionError.new(response_code) && k2_request.shutdown unless response_code[0].eql?(2.to_s)

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

      # Return the location url for post Requests
      return response_headers["location"]
    end

    # Return the result of the Query
    return response_body
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
k2-connect-ruby-0.0.2 lib/k2-connect-ruby/k2_utilities/k2_connection.rb
k2-connect-ruby-0.0.1 lib/k2-connect-ruby/k2_utilities/k2_connection.rb