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'.freeze

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 = [], status = 200, header = {}) {|_self| ... } ⇒ Response

Returns a new instance of Response.

Yields:

  • (_self)

Yield Parameters:



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

def initialize(body=[], status=200, header={})
  @status = status.to_i
  @header = Utils::HeaderHash.new.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.



22
23
24
# File 'lib/rack/response.rb', line 22

def body
  @body
end

#headerObject (readonly) Also known as: headers

Returns the value of attribute header.



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

def header
  @header
end

#lengthObject

Returns the value of attribute length.



22
23
24
# File 'lib/rack/response.rb', line 22

def length
  @length
end

#statusObject

Returns the value of attribute status.



22
23
24
# File 'lib/rack/response.rb', line 22

def status
  @status
end

Instance Method Details

#chunked?Boolean

Returns:

  • (Boolean)


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

def chunked?
  CHUNKED == get_header(TRANSFER_ENCODING)
end

#closeObject



94
95
96
# File 'lib/rack/response.rb', line 94

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

#delete_header(key) ⇒ Object



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

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

#each(&callback) ⇒ Object



75
76
77
78
79
# File 'lib/rack/response.rb', line 75

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

#empty?Boolean

Returns:

  • (Boolean)


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

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

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



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rack/response.rb', line 60

def finish(&block)
  @block = block

  if [204, 205, 304].include?(status.to_i)
    delete_header CONTENT_TYPE
    delete_header CONTENT_LENGTH
    close
    [status.to_i, header, []]
  else
    [status.to_i, header, BodyProxy.new(self){}]
  end
end

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



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

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

#has_header?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#redirect(target, status = 302) ⇒ Object



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

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

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



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

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

#write(str) ⇒ Object

Append to body and update Content-Length.

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



85
86
87
88
89
90
91
92
# File 'lib/rack/response.rb', line 85

def write(str)
  s = str.to_s
  @length += s.bytesize unless chunked?
  @writer.call s

  set_header(CONTENT_LENGTH, @length.to_s) unless chunked?
  str
end