Class: KineticSdk::Utils::KineticHttpResponse
- Inherits:
-
Object
- Object
- KineticSdk::Utils::KineticHttpResponse
- Defined in:
- lib/kinetic_sdk/utils/kinetic-http-response.rb
Overview
The KineticHttpResponse object normalizes the Net::HTTPResponse object properties so they are always consistent.
If the object passed in the constructor is a StandardError, the status code is set to 0, and the #exception and #backtrace methods can be used to get the details.
Regardless of whether a Net::HTTPResponse object or a StandardError object was passed in the constructor, the #code and #message methods will give information about the response.
Instance Attribute Summary collapse
-
#backtrace ⇒ Object
readonly
the StandardError backtrace if constructor object is a StandardError.
-
#code ⇒ Object
readonly
response code [String] - always '0' if constructor object is a StandardError.
-
#content ⇒ Object
the parsed JSON response body if content-type is application/json.
-
#content_string ⇒ Object
the raw response body string.
-
#content_type ⇒ Object
readonly
the response content-type.
-
#exception ⇒ Object
readonly
the raw StandardError if constructor object is a StandardError.
-
#headers ⇒ Object
readonly
the resonse headers.
-
#message ⇒ Object
readonly
response status message.
-
#response ⇒ Object
readonly
the raw response object.
-
#status ⇒ Object
readonly
response code [Fixnum] - always 0 if constructor object is a StandardError.
Instance Method Summary collapse
-
#initialize(object) ⇒ KineticHttpResponse
constructor
Constructor.
Constructor Details
#initialize(object) ⇒ KineticHttpResponse
Constructor
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/kinetic_sdk/utils/kinetic-http-response.rb', line 40 def initialize(object) case object when Net::HTTPResponse then @code = object.code @content_string = object.body @content_type = object.content_type @headers = object.each_header.inject({}) { |h,(k,v)| h[k] = v; h } @message = object. @response = object @status = @code.to_i # if content type is json, try to parse the content string @content = case @content_type when "application/json" then # will raise an exception if content_string is not valid json JSON.parse(@content_string) else {} end when StandardError then @code = "0" @content = {} @content_string = nil @content_type = nil @backtrace = object.backtrace @exception = object.exception @message = object. @status = @code.to_i else raise StandardError.new("Invalid response object: #{object.class}") end end |
Instance Attribute Details
#backtrace ⇒ Object (readonly)
the StandardError backtrace if constructor object is a StandardError
33 34 35 |
# File 'lib/kinetic_sdk/utils/kinetic-http-response.rb', line 33 def backtrace @backtrace end |
#code ⇒ Object (readonly)
response code [String] - always '0' if constructor object is a StandardError
16 17 18 |
# File 'lib/kinetic_sdk/utils/kinetic-http-response.rb', line 16 def code @code end |
#content ⇒ Object
the parsed JSON response body if content-type is application/json
18 19 20 |
# File 'lib/kinetic_sdk/utils/kinetic-http-response.rb', line 18 def content @content end |
#content_string ⇒ Object
the raw response body string
20 21 22 |
# File 'lib/kinetic_sdk/utils/kinetic-http-response.rb', line 20 def content_string @content_string end |
#content_type ⇒ Object (readonly)
the response content-type
22 23 24 |
# File 'lib/kinetic_sdk/utils/kinetic-http-response.rb', line 22 def content_type @content_type end |
#exception ⇒ Object (readonly)
the raw StandardError if constructor object is a StandardError
35 36 37 |
# File 'lib/kinetic_sdk/utils/kinetic-http-response.rb', line 35 def exception @exception end |
#headers ⇒ Object (readonly)
the resonse headers
24 25 26 |
# File 'lib/kinetic_sdk/utils/kinetic-http-response.rb', line 24 def headers @headers end |
#message ⇒ Object (readonly)
response status message
26 27 28 |
# File 'lib/kinetic_sdk/utils/kinetic-http-response.rb', line 26 def @message end |
#response ⇒ Object (readonly)
the raw response object
28 29 30 |
# File 'lib/kinetic_sdk/utils/kinetic-http-response.rb', line 28 def response @response end |
#status ⇒ Object (readonly)
response code [Fixnum] - always 0 if constructor object is a StandardError
30 31 32 |
# File 'lib/kinetic_sdk/utils/kinetic-http-response.rb', line 30 def status @status end |