MVC with ruby-web

You can get MVC action by combining two features of ruby-web. Before loading a page, ruby-web looks up the directory tree for an "application.rb" file. Then, in the application.rb file, you can define a Web::filter [1].

Let's use a basic hello.world.rb as an example:

#!/usr/bin/env ruby-web
puts "Hello World"

When you visit this cgi script, you'll get "Hello World". If you add this application.rb in the same directory as hello.world.rb:

Web::filter do |content|
"<h1>#{content}</h1>"
end

Visit the script again, and notice that Hello World is a bit louder. You can also have multiple chained filters:

Web::filter do |content|
"<h1>#{content}</h1>"
end

Web::filter do |content|
content.gsub(/Hello World/, "Hallo Welt")
end

Take that, Servlet API!


A few notes:

1. If you are running ruby-web 1.0.0, Web::ob_start is a more cryptic name for the exact same function. If you are running php, ob_start is also your friend :-)

2. I'm sorry that ruby-web scripts still run as basic cgi scripts. Alas, I have not yet written enough SAPI modules. One day I'll get them all done.


Comments:
Are more method names with the same effect a good thing?

perhaps just stick with Web.filter, and document the transition from PHP better, rather than having ob_start too. (Because then where's the ob_end_flush? And down that path lies madness...)
 
Actually, the first thing I did was implement ob_*.

Web#filter is the fun api, though.
 
This post has been removed by a blog administrator.
 
Is Ruby-Web still being actively developed? I've searched everywhere for a tool that would allow me to capture the Ruby Rails WEBrick server's output and save it to files.

The Oct '05 release of Ruby-Web indicated that Rails integration was on the things to do list. Will that be in the near future?

Anyway, I took a look at the instructions given in the Ruby-Web installation manual. Then I attempt to integrate the part for starting WEBrick with Ruby-Web into the webrick_server.rb module on my RadRails installation, but it didn't appear to work because I wasn't capturing any data.

Anyone have any advice on what I could try in the short term?
 
I'll answer in a few parts...

1. Is ruby-web being actively developed?

Ruby-web is being passively developed. I have a new job, which has sapped much of my time. However, I am using ruby-web at my new job, so I have been fixing annoyances with ruby-web. The main delay is that handling the intel macs have slowed down my release structure, and I haven't had the time yet to fix it.

2. Will rails integration happen in the near future?

Integration with rails has been delayed. There is a cgi replacement:

require 'web/shim/cgi'

This should allow one to run cgi.rb scripts, which might work with Rails. But Rails has some complicated requirements. The rails codebase also overlaps with ruby-web quite a bit, as it provides itself with its own cgi / web methods.

Intead of focusing "up" the stack with rails, I am now focusing "down" the stack on doing a better job integrating fastcgi / webrick / mod_ruby. There's work to be done there, and that direction is ruby-web's main focus anyways.

3. What can be done short term?

I would recommend playing with $stdout or $defout. Also, if you look at the implementation of webrick, you should be able to find the hook to save output into files.

Apologies for not having a easy answer, thanks for asking though.

Cheers,

patrick
 
Thanks for the quick responses.

I did try directlty redirecting $stdout and $defout, although $defout has been obsoleted in Ruby. In either case it still didn't seem to work.

It appears the server's http output is traveling on some other stream.

I also tried inserting

require 'web/sapi/webrick'
HTTPServlet::FileHandler.add_handler("rb", Web::RubyWebHandler)
HTTPServlet::FileHandler.add_handler("rhtml", Web::RubyWebHandler)

statements in the Ruby Rails server startup script, but I don't understand the activation sequence between those systems to know if I'm doing it correctly.

I may find some time later to look into how the server works. For now, I will have to fall back to accomplishing my tasks with a series of PHP and Ruby scripts, instead of just Ruby.

Thanks again.
 
Post a Comment

<< Home

~ Patrick May