Class: Skytap::Requester::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/skytap/requester.rb

Overview

TODO:NLA Move more of the implementation in request() below into this class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, http_response, options = {}) ⇒ Response

Returns a new instance of Response.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/skytap/requester.rb', line 6

def initialize(logger, http_response, options = {})
  @logger = logger
  @http_response = http_response

  logger.debug 'Response:'.color(:cyan).bright
  each_header do |k, v|
    logger.debug "#{k}: #{v}".color(:cyan)
  end
  logger.debug "\n#{body}".color(:cyan)
  logger.debug '---'.color(:cyan).bright

  # Raise unless response is 2XX.
  @http_response.value if options[:raise]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



21
22
23
# File 'lib/skytap/requester.rb', line 21

def method_missing(*args, &block)
  http_response.send(*args, &block)
end

Instance Attribute Details

#http_responseObject (readonly)

Returns the value of attribute http_response.



5
6
7
# File 'lib/skytap/requester.rb', line 5

def http_response
  @http_response
end

#loggerObject (readonly)

Returns the value of attribute logger.



5
6
7
# File 'lib/skytap/requester.rb', line 5

def logger
  @logger
end

Instance Method Details

#pretty_bodyObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/skytap/requester.rb', line 25

def pretty_body
  return body if body.blank?
  if content_type.include?('json')
    begin
      JSON.pretty_generate(JSON.load(body))
    rescue
      body
    end
  else
    body
  end
end