Class: KineticSdk::Utils::KineticHttpResponse

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ KineticHttpResponse

Constructor

Parameters:

  • object (Net::HTTPResponse | StandardError)

    either a Net::HTTPResponse or a StandardError



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.message
    @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.message
    @status = @code.to_i
  else
    raise StandardError.new("Invalid response object: #{object.class}")
  end
end

Instance Attribute Details

#backtraceObject (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

#codeObject (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

#contentObject

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_stringObject

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_typeObject (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

#exceptionObject (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

#headersObject (readonly)

the resonse headers



24
25
26
# File 'lib/kinetic_sdk/utils/kinetic-http-response.rb', line 24

def headers
  @headers
end

#messageObject (readonly)

response status message



26
27
28
# File 'lib/kinetic_sdk/utils/kinetic-http-response.rb', line 26

def message
  @message
end

#responseObject (readonly)

the raw response object



28
29
30
# File 'lib/kinetic_sdk/utils/kinetic-http-response.rb', line 28

def response
  @response
end

#statusObject (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