Class: Rack::Response

### Home Classes Methods

### In Files

- lib/rack/response.rb

### Parent

Object

### Namespace

- MODULE Rack::Response::Helpers

### Methods

- ::new
- #[]
- #[]=
- #close
- #delete_cookie
- #each
- #empty?
- #finish
- #redirect
- #set_cookie
- #to_a
- #write

### Included Modules

- Rack::Response::Helpers

### Files

- KNOWN-ISSUES
- README
- SPEC

### Class/Module Index

Quicksearch

- Rack
- Rack::Auth
- Rack::Auth::AbstractHandler
- Rack::Auth::AbstractRequest
- Rack::Auth::Basic
- Rack::Auth::Basic::Request
- Rack::Auth::Digest
- Rack::Auth::Digest::MD5
- Rack::Auth::Digest::Nonce
- Rack::Auth::Digest::Params
- Rack::Auth::Digest::Request
- Rack::Builder
- Rack::Cascade
- Rack::Chunked
- Rack::CommonLogger
- Rack::ConditionalGet
- Rack::Config
- Rack::ContentLength
- Rack::ContentType
- Rack::Deflater
- Rack::Deflater::DeflateStream
- Rack::Deflater::GzipStream
- Rack::Directory
- Rack::ETag
- Rack::File
- Rack::ForwardRequest
- Rack::Handler
- Rack::Handler::CGI
- Rack::Handler::EventedMongrel
- Rack::Handler::FastCGI
- Rack::Handler::LSWS
- Rack::Handler::Mongrel
- Rack::Handler::SCGI
- Rack::Handler::SwiftipliedMongrel
- Rack::Handler::Thin
- Rack::Handler::WEBrick
- Rack::Head
- Rack::Lint
- Rack::Lobster
- Rack::Lock
- Rack::Logger
- Rack::MethodOverride
- Rack::Mime
- Rack::MockRequest
- Rack::MockRequest::FatalWarner
- Rack::MockRequest::FatalWarning
- Rack::MockResponse
- Rack::NullLogger
- Rack::Recursive
- Rack::Reloader
- Rack::Reloader::Stat
- Rack::Request
- Rack::Response
- Rack::Response::Helpers
- Rack::RewindableInput
- Rack::RewindableInput::Tempfile
- Rack::Runtime
- Rack::Sendfile
- Rack::Server
- Rack::Server::Options
- Rack::Session
- Rack::Session::Abstract
- Rack::Session::Abstract::ID
- Rack::Session::Cookie
- Rack::Session::Memcache
- Rack::Session::Pool
- Rack::ShowExceptions
- Rack::ShowStatus
- Rack::Static
- Rack::URLMap
- Rack::Utils
- Rack::Utils::Context
- Rack::Utils::HeaderHash
- Rack::Utils::Multipart
- Rack::Utils::Multipart::UploadedFile
- FCGI
- FCGI::Stream

No matching classes.

# Rack::Response

Rack::Response provides a convenient interface to create a Rack response.

It allows setting of headers and cookies, and provides useful defaults (a OK response containing HTML).

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 syncronous with the Rack response.

Your application’s `call` should end returning Response#finish.

### Attributes

body[RW]
header[R]
headers[R]
length[RW]
status[RW]

### Public Class Methods

new(body=[], status=200, header={}, &block) click to toggle source

    # File lib/rack/response.rb, line 22
    def initialize(body=[], status=200, header={}, &block)
      @status = status.to_i
      @header = Utils::HeaderHash.new({"Content-Type" => "text/html"}.
                                      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

### Public Instance Methods

[](key) click to toggle source

    # File lib/rack/response.rb, line 49
    def [](key)
      header[key]
    end

[]=(key, value) click to toggle source

    # File lib/rack/response.rb, line 53
    def []=(key, value)
      header[key] = value
    end

close() click to toggle source

    # File lib/rack/response.rb, line 101
    def close
      body.close if body.respond_to?(:close)
    end

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

    # File lib/rack/response.rb, line 61
    def delete_cookie(key, value={})
      Utils.delete_cookie_header!(header, key, value)
    end

each(&callback) click to toggle source

    # File lib/rack/response.rb, line 82
    def each(&callback)
      @body.each(&callback)
      @writer = callback
      @block.call(self)  if @block
    end

empty?() click to toggle source

    # File lib/rack/response.rb, line 105
    def empty?
      @block == nil && @body.empty?
    end

finish(&block) click to toggle source

    # File lib/rack/response.rb, line 70
    def finish(&block)
      @block = block

      if [204, 304].include?(status.to_i)
        header.delete "Content-Type"
        [status.to_i, header, []]
      else
        [status.to_i, header, self]
      end
    end

Also aliased as: to_a
redirect(target, status=302) click to toggle source

    # File lib/rack/response.rb, line 65
    def redirect(target, status=302)
      self.status = status
      self["Location"] = target
    end

set_cookie(key, value) click to toggle source

    # File lib/rack/response.rb, line 57
    def set_cookie(key, value)
      Utils.set_cookie_header!(header, key, value)
    end

to_a(&block) click to toggle source
Alias for: finish
write(str) click to toggle source

Append to body and update Content-Length.

NOTE: Do not mix write and direct body access!

    # File lib/rack/response.rb, line 92
    def write(str)
      s = str.to_s
      @length += Rack::Utils.bytesize(s)
      @writer.call s

      header["Content-Length"] = @length.to_s
      str
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.