Class: Renee::Core::Response

Inherits:
Rack::Response
  • Object
show all
Defined in:
lib/renee_core/response.rb

Overview

The response object for a Renee request. Inherits from the Rack#Response object.

Instance Method Summary collapse

Instance Method Details

#body(val = nil) ⇒ Object

Get or set the body of the response.

Examples:

res.body "hello"
res.body => "hello"

Parameters:

  • val (String) (defaults to: nil)

    The contents to return.



46
47
48
# File 'lib/renee_core/response.rb', line 46

def body(val=nil)
  val ? self.body_attr = val : self.body_attr
end

#body=(value) ⇒ Object Also known as: body_attr=

Augment body to allow strings.

Examples:

res.body = "Hello"

Parameters:

  • The (String)

    contents for the response.



13
14
15
16
# File 'lib/renee_core/response.rb', line 13

def body=(value)
  value = value.body while Rack::Response === value
  @body = String === value ? [value.to_str] : value
end

#body_attrObject



21
# File 'lib/renee_core/response.rb', line 21

alias :body_attr  :body

#finishObject

Finishs the response based on the accumulated options. Calculates the size of the body content length and removes headers for 1xx status codes.



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/renee_core/response.rb', line 65

def finish
  if status.to_i / 100 == 1
    headers.delete "Content-Length"
    headers.delete "Content-Type"
  elsif Array === body and not [204, 304].include?(status.to_i)
    headers["Content-Length"] = body.inject(0) { |l, p| l + Rack::Utils.bytesize(p) }.to_s
  end

  status, headers, result = super
  [status, headers, result]
end

#headers(attrs = {}) ⇒ Object

Get or set the headers of the response.

Examples:

res.headers :foo => "bar"
res.headers => { :foo => "bar" }

Parameters:

  • attrs (Hash) (defaults to: {})

    The contents to return.



59
60
61
# File 'lib/renee_core/response.rb', line 59

def headers(attrs={})
  attrs ? attrs.each { |k, v| self[k.to_s] = v } : self.header
end

#status(val = nil) ⇒ Object

Get or set the status of the response.

Examples:

res.status 400
res.status => 400

Parameters:

  • val (String) (defaults to: nil)

    The status code to return.



33
34
35
# File 'lib/renee_core/response.rb', line 33

def status(val=nil)
  val ? self.status_attr = val : self.status_attr
end

#status_attrObject

Alias status and body methods to allow redefinition



19
# File 'lib/renee_core/response.rb', line 19

alias :status_attr :status