Class: PQSDK::RestLayer

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

Class Method Summary collapse

Class Method Details

.get(endpoint, parameters, headers) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/pqsdk/rest_layer.rb', line 3

def self.get(endpoint, parameters, headers)
  url = URI.parse("#{Settings.schema}://#{Settings.host}/#{endpoint}")
  url.query = URI.encode_www_form(parameters)
  req = Net::HTTP::Get.new(url.request_uri)

  headers.each do |name, value|
    req[name.to_s] = value
  end

  res = Net::HTTP.start(url.host, url.port) do |http|
    http.request(req)
  end

  check_status(res.code.to_i, res.body)

  [ res.code.to_i, JSON.parse(res.body), res.to_hash ]
end

.post(endpoint, parameters, headers) ⇒ Object



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/pqsdk/rest_layer.rb', line 21

def self.post(endpoint, parameters, headers)
  url = URI.parse("#{Settings.schema}://#{Settings.host}/#{endpoint}")
  req = Net::HTTP::Post.new(url.request_uri)

  if headers['Content-Type'] == 'application/json'
    req.body = parameters.to_json
  else
    req.set_form_data(parameters)
  end

  headers.each do |name, value|
    req[name.to_s] = value
  end

  res = Net::HTTP.start(url.host, url.port) do |http|
    http.request(req)
  end

  check_status(res.code.to_i, res.body)
  if res.body and res.body.length > 1
    [ res.code.to_i, JSON.parse(res.body), res.to_hash ]
  else
    [ res.code.to_i, {}, res.to_hash ]
  end
end

.put(endpoint, parameters, headers) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/pqsdk/rest_layer.rb', line 47

def self.put(endpoint, parameters, headers)
  url = URI.parse("#{Settings.schema}://#{Settings.host}/#{endpoint}")
  req = Net::HTTP::Put.new(url.request_uri)

  if headers['Content-Type'] == 'application/json'
    req.body = parameters.to_json
  else
    req.set_form_data(parameters)
  end

  headers.each do |name, value|
    req[name.to_s] = value
  end

  res = Net::HTTP.start(url.host, url.port) do |http|
    http.request(req)
  end

  check_status(res.code.to_i, res.body)
  [ res.code.to_i, JSON.parse(res.body), res.to_hash ]
end