class PQSDK::RestLayer

A small wrapper to the Faraday gem to make get/post/put requests to the API server.

Public Class Methods

check_result(result) click to toggle source
# File lib/pqsdk/rest_layer.rb, line 27
def self.check_result(result)
  status = result.status.to_i
  headers = result.headers
  raise "Internal Server Error: #{result.body}" if status >= 500
  raise 'You are not authorized to perform that request' if status == 401

  begin
    [status, JSON.parse(result.body), headers]
  rescue JSON::ParserError
    [status, nil, headers]
  end
end
connection() click to toggle source
# File lib/pqsdk/rest_layer.rb, line 23
def self.connection
  Faraday.new(Settings.api_root)
end
get(endpoint, parameters = {}, headers = {}) click to toggle source
# File lib/pqsdk/rest_layer.rb, line 5
def self.get(endpoint, parameters = {}, headers = {})
  res = connection.get endpoint, parameters, headers

  check_result(res)
end
post(endpoint, parameters = {}, headers = {}) click to toggle source
# File lib/pqsdk/rest_layer.rb, line 11
def self.post(endpoint, parameters = {}, headers = {})
  res = connection.post endpoint, parameters.to_json, headers.merge('Content-Type' => 'application/json')

  check_result(res)
end
put(endpoint, parameters = {}, headers = {}) click to toggle source
# File lib/pqsdk/rest_layer.rb, line 17
def self.put(endpoint, parameters = {}, headers = {})
  res = connection.put endpoint, parameters.to_json, headers.merge('Content-Type' => 'application/json')

  check_result(res)
end