module Rack::Response::Helpers
Public Instance Methods
Source
# File lib/rack/response.rb, line 218 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
Add a header that may have multiple values.
Example:
response.add_header 'vary', 'accept-encoding' response.add_header 'vary', 'cookie' assert_equal 'accept-encoding,cookie', response.get_header('vary')
Source
# File lib/rack/response.rb, line 306 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
Specify that the content should be cached. @param duration [Integer] The number of seconds until the cache expires. @option directive [String] The cache control directive, one of “public”, “private”, “no-cache” or “no-store”.
Source
# File lib/rack/response.rb, line 289 def cache_control get_header CACHE_CONTROL end
Source
# File lib/rack/response.rb, line 293 def cache_control=(value) set_header CACHE_CONTROL, value end
Source
# File lib/rack/response.rb, line 185 def client_error?; status >= 400 && status < 500; end
Source
# File lib/rack/response.rb, line 256 def content_length cl = get_header CONTENT_LENGTH cl ? cl.to_i : cl end
Source
# File lib/rack/response.rb, line 239 def content_type get_header CONTENT_TYPE end
Get the content type of the response.
Source
# File lib/rack/response.rb, line 244 def content_type=(content_type) set_header CONTENT_TYPE, content_type end
Set the content type of the response.
Source
# File lib/rack/response.rb, line 298 def do_not_cache! set_header CACHE_CONTROL, "no-cache, must-revalidate" set_header EXPIRES, Time.now.httpdate end
Specifies that the content shouldn’t be cached. Overrides ‘cache!` if already called.
Source
# File lib/rack/response.rb, line 317 def etag=(value) set_header ETAG, value end
Source
# File lib/rack/response.rb, line 205 def include?(header) has_header?(header) end
Source
# File lib/rack/response.rb, line 182 def informational?; status >= 100 && status < 200; end
Source
# File lib/rack/response.rb, line 180 def invalid?; status < 100 || status >= 600; end
Source
# File lib/rack/response.rb, line 265 def location=(location) set_header "location", location end
Source
# File lib/rack/response.rb, line 248 def media_type MediaType.type(content_type) end
Source
# File lib/rack/response.rb, line 252 def media_type_params MediaType.params(content_type) end
Source
# File lib/rack/response.rb, line 197 def method_not_allowed?; status == 405; end
Source
# File lib/rack/response.rb, line 192 def moved_permanently?; status == 301; end
Source
# File lib/rack/response.rb, line 198 def not_acceptable?; status == 406; end
Source
# File lib/rack/response.rb, line 200 def precondition_failed?; status == 412; end
Source
# File lib/rack/response.rb, line 203 def redirect?; [301, 302, 303, 307, 308].include? status; end
Source
# File lib/rack/response.rb, line 184 def redirection?; status >= 300 && status < 400; end
Source
# File lib/rack/response.rb, line 199 def request_timeout?; status == 408; end
Source
# File lib/rack/response.rb, line 186 def server_error?; status >= 500 && status < 600; end
Source
# File lib/rack/response.rb, line 183 def successful?; status >= 200 && status < 300; end
Source
# File lib/rack/response.rb, line 201 def unprocessable?; status == 422; end
Protected Instance Methods
Source
# File lib/rack/response.rb, line 353 def append(chunk) @body << chunk unless chunked? @length += chunk.bytesize set_header(CONTENT_LENGTH, @length.to_s) end return chunk end
Source
# File lib/rack/response.rb, line 323 def buffered_body! if @buffered.nil? if @body.is_a?(Array) # The user supplied body was an array: @body = @body.compact @body.each do |part| @length += part.to_s.bytesize end @buffered = true elsif @body.respond_to?(:each) # Turn the user supplied body into a buffered array: body = @body @body = Array.new body.each do |part| @writer.call(part.to_s) end body.close if body.respond_to?(:close) @buffered = true else @buffered = false end end return @buffered end