Middleware is a very powerful tool that is usually used to filter incoming requests or outgoing responses to your web application, and solves some problems in a very elegant and DRY way. It is usually a pluggable component acting as a filter in the request/response flow. Has the benefit of being easy to reuse in any application that needs them.
WSGI is an interface, that defines how web applications and web servers talk to each other. WSGI is the name most used with connection to the Python programming language, but WSGI has equivalents in most other programming languages used to create web application. For example Rack is the equivalent of WSGI for the Ruby programming language. Although it might not be apparent, WSGI in Python and Rack in Ruby are almost identical with regards to how you create middleware for them.
Popular web frameworks usually have their own way of adding middleware to WSGI/Rack that is sometimes simpler. One such example is the Django web framework that has a really excellent and simple way of adding middleware to your application, described in the Django middleware documentation.
In Rack (and in Python’s WSGI) middleware usually looks like this:
Django makes it simpler by adding methods for each step in the filter:
The best part about Rack middleware, is the excellent way to test your custom middleware using Rack::Test. It took a bit of wrapping my head around it, but when I finally had the eureka moment it all snapped into place. Let me demonstrate with a simple example that shows how to test a Rack middleware using RSpec and Rack::Test.
The secret sauce is including
Rack::Test::Methods at the start of the description, and it does all the magic of using “app” and allowing to get/post/etc… as documented in the
Hope this helps someone, leave your comments and/or questions below.
A bit of fairy dust, a sprinkle of elven blood, lots of water and boil it all for 5 hours on a low fire. What you get? … I wouldn’t know, but I do know that it took me almost 2 whole days to grasp the idea of a JRuby in a JAR, with two bootstrappers!
Let’s assume that I wrote this great utility that does this magnificent function that will help mankind. And this utility/software needs a computer to run. Now, I speak Ruby quite well – and specify my behavior thoroughly with RSpec. But alas – most computers don’t have ruby installed (Java JRE yes, Ruby no, especially in enterprises).
So let’s also assume that some giants wrote a Ruby interpreter that can run on a JVM, and called it JRuby. And further more assume that most java “utilities” can be written inside a single jar, and can be executed with:
java -jar fairy_dust.jar
How can I take my ruby code and have it run just like the above? With just a single file and all …
3. Custom Bootstrapper
And the result is – a JAR file, with multiple Ruby files inside – that works just like a java jar:
java -jar rubified.jar
And you can get all the above (with the ant build.xml) for the price of one download!
Presenting: Rubified 0.1 BETA
I started using NetBeans recently. And find it’s ruby support superb! (as far as an IDE can go). But there was an error installing gems with their gui manager, and I was wondering how to debug that.
A quick peek into ruby/bin/gem revealed much of what I needed to know.
Lets demonstrate :
Mike Zillion asked about how to make HAML a processor (of haml files) for Apache on the HAML Group on Google. That inspired me to write a proper wrapper with caching that will Hamlize templates into HTML and cache those for speedy access on subsequent requests.
This is what I came up with:
Recently Wolfman posted a description about Rails helpers written with Haml::Helpers#open and rSpec.
I want it to be more DRY than it is, help since the whole application is using rSpec and Haml, cialis all helpers should have the same
So basically – what I did was :
I had an enlightenment today – truly an enlightenment.
After months of reading and getting to know what test driven development and extreme programming is all about, I have finally been able to practice what I preach. Since I work with several programmers and the product we are writing is so new that it is still in diapers (famous hebrew saying). Quality control is a demand from above management, after they got burned with too many escalations with the existing product. So part of my job description was preaching unit testing and such.
Anyways – I really did understand and could quote all the reasons why testing is important, and why its a sort of a documentation, and why it should be done before coding, and why it makes coding more structured and disciplined and gives the programmer a direction. But I could not make myself understand how to implement it with ruby’s Test::Unit for example, not even talking about more obscure implementations of xUnit like CppUnit which is something that was chosen for our project.
Last week I saw the presentation Dave Astels made at google, freely downloadable from google video. An I saw that it was god (with one ‘o’). And today I finally got to trash all the beginnings that I tried to do with Test::Unit on the ClearCase wrapper I’m writing – and started doing rSpec instead.
It flowed… like a river. And any bumps that were on the were not frustrating, they were annoying, thus actually motivating me to kick them of my route and continue flowing with the flow without any frustration. It rocks. I love it.
Still the one thing I miss with all this TDD and BDD are good concrete examples that are a bit more than what rSpec examples are, I need it chewed and digested for extreme easy understanding. Maybe a step-by-step evolving kind of code, maybe even a book matching the Progmatic books quality is the thing missing in all of this. Because it always looks really great – but when a motivated but inexperienced programmer like me tries to implement this stuff it soon becomes frustrating. Even though rSpec is not too frustrating, and hopefully it will only grow to be easier and more intuitive and productive like it is already in its alpha(?) state.
Enough ramblings for now – back to the code! 🙂