Module: Merb::ControllerMixin
Module that is mixed in to all implemented controllers.
Public Instance Methods
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.
# File lib/merb/mixins/controller.rb, line 154 154: def delete_cookie(name) 155: set_cookie(name, nil, Merb::Const::COOKIE_EXPIRED_TIME) 156: end
nginx_send_file (file)
Uses the nginx specific +X-Accel-Redirect+ header to send a file directly from nginx. For more information, see the nginx wiki: wiki.codemongers.com/NginxXSendfile
Parameters
file - Path to file to send to the client.
# File lib/merb/mixins/controller.rb, line 130 130: def nginx_send_file(file) 131: headers['X-Accel-Redirect'] = File.expand_path(file) 132: return 133: end
redirect (url)
Redirects to a URL. The url parameter can be either a relative URL (e.g., +/posts/34+) or a fully-qualified URL (e.g., +www.merbivore.com/+).
Parameters
url - URL to redirect to; it can be either a relative or fully-qualified URL.
# File lib/merb/mixins/controller.rb, line 68 68: def redirect(url) 69: Merb.logger.info("Redirecting to: #{url}") 70: set_status(302) 71: headers['Location'] = url 72: "<html><body>You are being <a href=\"#{url}\">redirected</a>.</body></html>" 73: end
render_chunked (&blk)
Renders the block given as a parameter using chunked encoding.
Examples
def stream
prefix = '<p>'
suffix = "</p>\r\n"
render_chunked do
IO.popen("cat /tmp/test.log") do |io|
done = false
until done
sleep 0.3
line = io.gets.chomp
if line == 'EOF'
done = true
else
send_chunk(prefix + line + suffix)
end
end
end
end
end
# File lib/merb/mixins/controller.rb, line 30 30: def render_chunked(&blk) 31: headers['Transfer-Encoding'] = 'chunked' 32: Proc.new { 33: response.send_status_no_connection_close(0) 34: response.send_header 35: blk.call 36: response.write("0\r\n\r\n") 37: } 38: end
render_deferred (&blk)
Returns a Proc that Mongrel can call later, allowing Merb to release the thread lock and render another request.
# File lib/merb/mixins/controller.rb, line 43 43: def render_deferred(&blk) 44: Proc.new { 45: result = blk.call 46: response.send_status(result.length) 47: response.send_header 48: response.write(result) 49: } 50: end
send_chunk (data)
Writes a chunk from render_chunked to the response that is sent back to the client.
# File lib/merb/mixins/controller.rb, line 54 54: def send_chunk(data) 55: response.write('%x' % data.size + "\r\n") 56: response.write(data + "\r\n") 57: end
send_file (file, opts={})
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.
Parameters
file - Path to file to send to the client.
# File lib/merb/mixins/controller.rb, line 82 82: def send_file(file, opts={}) 83: opts.update(Merb::Const::DEFAULT_SEND_FILE_OPTIONS.merge(opts)) 84: disposition = opts[:disposition].dup || 'attachment' 85: disposition << %(; filename="#{opts[:filename] ? opts[:filename] : File.basename(file)}") 86: headers.update( 87: 'Content-Type' => opts[:type].strip, # fixes a problem with extra '\r' with some browsers 88: 'Content-Disposition' => disposition, 89: 'Content-Transfer-Encoding' => 'binary', 90: 'X-SENDFILE' => file 91: ) 92: return 93: end
set_cookie (name, value, expires)
Sets a cookie to be included in the response. This method is used primarily internally in Merb.
If you need to set a cookie, then use the cookies hash.
# File lib/merb/mixins/controller.rb, line 140 140: def set_cookie(name, value, expires) 141: (headers['Set-Cookie'] ||='') << (Merb::Const::SET_COOKIE % [ 142: name.to_s, 143: escape(value.to_s), 144: # Cookie expiration time must be GMT. See RFC 2109 145: expires.gmtime.strftime(Merb::Const::COOKIE_EXPIRATION_FORMAT) 146: ]) 147: end
stream_file (opts={}, &stream)
Streams a file over HTTP.
Example
stream_file( { :filename => file_name,
:type => content_type,
:content_length => content_length }) do
AWS::S3::S3Object.stream(user.folder_name + "-" + user_file.unique_id, bucket_name) do |chunk|
response.write chunk
end
end
# File lib/merb/mixins/controller.rb, line 106 106: def stream_file(opts={}, &stream) 107: opts.update(Merb::Const::DEFAULT_SEND_FILE_OPTIONS.merge(opts)) 108: disposition = opts[:disposition].dup || 'attachment' 109: disposition << %(; filename="#{opts[:filename]}") 110: response.headers.update( 111: 'Content-Type' => opts[:type].strip, # fixes a problem with extra '\r' with some browsers 112: 'Content-Disposition' => disposition, 113: 'Content-Transfer-Encoding' => 'binary', 114: 'CONTENT-LENGTH' => opts[:content_length] 115: ) 116: response.send_status(opts[:content_length]) 117: response.send_header 118: stream 119: end