Module: Merb::ControllerMixin
- Included in:
- Controller
- Defined in:
- merb-core/lib/merb-core/controller/mixins/controller.rb
Overview
Module that is mixed in to all implemented controllers.
Instance Method Summary (collapse)
-
- (Object) delete_cookie(name)
Marks a cookie as deleted and gives it an expires stamp in the past.
-
- (String) escape_xml(obj)
(also: #h, #escape_html)
Escapes the string representation of
objand escapes it for use in XML. -
- (Object) message
Retreives the redirect message either locally or from the request.
-
- (String) nginx_send_file(path, content_type = "")
Uses the nginx specific
X-Accel-Redirectheader to send a file directly from nginx. -
- (String) redirect(url, opts = {})
Explanation of redirect.
-
- (Object) render_chunked(&blk)
Renders the block given as a parameter using chunked encoding.
-
- (Proc) render_deferred(&blk)
A block that the server can call later, allowing Merb to release the thread lock and render another request.
-
- (Proc) render_then_call(str, &blk)
Renders the passed in string, then calls the block outside the mutex and after the string has been returned to the client.
-
- (Object) run_later(&blk)
Queue a block to run in a background thread outside of the request response dispatch.
-
- (Object) send_chunk(data)
Writes a chunk from #render_chunked to the response that is sent back to the client.
-
- (String) send_data(data, opts = {})
Send binary data over HTTP to the user as a file download.
-
- (IO) send_file(file, opts = {})
Sends a file over HTTP.
-
- (Object) set_cookie(name, value, expires)
Sets a cookie to be included in the response.
-
- (Object) stream_file(opts = {}, &stream)
Streams a file over HTTP.
Instance Method Details
- (Object) delete_cookie(name)
Marks a cookie as deleted and gives it an expires stamp in the past.
This method is used primarily internally in Merb. Use the cookies
hash to manipulate cookies instead.
307 308 309 |
# File 'merb-core/lib/merb-core/controller/mixins/controller.rb', line 307 def (name) (name, nil, Merb::Const::COOKIE_EXPIRED_TIME) end |
- (String) escape_xml(obj) Also known as: h, escape_html
Escapes the string representation of obj and escapes it for use in XML.
318 319 320 |
# File 'merb-core/lib/merb-core/controller/mixins/controller.rb', line 318 def escape_xml(obj) Merb::Parse.escape_xml(obj.to_s) end |
- (Object) message
Retreives the redirect message either locally or from the request.
149 150 151 |
# File 'merb-core/lib/merb-core/controller/mixins/controller.rb', line 149 def @_message = defined?(@_message) ? @_message : request. end |
- (String) nginx_send_file(path, content_type = "")
Uses the nginx specific X-Accel-Redirect header to send a file directly
from nginx.
Unless Content-Disposition is set before calling this method, it is set to attachment with streamed file name.
For more information, see:
- The nginx wiki
- A sample gist
- An example application on GitHub
274 275 276 277 278 279 280 281 282 |
# File 'merb-core/lib/merb-core/controller/mixins/controller.rb', line 274 def nginx_send_file(path, content_type = "") # Let Nginx detect content type unless it is explicitly set headers['Content-Type'] = content_type headers["Content-Disposition"] ||= "attachment; filename=#{path.split('/').last}" headers['X-Accel-Redirect'] = path return ' ' end |
- (String) redirect(url, opts = {})
Explanation of redirect.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'merb-core/lib/merb-core/controller/mixins/controller.rb', line 131 def redirect(url, opts = {}) = { :message => nil, :permanent => false } opts = .merge(opts) url = (url,opts) _status = opts[:status] if opts[:status] _status ||= opts[:permanent] ? 301 : 302 self.status = _status Merb.logger.info("Redirecting to: #{url} (#{self.status})") headers['Location'] = url "<html><body>You are being <a href=\"#{url}\">redirected</a>.</body></html>" end |
- (Object) render_chunked(&blk)
Renders the block given as a parameter using chunked encoding.
48 49 50 51 52 53 54 55 56 57 58 |
# File 'merb-core/lib/merb-core/controller/mixins/controller.rb', line 48 def render_chunked(&blk) must_support_streaming! headers['Transfer-Encoding'] = 'chunked' Proc.new { |response| @response = response response.send_status_no_connection_close('') response.send_header blk.call response.write("0\r\n\r\n") } end |
- (Proc) render_deferred(&blk)
A block that the server can call later, allowing Merb to release the thread lock and render another request.
79 80 81 82 83 |
# File 'merb-core/lib/merb-core/controller/mixins/controller.rb', line 79 def render_deferred(&blk) Proc.new do |response| response.write(blk.call) end end |
- (Proc) render_then_call(str, &blk)
Renders the passed in string, then calls the block outside the mutex and after the string has been returned to the client.
96 97 98 99 100 101 |
# File 'merb-core/lib/merb-core/controller/mixins/controller.rb', line 96 def render_then_call(str, &blk) Proc.new do |response| response.write(str) blk.call end end |
- (Object) run_later(&blk)
Queue a block to run in a background thread outside of the request response dispatch.
16 17 18 |
# File 'merb-core/lib/merb-core/controller/mixins/controller.rb', line 16 def run_later(&blk) Merb.run_later(&blk) end |
- (Object) send_chunk(data)
Writes a chunk from #render_chunked to the response that is sent back to
the client. This should only be called within a render_chunked block.
66 67 68 69 70 |
# File 'merb-core/lib/merb-core/controller/mixins/controller.rb', line 66 def send_chunk(data) only_runs_on_mongrel! @response.write('%x' % data.size + "\r\n") @response.write(data + "\r\n") end |
- (String) send_data(data, opts = {})
Send binary data over HTTP to the user as a file download.
May set content type, apparent file name, and specify whether to show data inline or download as an attachment.
204 205 206 207 208 209 210 211 212 213 214 |
# File 'merb-core/lib/merb-core/controller/mixins/controller.rb', line 204 def send_data(data, opts={}) opts.update(Merb::Const::DEFAULT_SEND_FILE_OPTIONS.merge(opts)) disposition = opts[:disposition].dup || 'attachment' disposition << %(; filename="#{opts[:filename]}") if opts[:filename] headers.update( 'Content-Type' => opts[:type].strip, # fixes a problem with extra '\r' with some browsers 'Content-Disposition' => disposition, 'Content-Transfer-Encoding' => 'binary' ) data end |
- (IO) send_file(file, opts = {})
Docs, correctness: is the return type correct?
Sends a file over HTTP. When given a path to a file, it will set the right headers so that the static file is served directly.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'merb-core/lib/merb-core/controller/mixins/controller.rb', line 169 def send_file(file, opts={}) opts.update(Merb::Const::DEFAULT_SEND_FILE_OPTIONS.merge(opts)) disposition = opts[:disposition].dup || 'attachment' disposition << %(; filename="#{opts[:filename] ? opts[:filename] : File.basename(file)}") headers.update( 'Content-Type' => opts[:type].strip, # fixes a problem with extra '\r' with some browsers 'Content-Disposition' => disposition, 'Content-Transfer-Encoding' => 'binary' ) Proc.new do |response| file = File.open(file, 'rb') while chunk = file.read(16384) response.write chunk end file.close end end |
- (Object) set_cookie(name, value, expires)
Sets a cookie to be included in the response.
If you need to set a cookie, then use the cookies hash.
294 295 296 297 |
# File 'merb-core/lib/merb-core/controller/mixins/controller.rb', line 294 def (name, value, expires) = expires.is_a?(Hash) ? expires : {:expires => expires} .(name, value, ) end |
- (Object) stream_file(opts = {}, &stream)
Streams a file over HTTP.
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'merb-core/lib/merb-core/controller/mixins/controller.rb', line 239 def stream_file(opts={}, &stream) opts.update(Merb::Const::DEFAULT_SEND_FILE_OPTIONS.merge(opts)) disposition = opts[:disposition].dup || 'attachment' disposition << %(; filename="#{opts[:filename]}") headers.update( 'Content-Type' => opts[:type].strip, # fixes a problem with extra '\r' with some browsers 'Content-Disposition' => disposition, 'Content-Transfer-Encoding' => 'binary', # Rack specification requires header values to respond to :each 'CONTENT-LENGTH' => opts[:content_length].to_s ) Proc.new do |response| stream.call(response) end end |