Class: IBM::Cloud::SDK::VPC::Response
- Inherits:
-
Object
- Object
- IBM::Cloud::SDK::VPC::Response
- Defined in:
- lib/ibm/cloud/sdk/vpc/helpers/response.rb
Overview
Encapsulate the HTTP response.
Instance Attribute Summary collapse
-
#response ⇒ HTTP::Response
readonly
The raw HTTP response.
Instance Method Summary collapse
-
#array_response ⇒ Array
Verify that the json response is an array.
-
#body ⇒ String?
Return the raw response string.
-
#check_object(obj) ⇒ Object
Check to see if the returned object is the expected object.
-
#code ⇒ Integer?
(also: #status)
Return the response code.
-
#connection ⇒ HTTP::Connection?
Return the raw connection object.
-
#content_type ⇒ String?
Return the content type of the response.
-
#hash_response ⇒ Hash
Verify that the json response is a hash.
-
#initialize(response) ⇒ Response
constructor
A new instance of Response.
-
#json ⇒ Hash, Array
Return the response in a hash or array.
-
#raise_for_status? ⇒ Response
Chainable method to verify the status code.
-
#reason ⇒ String?
Return the textual reason.
-
#subkey(key) ⇒ Any
Find a subkey within the returned response.
-
#uri ⇒ HTTP::URI?
Return the sent url as a URI class.
-
#url ⇒ String?
Return the sent url as a string.
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
#response ⇒ HTTP::Response (readonly)
The raw HTTP response.
19 20 21 |
# File 'lib/ibm/cloud/sdk/vpc/helpers/response.rb', line 19 def response @response end |
Instance Method Details
#array_response ⇒ Array
Verify that the json response is an array.
103 104 105 |
# File 'lib/ibm/cloud/sdk/vpc/helpers/response.rb', line 103 def array_response check_object(Array) end |
#body ⇒ String?
Return the raw response string.
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.
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 |
#code ⇒ Integer? Also known as: status
Return the response code.
41 42 43 |
# File 'lib/ibm/cloud/sdk/vpc/helpers/response.rb', line 41 def code response&.code end |
#connection ⇒ HTTP::Connection?
Return the raw connection object.
50 51 52 |
# File 'lib/ibm/cloud/sdk/vpc/helpers/response.rb', line 50 def connection response&.connection end |
#content_type ⇒ String?
Return the content type of the response.
67 68 69 |
# File 'lib/ibm/cloud/sdk/vpc/helpers/response.rb', line 67 def content_type response&.response&.mime_type end |
#hash_response ⇒ Hash
Verify that the json response is a hash.
96 97 98 |
# File 'lib/ibm/cloud/sdk/vpc/helpers/response.rb', line 96 def hash_response check_object(Hash) end |
#json ⇒ Hash, Array
Return the response in a hash or array.
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.
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 |
#reason ⇒ String?
Return the textual reason.
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.
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 |
#uri ⇒ HTTP::URI?
Return the sent url as a URI class.
89 90 91 |
# File 'lib/ibm/cloud/sdk/vpc/helpers/response.rb', line 89 def uri response&.uri end |
#url ⇒ String?
Return the sent url as a string.
81 82 83 |
# File 'lib/ibm/cloud/sdk/vpc/helpers/response.rb', line 81 def url response&.uri.to_s end |