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 (an OK response with empty headers and body).

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 synchronous with the Rack response.

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

Direct Known Subclasses

MockResponse

Defined Under Namespace

Modules: Helpers Classes: Raw

Constant Summary collapse

CHUNKED =
'chunked'
STATUS_WITH_NO_ENTITY_BODY =
Utils::STATUS_WITH_NO_ENTITY_BODY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#accepted?, #add_header, #bad_request?, #cache_control, #cache_control=, #client_error?, #content_length, #content_type, #created?, #delete_cookie, #etag, #etag=, #forbidden?, #include?, #informational?, #invalid?, #location, #location=, #media_type, #media_type_params, #method_not_allowed?, #moved_permanently?, #no_content?, #not_found?, #ok?, #precondition_failed?, #redirect?, #redirection?, #server_error?, #set_cookie, #set_cookie_header, #set_cookie_header=, #successful?, #unauthorized?, #unprocessable?

Constructor Details

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

Returns a new instance of Response.

Yields:

  • (_self)

Yield Parameters:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rack/response.rb', line 31

def initialize(body = nil, status = 200, header = {})
  @status = status.to_i
  @header = Utils::HeaderHash.new(header)

  @writer = self.method(:append)

  @block = nil
  @length = 0

  # Keep track of whether we have expanded the user supplied body.
  if body.nil?
    @body = []
    @buffered = true
  elsif body.respond_to?(:to_str)
    @body = [body]
    @buffered = true
  else
    @body = body
    @buffered = false
  end

  yield self if block_given?
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



24
25
26
# File 'lib/rack/response.rb', line 24

def body
  @body
end

#headerObject (readonly) Also known as: headers

Returns the value of attribute header.



25
26
27
# File 'lib/rack/response.rb', line 25

def header
  @header
end

#lengthObject

Returns the value of attribute length.



24
25
26
# File 'lib/rack/response.rb', line 24

def length
  @length
end

#statusObject

Returns the value of attribute status.



24
25
26
# File 'lib/rack/response.rb', line 24

def status
  @status
end

Instance Method Details

#chunked?Boolean

Returns:

  • (Boolean)


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

def chunked?
  CHUNKED == get_header(TRANSFER_ENCODING)
end

#closeObject



102
103
104
# File 'lib/rack/response.rb', line 102

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

#delete_header(key) ⇒ Object



113
# File 'lib/rack/response.rb', line 113

def delete_header(key); headers.delete key; end

#each(&callback) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/rack/response.rb', line 82

def each(&callback)
  @body.each(&callback)
  @buffered = true

  if @block
    @writer = callback
    @block.call(self)
  end
end

#empty?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/rack/response.rb', line 106

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

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



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

def finish(&block)
  if STATUS_WITH_NO_ENTITY_BODY[status.to_i]
    delete_header CONTENT_TYPE
    delete_header CONTENT_LENGTH
    close
    [status.to_i, header, []]
  else
    if block_given?
      @block = block
      [status.to_i, header, self]
    else
      [status.to_i, header, @body]
    end
  end
end

#get_header(key) ⇒ Object Also known as: []



111
# File 'lib/rack/response.rb', line 111

def get_header(key);    headers[key];       end

#has_header?(key) ⇒ Boolean

Returns:

  • (Boolean)


110
# File 'lib/rack/response.rb', line 110

def has_header?(key);   headers.key? key;   end

#redirect(target, status = 302) ⇒ Object



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

def redirect(target, status = 302)
  self.status = status
  self.location = target
end

#set_header(key, v) ⇒ Object Also known as: []=



112
# File 'lib/rack/response.rb', line 112

def set_header(key, v); headers[key] = v;   end

#write(chunk) ⇒ Object

Append to body and update Content-Length.

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



96
97
98
99
100
# File 'lib/rack/response.rb', line 96

def write(chunk)
  buffered_body!

  @writer.call(chunk.to_s)
end