Class: AgileProxy::Response

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/agile_proxy/model/response.rb

Overview

The associated response for the RequestSpec

An instance of this class is expected to be stored alongside every RequestSpec.

It is responsible for the following :-

  1. Retrieving response

  2. Persisting responses

  3. Providing a ‘rack’ ouput of the response

  4. Convenient setters for code, body and content_type

  5. Template parsing

Constant Summary collapse

PROTECTED_HEADERS =
['Content-Type']

Instance Method Summary collapse

Instance Method Details

#content_type=(val) ⇒ Object

A convenient setter for the content_type within the header

Parameters:

  • val (String)

    The content type



22
23
24
25
26
# File 'lib/agile_proxy/model/response.rb', line 22

def content_type=(val)
  write_attribute(:content_type, val)
  headers.merge!('Content-Type' => val)
  val
end

#to_rack(input_params, _input_headers, _input_body) ⇒ Array

Provides the response as a ‘rack’ response

If the response is a template (by specifying is_template as true), the output will have its template values parsed and replaced with data from the input_params, input_headers and input_body Otherwise, the body of the output is sent as is.

Parameters:

  • input_params (Hash)

    The input parameters as a hash

  • _input_headers (Hash)

    The input headers as a hash

  • _input_body (String)

    The input body

Returns:

  • (Array)

    A ‘rack’ response array (status, headers, body)



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/agile_proxy/model/response.rb', line 37

def to_rack(input_params, _input_headers, _input_body)
  output_headers = headers.clone
  output_headers.merge! 'Cache-Control' => 'no-store'
  output_content = content
  output_status_code = status_code
  if is_template
    data = OpenStruct.new input_params
    template = Tilt['handlebars'].new { output_content }
    output_content = template.render data
  end
  EventMachine::Synchrony.sleep(delay) if delay > 0
  [output_status_code, output_headers, [output_content]]
end