module Rack::Response::Helpers
def accepted?; status == 202; end
def accepted?; status == 202; end
def add_header(key, value)
assert_equal 'accept-encoding,cookie', response.get_header('vary')
response.add_header 'vary', 'cookie'
response.add_header 'vary', 'accept-encoding'
Example:
Add a header that may have multiple values.
def add_header(key, value) raise ArgumentError unless key.is_a?(String) if value.nil? return get_header(key) end value = value.to_s if header = get_header(key) if header.is_a?(Array) header << value else set_header(key, [header, value]) end else set_header(key, value) end end
def append(chunk)
def append(chunk) chunk = chunk.dup unless chunk.frozen? @body << chunk if @length @length += chunk.bytesize elsif @buffered @length = chunk.bytesize end return chunk end
def bad_request?; status == 400; end
def bad_request?; status == 400; end
def buffered_body!
-
(Boolean)
- whether the body is buffered as an Array instance.
def buffered_body! if @buffered.nil? if @body.is_a?(Array) # The user supplied body was an array: @body = @body.compact @length = @body.sum{|part| part.bytesize} @buffered = true elsif @body.respond_to?(:each) # Turn the user supplied body into a buffered array: body = @body @body = Array.new @buffered = true body.each do |part| @writer.call(part.to_s) end body.close if body.respond_to?(:close) else # We don't know how to buffer the user-supplied body: @buffered = false end end return @buffered end
def cache!(duration = 3600, directive: "public")
(**directive)
-
The
(String
) -- cache control directive, one of "public", "private", "no-cache" or "no-store".
Parameters:
-
duration
(Integer
) -- The number of seconds until the cache expires.
def cache!(duration = 3600, directive: "public") unless headers[CACHE_CONTROL] =~ /no-cache/ set_header CACHE_CONTROL, "#{directive}, max-age=#{duration}" set_header EXPIRES, (Time.now + duration).httpdate end end
def cache_control
def cache_control get_header CACHE_CONTROL end
def cache_control=(value)
def cache_control=(value) set_header CACHE_CONTROL, value end
def client_error?; status >= 400 && status < 500; end
def client_error?; status >= 400 && status < 500; end
def content_length
def content_length cl = get_header CONTENT_LENGTH cl ? cl.to_i : cl end
def content_type
def content_type get_header CONTENT_TYPE end
def content_type=(content_type)
def content_type=(content_type) set_header CONTENT_TYPE, content_type end
def created?; status == 201; end
def created?; status == 201; end
def delete_cookie(key, value = {})
def delete_cookie(key, value = {}) set_header(SET_COOKIE, Utils.delete_set_cookie_header!( get_header(SET_COOKIE), key, value ) ) end
def do_not_cache!
def do_not_cache! set_header CACHE_CONTROL, "no-cache, must-revalidate" set_header EXPIRES, Time.now.httpdate end
def etag
def etag get_header ETAG end
def etag=(value)
def etag=(value) set_header ETAG, value end
def forbidden?; status == 403; end
def forbidden?; status == 403; end
def include?(header)
def include?(header) has_header?(header) end
def informational?; status >= 100 && status < 200; end
def informational?; status >= 100 && status < 200; end
def invalid?; status < 100 || status >= 600; end
def invalid?; status < 100 || status >= 600; end
def location
def location get_header "location" end
def location=(location)
def location=(location) set_header "location", location end
def media_type
def media_type MediaType.type(content_type) end
def media_type_params
def media_type_params MediaType.params(content_type) end
def method_not_allowed?; status == 405; end
def method_not_allowed?; status == 405; end
def moved_permanently?; status == 301; end
def moved_permanently?; status == 301; end
def no_content?; status == 204; end
def no_content?; status == 204; end
def not_acceptable?; status == 406; end
def not_acceptable?; status == 406; end
def not_found?; status == 404; end
def not_found?; status == 404; end
def ok?; status == 200; end
def ok?; status == 200; end
def precondition_failed?; status == 412; end
def precondition_failed?; status == 412; end
def redirect?; [301, 302, 303, 307, 308].include? status; end
def redirect?; [301, 302, 303, 307, 308].include? status; end
def redirection?; status >= 300 && status < 400; end
def redirection?; status >= 300 && status < 400; end
def request_timeout?; status == 408; end
def request_timeout?; status == 408; end
def server_error?; status >= 500 && status < 600; end
def server_error?; status >= 500 && status < 600; end
def set_cookie(key, value)
def set_cookie(key, value) add_header SET_COOKIE, Utils.set_cookie_header(key, value) end
def set_cookie_header
def set_cookie_header get_header SET_COOKIE end
def set_cookie_header=(value)
def set_cookie_header=(value) set_header SET_COOKIE, value end
def successful?; status >= 200 && status < 300; end
def successful?; status >= 200 && status < 300; end
def unauthorized?; status == 401; end
def unauthorized?; status == 401; end
def unprocessable?; status == 422; end
def unprocessable?; status == 422; end