Class: Rack::Response

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/rack/response.rb

Overview

Rack::Response provides a convenient interface to create a Rack response.

It allows setting of headers and cookies, and provides useful defaults (a OK response containing HTML).

You can use Response#write to iteratively generate your response, but note that this is buffered by Rack::Response until you call finish. finish however can take a block inside which calls to write are syncronous with the Rack response.

Your application’s call should end returning Response#finish.

Defined Under Namespace

Modules: Helpers

Instance Attribute Summary collapse

Attributes included from Helpers

#original_headers

Instance Method Summary collapse

Methods included from Helpers

#client_error?, #content_length, #content_type, #forbidden?, #include?, #informational?, #invalid?, #location, #not_found?, #ok?, #redirect?, #redirection?, #server_error?, #successful?

Constructor Details

#initialize(body = [], status = 200, header = {}) {|_self| ... } ⇒ Response

Returns a new instance of Response.

Yields:

  • (_self)

Yield Parameters:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rack/response.rb', line 21

def initialize(body=[], status=200, header={}, &block)
  @status = status
  @header = Utils::HeaderHash.new({"Content-Type" => "text/html"}.
                                  merge(header))

  @writer = lambda { |x| @body << x }
  @block = nil
  @length = 0

  @body = []

  if body.respond_to? :to_str
    write body.to_str
  elsif body.respond_to?(:each)
    body.each { |part|
      write part.to_s
    }
  else
    raise TypeError, "stringable or iterable required"
  end

  yield self  if block_given?
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



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

def body
  @body
end

#headerObject (readonly) Also known as: headers

Returns the value of attribute header.



45
46
47
# File 'lib/rack/response.rb', line 45

def header
  @header
end

#lengthObject

Returns the value of attribute length.



19
20
21
# File 'lib/rack/response.rb', line 19

def length
  @length
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

Instance Method Details

#[](key) ⇒ Object



48
49
50
# File 'lib/rack/response.rb', line 48

def [](key)
  header[key]
end

#[]=(key, value) ⇒ Object



52
53
54
# File 'lib/rack/response.rb', line 52

def []=(key, value)
  header[key] = value
end

#closeObject



100
101
102
# File 'lib/rack/response.rb', line 100

def close
  body.close if body.respond_to?(:close)
end


60
61
62
# File 'lib/rack/response.rb', line 60

def delete_cookie(key, value={})
  Utils.delete_cookie_header!(header, key, value)
end

#each(&callback) ⇒ Object



81
82
83
84
85
# File 'lib/rack/response.rb', line 81

def each(&callback)
  @body.each(&callback)
  @writer = callback
  @block.call(self)  if @block
end

#empty?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/rack/response.rb', line 104

def empty?
  @block == nil && @body.empty?
end

#finish(&block) ⇒ Object Also known as: to_a



69
70
71
72
73
74
75
76
77
78
# File 'lib/rack/response.rb', line 69

def finish(&block)
  @block = block

  if [204, 304].include?(status.to_i)
    header.delete "Content-Type"
    [status.to_i, header.to_hash, []]
  else
    [status.to_i, header.to_hash, self]
  end
end

#redirect(target, status = 302) ⇒ Object



64
65
66
67
# File 'lib/rack/response.rb', line 64

def redirect(target, status=302)
  self.status = status
  self["Location"] = target
end


56
57
58
# File 'lib/rack/response.rb', line 56

def set_cookie(key, value)
  Utils.set_cookie_header!(header, key, value)
end

#write(str) ⇒ Object

Append to body and update Content-Length.

NOTE: Do not mix #write and direct #body access!



91
92
93
94
95
96
97
98
# File 'lib/rack/response.rb', line 91

def write(str)
  s = str.to_s
  @length += Rack::Utils.bytesize(s)
  @writer.call s

  header["Content-Length"] = @length.to_s
  str
end