Class: Brut::FrontEnd::GenericResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/brut/front_end/generic_response.rb

Overview

A generic response to use when no built-in Brut construct will suffice. This mirrors the response a Rack server is intended to produce, and serves as merely a typed wrapper around that kind of response for the purposes of understanding the intention of whoever is returning this.

Once created, #to_ary will convert this into the array that Rack requires.

Instance Method Summary collapse

Constructor Details

#initialize(http_status: 200, headers: {}, response_body:) ⇒ GenericResponse

Create a generic response.

Note that this value must be a valid HTTP status. through directly to the underlying Rack server so it should be whatever Rack expects, which is generally something that responds to each.

Parameters:

  • http_status (Brut::FrontEnd::HttpStatus|number) (defaults to: 200)

    the status to send. If omitted, a 200 is used.

  • headers (Hash<String|String>) (defaults to: {})

    hash of headers to send with the response.

  • response_body (String|Array<String>|IO|Enumerable)

    the body of the response. This is passed



16
17
18
19
20
# File 'lib/brut/front_end/generic_response.rb', line 16

def initialize(http_status: 200, headers: {}, response_body:)
  @response_body = response_body
  @headers       = headers
  @http_status   = Brut::FrontEnd::HttpStatus.new(http_status.to_i)
end

Instance Method Details

#to_aryArray{String,3} Also known as: deconstruct

Return this as an array suitable for use as a Rack response.

Returns:

  • (Array{String,3})

    the response_body, headers, and http_status



25
26
27
28
29
30
31
# File 'lib/brut/front_end/generic_response.rb', line 25

def to_ary
  [
    @http_status.to_i,
    @headers,
    @response_body,
  ]
end