Class: Sinatra::Response

Inherits:
Rack::Response
  • Object
show all
Defined in:
lib/sinatra/base.rb

Overview

The response object. See Rack::Response and Rack::ResponseHelpers for more info: rack.rubyforge.org/doc/classes/Rack/Response.html rack.rubyforge.org/doc/classes/Rack/Response/Helpers.html

Instance Method Summary collapse

Constructor Details

#initializeResponse

Returns a new instance of Response.



35
36
37
38
# File 'lib/sinatra/base.rb', line 35

def initialize
  @status, @body = 200, []
  @header = Rack::Utils::HeaderHash.new({'Content-Type' => 'text/html'})
end

Instance Method Details

#finishObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sinatra/base.rb', line 45

def finish
  @body = block if block_given?
  if [204, 304].include?(status.to_i)
    header.delete "Content-Type"
    [status.to_i, header.to_hash, []]
  else
    body = @body || []
    body = [body] if body.respond_to? :to_str
    if header["Content-Length"].nil? && body.respond_to?(:to_ary)
      header["Content-Length"] = body.to_ary.
        inject(0) { |len, part| len + part.bytesize }.to_s
    end
    [status.to_i, header.to_hash, body]
  end
end

#write(str) ⇒ Object



40
41
42
43
# File 'lib/sinatra/base.rb', line 40

def write(str)
  @body << str.to_s
  str
end