Module: QTest::REST::Utils

Included in:
API, Module, Project, Release, TestCase, TestCycle, TestRun, TestSuite
Defined in:
lib/qtest/rest/utils.rb

Instance Method Summary collapse

Instance Method Details

#decode_response_body(body, symbolize_keys = true) ⇒ Object

Decode a String (Response body, typically) from JSON.

By default, keys in the Hash will be converted to symbols.

Parameters:

  • body (String)

    String to decode

  • symbolize_keys (Boolean) (defaults to: true)

    convert keys to Symbols



52
53
54
# File 'lib/qtest/rest/utils.rb', line 52

def decode_response_body(body, symbolize_keys = true)
  JSON.parse(body, symbolize_names: symbolize_keys)
end

#delete(query, _opts = {}) ⇒ Object



16
17
18
# File 'lib/qtest/rest/utils.rb', line 16

def delete(query, _opts = {})
  handle_response(self.class.delete(query[:path], query), raw: true)
end

#get(query, opts = {}) ⇒ Object



8
9
10
# File 'lib/qtest/rest/utils.rb', line 8

def get(query, opts = {})
  handle_response(self.class.get(query[:path], query), opts)
end

#handle_response(response, opts = {}) ⇒ Object

Handle a Response based on its status code.

By default, the Response body is assumed to be parsed from JSON.

## Options

* :raw - Return the Response body without decoding.

Parameters:

  • response (Net::HTTP::Response)

    response to handle

  • opts (Hash) (defaults to: {})

    Hash of options



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/qtest/rest/utils.rb', line 31

def handle_response(response, opts = {})
  case response.code
  when 200..207
    if opts[:raw]
      response.body
    else
      decode_response_body(response.body)
    end
  when 401
    raise QTest::AuthorizationError, response.body
  else
    raise QTest::Error, response.body
  end
end

#post(query, opts = {}) ⇒ Object



4
5
6
# File 'lib/qtest/rest/utils.rb', line 4

def post(query, opts = {})
  handle_response(self.class.post(query[:path], query), opts)
end

#put(query, opts = {}) ⇒ Object



12
13
14
# File 'lib/qtest/rest/utils.rb', line 12

def put(query, opts = {})
  handle_response(self.class.put(query[:path], query), opts)
end