LennarbSourceLennarbResponse

class Response

Definitions

attr_accessor :status

Signature

returns Integer

attr_reader :body

Signature

returns Array

attr_reader :headers

Signature

returns Hash

attr_reader :length

Signature

returns Integer

LOCATION = 'location'

Constants

def initialize

Initialize the response object

Signature

returns Response

Implementation

def initialize
  @status  = 404
  @headers = {}
  @body    = []
  @length  = 0
end

def [](key)

Set the response header

Signature

returns String

value

Implementation

def [](key)
  @headers[key]
end

def []=(key, value)

Get the response header

Signature

returns String

value

Implementation

def []=(key, value)
  @headers[key] = value
end

def write(str)

Write to the response body

Signature

returns String

str

Implementation

def write(str)
  str = str.to_s
  @length += str.bytesize
  @headers[CONTENT_LENGTH] = @length.to_s
  @body << str
end

def text(str)

Set the response type to text

Signature

returns String

str

Implementation

def text(str)
  @headers[CONTENT_TYPE] = ContentType[:TEXT]
  write(str)
end

def html(str)

Set the response type to html

Signature

returns String

str

Implementation

def html(str)
  @headers[CONTENT_TYPE] = ContentType[:HTML]
  write(str)
end

def json(str)

Set the response type to json

Signature

returns String

str

Implementation

def json(str)
  @headers[CONTENT_TYPE] = ContentType[:JSON]
  write(str)
end

def redirect(path, status = 302)

Redirect the response

Implementation

def redirect(path, status = 302)
  @headers[LOCATION] = path
  @status = status

  throw :halt, finish
end

def finish

Finish the response

Signature

returns Array

response

Implementation

def finish
  [@status, @headers, @body]
end