Class: Rack::Response

Inherits:
Object show all
Includes:
Helpers
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rack-2.2.5/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, Sinatra::Response

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#accepted?, #add_header, #bad_request?, #cache!, #cache_control, #cache_control=, #client_error?, #content_length, #content_type, #content_type=, #created?, #delete_cookie, #do_not_cache!, #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, headers = {}) {|_self| ... } ⇒ Response

Initialize the response object with the specified body, status and headers.

HTTP protocol RFCs. conform to the HTTP protocol RFCs.

Providing a body which responds to #to_str is legacy behaviour.

Parameters:

  • body (nil, #each, #to_str) (defaults to: nil)

    the response body.

  • status (Integer) (defaults to: 200)

    the integer status as defined by the

  • headers (#each) (defaults to: {})

    a list of key-value header pairs which

Yields:

  • (_self)

Yield Parameters:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rack-2.2.5/lib/rack/response.rb', line 42

def initialize(body = nil, status = 200, headers = {})
  @status = status.to_i
  @headers = Utils::HeaderHash[headers]

  @writer = self.method(:append)

  @block = nil

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

  yield self if block_given?
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



26
27
28
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rack-2.2.5/lib/rack/response.rb', line 26

def body
  @body
end

#headersObject (readonly) Also known as: header

Returns the value of attribute headers.



27
28
29
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rack-2.2.5/lib/rack/response.rb', line 27

def headers
  @headers
end

#lengthObject

Returns the value of attribute length.



26
27
28
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rack-2.2.5/lib/rack/response.rb', line 26

def length
  @length
end

#statusObject

Returns the value of attribute status.



26
27
28
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rack-2.2.5/lib/rack/response.rb', line 26

def status
  @status
end

Class Method Details

.[](status, headers, body) ⇒ Object



19
20
21
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rack-2.2.5/lib/rack/response.rb', line 19

def self.[](status, headers, body)
  self.new(body, status, headers)
end

Instance Method Details

#chunked?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rack-2.2.5/lib/rack/response.rb', line 73

def chunked?
  CHUNKED == get_header(TRANSFER_ENCODING)
end

#closeObject



118
119
120
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rack-2.2.5/lib/rack/response.rb', line 118

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

#delete_header(key) ⇒ Object



129
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rack-2.2.5/lib/rack/response.rb', line 129

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

#each(&callback) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rack-2.2.5/lib/rack/response.rb', line 98

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

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

#empty?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rack-2.2.5/lib/rack/response.rb', line 122

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

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

Generate a response array consistent with the requirements of the SPEC. which is suitable to be returned from the middleware ‘#call(env)` method.

Returns:

  • (Array)

    a 3-tuple suitable of ‘[status, headers, body]`



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rack-2.2.5/lib/rack/response.rb', line 80

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

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



127
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rack-2.2.5/lib/rack/response.rb', line 127

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

#has_header?(key) ⇒ Boolean

Returns:

  • (Boolean)


126
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rack-2.2.5/lib/rack/response.rb', line 126

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

#redirect(target, status = 302) ⇒ Object



68
69
70
71
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rack-2.2.5/lib/rack/response.rb', line 68

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

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



128
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rack-2.2.5/lib/rack/response.rb', line 128

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!



112
113
114
115
116
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rack-2.2.5/lib/rack/response.rb', line 112

def write(chunk)
  buffered_body!

  @writer.call(chunk.to_s)
end