# module Rack::Response::Helpers

### Public Instance Methods

accepted?() click to toggle source

    # File lib/rack/response.rb, line 145
    def accepted?;            status == 202;                        end

add_header(key, v) click to toggle source

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')

www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2

    # File lib/rack/response.rb, line 171
    def add_header(key, v)
      if v.nil?
        get_header key
      elsif has_header? key
        set_header key, "#{get_header key},#{v}"
      else
        set_header key, v
      end
    end

bad_request?() click to toggle source

    # File lib/rack/response.rb, line 148
    def bad_request?;         status == 400;                        end

cache!(duration = 3600, directive: "public") click to toggle source

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”.

    # File lib/rack/response.rb, line 246
    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

cache_control() click to toggle source

    # File lib/rack/response.rb, line 229
    def cache_control
      get_header CACHE_CONTROL
    end

cache_control=(v) click to toggle source

    # File lib/rack/response.rb, line 233
    def cache_control=(v)
      set_header CACHE_CONTROL, v
    end

client_error?() click to toggle source

    # File lib/rack/response.rb, line 140
    def client_error?;        status >= 400 && status < 500;        end

content_length() click to toggle source

    # File lib/rack/response.rb, line 199
    def content_length
      cl = get_header CONTENT_LENGTH
      cl ? cl.to_i : cl
    end

content_type() click to toggle source

Get the content type of the response.

    # File lib/rack/response.rb, line 182
    def content_type
      get_header CONTENT_TYPE
    end

content_type=(content_type) click to toggle source

Set the content type of the response.

    # File lib/rack/response.rb, line 187
    def content_type=(content_type)
      set_header CONTENT_TYPE, content_type
    end

created?() click to toggle source

    # File lib/rack/response.rb, line 144
    def created?;             status == 201;                        end

delete_cookie(key, value = {}) click to toggle source

    # File lib/rack/response.rb, line 217
    def delete_cookie(key, value = {})
      set_header SET_COOKIE, ::Rack::Utils.add_remove_cookie_to_header(get_header(SET_COOKIE), key, value)
    end

do_not_cache!() click to toggle source

Specifies that the content shouldn't be cached. Overrides `cache!` if already called.

    # File lib/rack/response.rb, line 238
    def do_not_cache!
      set_header CACHE_CONTROL, "no-cache, must-revalidate"
      set_header EXPIRES, Time.now.httpdate
    end

etag() click to toggle source

    # File lib/rack/response.rb, line 253
    def etag
      get_header ETAG
    end

etag=(v) click to toggle source

    # File lib/rack/response.rb, line 257
    def etag=(v)
      set_header ETAG, v
    end

forbidden?() click to toggle source

    # File lib/rack/response.rb, line 150
    def forbidden?;           status == 403;                        end

include?(header) click to toggle source

    # File lib/rack/response.rb, line 158
    def include?(header)
      has_header? header
    end

informational?() click to toggle source

    # File lib/rack/response.rb, line 137
    def informational?;       status >= 100 && status < 200;        end

invalid?() click to toggle source

    # File lib/rack/response.rb, line 135
    def invalid?;             status < 100 || status >= 600;        end

location() click to toggle source

    # File lib/rack/response.rb, line 204
    def location
      get_header "Location"
    end

location=(location) click to toggle source

    # File lib/rack/response.rb, line 208
    def location=(location)
      set_header "Location", location
    end

media_type() click to toggle source

    # File lib/rack/response.rb, line 191
    def media_type
      MediaType.type(content_type)
    end

media_type_params() click to toggle source

    # File lib/rack/response.rb, line 195
    def media_type_params
      MediaType.params(content_type)
    end

method_not_allowed?() click to toggle source

    # File lib/rack/response.rb, line 152
    def method_not_allowed?;  status == 405;                        end

moved_permanently?() click to toggle source

    # File lib/rack/response.rb, line 147
    def moved_permanently?;   status == 301;                        end

no_content?() click to toggle source

    # File lib/rack/response.rb, line 146
    def no_content?;          status == 204;                        end

not_found?() click to toggle source

    # File lib/rack/response.rb, line 151
    def not_found?;           status == 404;                        end

ok?() click to toggle source

    # File lib/rack/response.rb, line 143
    def ok?;                  status == 200;                        end

precondition_failed?() click to toggle source

    # File lib/rack/response.rb, line 153
    def precondition_failed?; status == 412;                        end

redirect?() click to toggle source

    # File lib/rack/response.rb, line 156
    def redirect?;            [301, 302, 303, 307, 308].include? status; end

redirection?() click to toggle source

    # File lib/rack/response.rb, line 139
    def redirection?;         status >= 300 && status < 400;        end

server_error?() click to toggle source

    # File lib/rack/response.rb, line 141
    def server_error?;        status >= 500 && status < 600;        end

set_cookie(key, value) click to toggle source

    # File lib/rack/response.rb, line 212
    def set_cookie(key, value)
      cookie_header = get_header SET_COOKIE
      set_header SET_COOKIE, ::Rack::Utils.add_cookie_to_header(cookie_header, key, value)
    end

set_cookie_header() click to toggle source

    # File lib/rack/response.rb, line 221
    def set_cookie_header
      get_header SET_COOKIE
    end

set_cookie_header=(v) click to toggle source

    # File lib/rack/response.rb, line 225
    def set_cookie_header=(v)
      set_header SET_COOKIE, v
    end

successful?() click to toggle source

    # File lib/rack/response.rb, line 138
    def successful?;          status >= 200 && status < 300;        end

unauthorized?() click to toggle source

    # File lib/rack/response.rb, line 149
    def unauthorized?;        status == 401;                        end

unprocessable?() click to toggle source

    # File lib/rack/response.rb, line 154
    def unprocessable?;       status == 422;                        end

### Protected Instance Methods

append(chunk) click to toggle source

    # File lib/rack/response.rb, line 287
    def append(chunk)
      @body << chunk

      unless chunked?
        @length += chunk.bytesize
        set_header(CONTENT_LENGTH, @length.to_s)
      end

      return chunk
    end

buffered_body!() click to toggle source

    # File lib/rack/response.rb, line 263
    def buffered_body!
      return if @buffered

      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
      else
        # 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)
      end

      @buffered = true
    end