Class: Sinatra::Response

Inherits:
Rack::Response show all
Defined in:
lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb

Overview

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

Constant Summary collapse

DROP_BODY_RESPONSES =
[204, 205, 304]

Instance Attribute Summary

Attributes inherited from Rack::Response

#body, #header, #length, #status

Attributes included from Rack::Response::Helpers

#headers, #original_headers

Instance Method Summary collapse

Methods inherited from Rack::Response

#[], #[]=, #close, #delete_cookie, #empty?, #redirect, #set_cookie, #write

Methods included from Rack::Response::Helpers

#bad_request?, #client_error?, #content_length, #content_type, #forbidden?, #include?, #informational?, #invalid?, #location, #method_not_allowed?, #not_found?, #ok?, #redirect?, #redirection?, #server_error?, #successful?, #unprocessable?

Constructor Details

#initializeResponse

Returns a new instance of Response.



122
123
124
125
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 122

def initialize(*)
  super
  headers['Content-Type'] ||= 'text/html'
end

Instance Method Details

#body=(value) ⇒ Object



127
128
129
130
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 127

def body=(value)
  value = value.body while Rack::Response === value
  @body = String === value ? [value.to_str] : value
end

#eachObject



132
133
134
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 132

def each
  block_given? ? super : enum_for(:each)
end

#finishObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/vendor/sinatra-1.4.4/lib/sinatra/base.rb', line 136

def finish
  result = body

  if 
    headers.delete "Content-Length"
    headers.delete "Content-Type"
  end

  if drop_body?
    close
    result = []
  end

  if calculate_content_length?
    # if some other code has already set Content-Length, don't muck with it
    # currently, this would be the static file-handler
    headers["Content-Length"] = body.inject(0) { |l, p| l + Rack::Utils.bytesize(p) }.to_s
  end

  [status.to_i, headers, result]
end