Class: IBM::Cloud::SDK::VPC::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/ibm/cloud/sdk/vpc/helpers/response.rb

Overview

Encapsulate the HTTP response.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



13
14
15
# File 'lib/ibm/cloud/sdk/vpc/helpers/response.rb', line 13

def initialize(response)
  @response = response
end

Instance Attribute Details

#responseHTTP::Response (readonly)

The raw HTTP response.

Returns:

  • (HTTP::Response)


19
20
21
# File 'lib/ibm/cloud/sdk/vpc/helpers/response.rb', line 19

def response
  @response
end

Instance Method Details

#array_responseArray

Verify that the json response is an array.

Returns:

  • (Array)

    Response from JSON

Raises:

  • (RuntimeError)

    JSON object is not a Array.



103
104
105
# File 'lib/ibm/cloud/sdk/vpc/helpers/response.rb', line 103

def array_response
  check_object(Array)
end

#bodyString?

Return the raw response string.

Returns:

  • (String)
  • (nil)

    Response does not have body method.



34
35
36
# File 'lib/ibm/cloud/sdk/vpc/helpers/response.rb', line 34

def body
  response&.body.to_s
end

#check_object(obj) ⇒ Object

Check to see if the returned object is the expected object.

Parameters:

  • obj (Object)

    The object to test the response against.

Raises:



123
124
125
126
127
128
129
# File 'lib/ibm/cloud/sdk/vpc/helpers/response.rb', line 123

def check_object(obj)
  ret = json
  return ret if ret.instance_of?(obj)

  msg = "Expected #{obj} in response for #{url}. The returned object is a #{ret.class}."
  raise Exceptions::ExceptionWithResponse.new(self, msg)
end

#codeInteger? Also known as: status

Return the response code.

Returns:

  • (Integer)

    Response has code method.

  • (nil)

    Response does not have code method.



41
42
43
# File 'lib/ibm/cloud/sdk/vpc/helpers/response.rb', line 41

def code
  response&.code
end

#connectionHTTP::Connection?

Return the raw connection object.

Returns:

  • (HTTP::Connection)
  • (nil)

    Response does not have a connection method.



50
51
52
# File 'lib/ibm/cloud/sdk/vpc/helpers/response.rb', line 50

def connection
  response&.connection
end

#content_typeString?

Return the content type of the response.

Returns:

  • (String)

    The mimetype of the response.

  • (nil)

    Response does not have response method that responds to mime_type.



67
68
69
# File 'lib/ibm/cloud/sdk/vpc/helpers/response.rb', line 67

def content_type
  response&.response&.mime_type
end

#hash_responseHash

Verify that the json response is a hash.

Returns:

  • (Hash)

    Response from JSON

Raises:

  • (RuntimeError)

    JSON object is not a Hash.



96
97
98
# File 'lib/ibm/cloud/sdk/vpc/helpers/response.rb', line 96

def hash_response
  check_object(Hash)
end

#jsonHash, Array

Return the response in a hash or array.

Returns:

  • (Hash)

    When response is a hash.

  • (Array)

    When response is an array.

Raises:



25
26
27
28
29
# File 'lib/ibm/cloud/sdk/vpc/helpers/response.rb', line 25

def json
  JSON.parse(body, symbolize_names: true)
rescue StandardError
  raise Exceptions::ExceptionWithResponse.new(self, "#{url} Error while parsing response body. #{response.body}")
end

#raise_for_status?Response

Chainable method to verify the status code. Raise an exception for non 200-series or 404 status codes.

Returns:

  • (Response)

    Allows for method to be chainable.

Raises:



57
58
59
60
61
62
# File 'lib/ibm/cloud/sdk/vpc/helpers/response.rb', line 57

def raise_for_status?
  return self if (200..299).include?(code)
  return self if code == 404

  raise Exceptions::HttpStatusError.new(self)
end

#reasonString?

Return the textual reason.

Returns:

  • (String)

    HTTP Reason

  • (nil)

    Response does not have reaspn method that responds.



74
75
76
# File 'lib/ibm/cloud/sdk/vpc/helpers/response.rb', line 74

def reason
  response&.reason
end

#subkey(key) ⇒ Any

Find a subkey within the returned response.

Parameters:

  • key (String)

    Name of a first level key.

Returns:

  • (Any)

    Response from JSON

Raises:

  • (RuntimeError)

    JSON object is not a Array.



111
112
113
114
115
116
117
118
# File 'lib/ibm/cloud/sdk/vpc/helpers/response.rb', line 111

def subkey(key)
  ret = hash_response
  sym_key = key.to_sym
  return ret.fetch(sym_key) if ret.key?(sym_key)

  msg = "Key #{key} not found in #{ret}."
  raise Exceptions::ExceptionWithResponse.new(self, msg)
end

#uriHTTP::URI?

Return the sent url as a URI class.

Returns:

  • (HTTP::URI)
  • (nil)

    Response does not have response method that responds to mime_type.

See Also:



89
90
91
# File 'lib/ibm/cloud/sdk/vpc/helpers/response.rb', line 89

def uri
  response&.uri
end

#urlString?

Return the sent url as a string.

Returns:

  • (String)

    Full URL sent

  • (nil)

    Response does not have response method that responds to mime_type.



81
82
83
# File 'lib/ibm/cloud/sdk/vpc/helpers/response.rb', line 81

def url
  response&.uri.to_s
end