Class: PQSDK::RestLayer

Inherits:
Object
  • Object
show all
Defined in:
lib/pqsdk/rest_layer.rb

Overview

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

Class Method Summary collapse

Class Method Details

.check_result(result) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# 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

.connectionObject



23
24
25
# File 'lib/pqsdk/rest_layer.rb', line 23

def self.connection
  Faraday.new(Settings.api_root)
end

.get(endpoint, parameters = {}, headers = {}) ⇒ Object



5
6
7
8
9
# 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 = {}) ⇒ Object



11
12
13
14
15
# 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 = {}) ⇒ Object



17
18
19
20
21
# 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