YoWhat is Yo?Yo is a library for easily writing web interfaces to Ruby stuff. It's a bit like CGI:
require 'Yo'
Yo.page("first") {Yo.print "<h1>Hello World!</h1>" }
Yo.start
This is a complete webapp. You can put it into a file and run it, then open http://localhost/first . No web server is needed: the call to Yo.start starts up its own WEBRick server on port 80, and hangs. See, Yo is trying to be simple. And that's all? Sounds a bit disappointing.No. There's more. Here's a working guestbook webapp:
require 'Yo'
entries = []
Yo.page("view") do
Yo.print( entries.join("<p>") + "<p><a href='sign'>Sign the guestbook</a>" )
end
Yo.page("sign") do
url = Yo.action_url {|h| entries << h["text"]; Yo.redirect_to "view" }
Yo.print "<form action='#{url}'><textarea name='text' rows=5 cols=40></textarea>
<input type='submit' value='Submit'/></form>"
end
Yo.start
There's a bit more going on here. The contents of the guestbook are kept in an array - this is a major difference from CGI, where you would have to keep it on disk, synchronize access to files etc. Pages in Yo share a common execution context - this is a win. However, even this isn't the major point of Yo. Note that whenever the "sign" page is rendered, a callback is registered for the form's action URL. For the programmer, this means that he/she can specify how to handle user input, in the same place where the form is defined. OK, that was a bit too complex, let's go at it in stages. Look at this line again:
url = Yo.action_url {|h| entries << h["text"]; Yo.redirect_to "view" }
After this line, the "url" variable holds some meaningless, random URL, created by the system. This URL doesn't point to a specific webpage - it points to a callback. When the callback is executed (which means that the user clicked "submit" on the form), the hash of form parameters gets passed to the callback (as h). The callback adds h["text"] (which corresponds to a textarea in the form) to the guestbook array, and sends the user an HTTP redirect - which means that the user will instantly be forwarded to the "view" page again. And another fun thing that we get for free: if the user hits Refresh right after adding a guestbook entry, the entry won't be added a second time - because we've used the redirect trick. Naturally, the callback may opt to not return a redirect response, but rather to render another HTML page, register new callbacks there, etc. Actually, the context inside the callback is no different from the context inside a Yo.page block. Which means that we can tie callbacks to hyperlinks, not just form action URLs. Also, this means that we can take advantage of the CGI param hash in regular pages:
Yo.page("echo") do |h|
h.each {|k,v| Yo.print "#{k} : #{v} <br>"}
end
This page (http://localhost/echo) just echoes all CGI parameters as name : value pairs. What else? How about sessions?There's not much else. If you want sessions, you'll have to do your own keying, bookkeeping and expiry; thankfully, this is much easier when we have a common execution context and Yoshinori K. Okuji's Cache module (a wonderful library which is included in Yo, require 'Yo/Utils/Cache'). There's also much more you can do, like communication between users etc. If, by chance, you happen to need a unique key, just call Yo.new_key . Actually you can do surprising things with what's already there: for example, you can chain some pages in a sequence - just like Seaside or Borges fans like to. But Yo doesn't use continuations - you'll need to use CPS tricks to chain stuff together. However, unlike Seaside or Borges, bookmarking inside a Yo app will Just Work most of the time. A rule of thumb is: "common CGI parameters" describe what information to show, and "action parameters" describe actions to be performed. This way, hopefully, the number of dynamically-generated URLs in your webapp can be kept at a minimum. Yo is currently at an alpha stage, which means that the user can't yet mess with response headers, request headers, path_info and stuff of the sort. I promise to be good and fix it, as soon as I see how to make it easy and clean. If you get really angered, just email me! Well, where to get Yo?Here on RubyForge, from the filelist.Who wrote Yo?Me, Vladimir Slepnev (slepnev_v [at] rambler [dot] ru). I'd like to hear from you. |