Posts tagged with 'middleware':

Coderack, a Rack middleware contest launched


You’ve probably heard about Rack, right? Few months ago during the conversation with my teammate about potential and possibilities of Rack, and Rack middleware we’ve came up with an idea of creating some coding contest. We wanted to encourage Ruby developers to explore the power of Rack because we believe it’s the best thing since sliced bread. And what better way than to hold a contest? And now we can proudly say that it happened! We’ve just launched it and got really positive feedback about it so far.

But what is this CodeRack thingie all about? Like I’ve mentioned first goal is to show Rack’s power and encourage its use in Ruby web apps. The secondary goal of the contest is to generate a set of open source solutions that will solve real problems and inspire others. Every entry will be released under the MIT open source license.

Programmers are encouraged to submit contest entries that will be judged based on the cleverness of the application and the elegance of the code. Entries can be submitted at coderack.org until midnight CEST November 15th. Finalists are scheduled to be announced on the 1st of December and public voting will run for one month. The final winners will be announced on the 5th of January.

The first round of the contest will be judged by an elite panel of judges including Ben Bangert (Pylons framework), Chris Wanstrath and PJ Hyett of GitHub, Joshua Peek of 37Signals, Yehuda Katz of Engine Yard (also Rails core team member), Ryan Tomayko of Heroku, Core Rails team member Matt Aimonetti, and the Rails Envy team of Gregg Pollack and Jason Seifer. Once the finalists have been selected by the panel, the public will vote for the top prize winners.

Prizes have been donated by Bytemark Hosting, GitHub, Jetbrains, Mindmeister, Freelance Total, Heroku, Rackspace Hosting, Peepcode, BDDCasts, and Zenbe Shareflow. The top prize includes a dedicated quad core server package and is valued at over $3000. Every entrant will receive a credit from bddcasts.com and $30 credit from Heroku! All finalists will receive a package including Zenbe Shareflow subscriptions, a RubyMine license from JetBrains, and five credits from bddcasts.com. Details of all of the prize packages will soon be available on the contest website.

So, want to become a Rackstar? It’s easy, create some interesting code and submit it to the contest. We’ve already collected over 45 entries in just few days. Oh, and I’ve almost forgot, CodeRack site itself is using middlewares, one of them being Ryan Tomayko’s awesome Rack::Cache.

View Comments

LessCSS goodies


If you haven’t heard about LessCSS then check it out, really cool solution for writing css code in concise way. I’m planning to use it in some of my projects (and probably for this blog’s styles) and I’ve created two tools which could ease my work with LessCSS.

First tool, css2less, is CSS-to-LessCSS converter. Of course it can’t use all of LessCSS features because only programmer knows where to use variables and other stuff properly for specific project. However what it can do is to find all rules that can be nested and outputs them for given css file.

Grab it from github and use like that:

ruby css2less.rb my.css > my.less

You will get .less file with all styles from original .css file but with nested rules notation. It’s a good start for further tweaking, ie adding variables.

Second tool, rack-lesscss, is a Rack middleware which converts .less files into .css files on the fly during request. It’s main purpose is to ease development stage when you change your .less files frequently. With rack-lesscss middleware enabled you don’t need to compile .less files by hand after every change. LessCSS compiler has an option to watch for changes in .less file and automatically recompiles it but you need to remember to run compiler in watch mode for every stylesheet every time you start development session. There are also at least two Rails plugins which nicely integrates LessCSS into the app but this middleware can be used with Rails as well as with other ruby web frameworks like Merb or Sinatra.

Install gem:

sudo gem install --source http://gems.github.com sickill-rack-lesscss

or grab it from github.

Enable by adding following to your rackup file (config.ru, or config/rack.rb in Merb app):

require 'rack-lesscss'
use Rack::LessCss, :less_path => File.join(APP_ROOT, "public", "css"), :css_route => "/css"

Above code will enable on-the-fly .less files compilation for all requests matching /css/*.css.

It accepts two arguments:

  • :less_path – directory where source .less files are stored (required)
  • :css_route – route which this middleware will handle (optional, default is /stylesheets)

And don’t use it on production for obvious reason :) It’s meant to be used for development only.

View Comments

Rack middleware showing git or svn revision


Rack is fun. Really, lots of fun. After creating middleware for showing markup errors and viewing several presentations related to Rack I was thinking about Rack’s potential. And it’s big. The result of my thinking (and a little coding) is another middleware.

When you deploy application to a demo server QA (or client) wants to know which revision is currently running. Of course you can handle it in a old-school way putting some helper into your layout(s) which will obtain revision number exec’ing “svnversion” or sth like this. It works but it’s not the most elegant solution. First, you are including code related only to demo/staging server in layouts/helpers (production server and your development box don’t need it at all). Second, you probably do this for every new project. But all ruby web frameworks now run on Rack so better solution would be to move revision-displaying code from the app itself to the middleware. So here comes RevisionInfo.

Install:

gem install sickill-rack_revision_info --source http://gems.github.com

Enable in Merb:

config/dependencies.rb:

dependency "sickill-rack_revision_info", :require_as => "rack_revision_info"

config/rack.rb (before line with run Merb::Rack::Application.new):

use Rack::RevisionInfo, :path => Merb.root

Enable in Rails:

config/environment.rb:

config.gem "sickill-rack_revision_info", :lib => "rack_revision_info", :source => "http://gems.github.com"
config.middleware.use "Rack::RevisionInfo", :path => RAILS_ROOT

Enabling this middleware for svn managed application you will get <!-- Revision 666 (2009-05-28 19:00:25 +00:00) --> appended to the end of resulting html. For git repository it will be <!-- Revision 31d0fa132584c7e9bf978443052b545c1aeca96b (2009-05-28 19:00:25 +00:00) -->. It’s commented out so in order to see it look into page source.

However if you prefer to see revision number somewhere on the page you can specify CSS selector of page element and method of injection like this:

use Rack::RevisionInfo, :path => Merb.root, :inner_html => ".footer li.revision"

Here are available injection methods:

  • :append => "div.footer" inserts revision info at the end of footer div content
    <div class="footer"><img...> Revision 666 (2009-05-28 19:00:25 +00:00)</div>
  • :prepend => "div.footer" inserts revision info at the begining of footer div
    <div class="footer">Revision 666 (2009-05-28 19:00:25 +00:00) <img...></div>
  • :after => "div.footer" inserts revision info after footer div
    <div class="footer"><img...></div>Revision 666 (2009-05-28 19:00:25 +00:00)
  • :before => "div.footer" inserts revision info before footer div
    Revision 666 (2009-05-28 19:00:25 +00:00)<div class="footer"><img...></div>
  • :inner_html => "div.footer" replaces footer div content with revision info
    <div class="footer">Revision 666 (2009-05-28 19:00:25 +00:00)</div>
  • :swap => "div.footer" replaces whole footer div with revision info
    Revision 666 (2009-05-28 19:00:25 +00:00)

If you enable injection using one of above methods you need to have Hpricot gem installed because HTML manipulation is done using it (if you don’t use injection – only comment appending – you don’t need hpricot). Specified CSS selector can be any selector supported by hpricot so you can freely use funky stuff like #main ul.footer li:last. You can also use XPath like //div/ul/li.

Sources (as usual) are available on github.com. Enjoy!

View Comments

Rack middleware using HTML Tidy


Did you waste some time trying to spot the problem in html markup in your ruby web app? Are you strict validation freak? If yes, then read on.

I had several situations when something disappeared from the page or layout broke and the problem was in some missing closing tags or invalid nesting of elements. As I recently do my web development in Merb and Sinatra (this blog actually runs on Sinatra) and both of them are built on top of Rack I thought that I could write a middleware which can show me the problems in my markup.

So here it is. It uses well known Tidy tool to check for markup errors in actions output. Following code snippet shows the middleware class and its use in the simplest Sinatra app.

After running this tiny webapp and opening root url in your browser you will get following output in terminal:

It prints any warning and error messages to the console so you can easily spot any minor and major problems in the markup.

View Comments