So, a couple of days ago I sat in front of my computer, the EVE Metrics development environment still rebuilding itself on my local development box, and thought “Right, I need a project”. Inspired by election mashups like Debillitated and VoteThemOut, I figured I should do something for the election. I tweeted, and a short while later had a suggestion from Jim Killock of the Open Rights Group. I got that at about 9PM on Saturday, and I launched PoliticsPosters.co.uk about 24 hours later via Twitter.
The basic premise of the site is you can stick in your postcode, and it’ll provide you with a poster to stick up in your window to encourage candidates to come and talk to you about the Digital Economy Bill. The poster includes how your last MP voted, too. It’ll also give you links to share the site and a link over to Email Your Candidates so you can get in touch with your candidates directly.
It’s a neat little site people seem to like, and after some polishing it runs smoothly and fast as anything, too. I’m running it off one of my Linode 360 VPSes, which are fantastic little VPSes- small but mighty. Without any optimization the site can handle 250 requests a second on the faster portions of the site, accelerated to 1000 requests a second (testing with ab, 25 concurrent requests and 15000 total) with Rack::Cache and memcached. Even the very slow, involving-the-TWFY-API-and-database-and-PDF-renderer-sometimes portion of the site can manage 80 requests a second (And my apologies for TWFY for hammering your API by accident, though it seems not to have noticed).
The site’s pretty simple. It’s a Sinatra based app, using jQuery, Cufon (for font rendering) and font-kit (for more of the same) on the client side, and YAML, DataMapper, Prawn, libraries for TWFY, Haml, Rack, Rack::Cache, and Rack::Hoptoad on the server side. If you ignore the libraries, the whole site weighs in at around 300 lines of code; I may open source it if I get a spare moment.
Prawn- an excellent Ruby PDF library- is an absolute joy to use if you’re not using custom fonts. If you are, it gets difficult due to some bugs, but they’re getting fixed and they’re easy to work around. Rendering any poster I choose along a basic common framework is accomplished in less than 30 lines of code, which lets me render posters like so:
-
r.render("out.pdf", {
-
:text=>[
-
{:content=>"Candidates: Please call in!", :pad_top=>2, :pad_bottom=>2, :font_size=>72},
-
{:content=>"David Cameron didn't turn up to vote.", :font_size=>48},
-
{:content=>"Tell me what you did about the Digital Economy Act.", :pad_top=>1, :font_size=>42}
-
]
-
})
Mix a little YAML into the equation and I now just have a posters config file that defines all the posters for any constituency, with the appropriate names and phrases subbed in at runtime. Perfect!
DataMapper backed onto PostgreSQL running on another Linode 360 over Linode’s internal network handles persistence; this is used to store MPs, parties and constituencies. This lets me cache results of things like looking up which MPs voted which way on the Digital Economy Bill, and just makes things like that a lot easier.
The site itself is served up using Nginx 0.8.35 for static files, which routes dynamic requests via a Unix socket to Unicorn. Unicorn is a blazing fast, brilliantly easy to manage app server that will soon be replacing Thin on EVE Metrics after this; completely seamless app reboots are a matter of sending a kill -HUP, and the whole thing is speedy as hell to boot. Rack::Cache sits in front of Sinatra and uses memcached to reply with lightning speed on anything we can cache. The stack is a pleasure to maintain and works well while being fast, so I’m happy with that.
Sinatra is as ever a very nice way to write webapps, Haml I’ve used for years now and this project is no different; not a lot of it, but what there is is done with Haml exclusively. Just too damn nice to go back to Erb.
So there you have it, everything that goes into an app like that. I’ll be out most of tomorrow and hopefully it won’t need any babysitting while I’m out. Having said that, I’ve probably doomed myself to an odd bug early in the morning. Ah well. Off to sleep for me!
Thanks, very useful little page
James
Love the idea, directed to your site from Ben Goldacre. Would probably like it more if it worked – tried my postcode (Burton/Derby, for what it’s worth) and it gave me pages of code but nothing useful. I have a feeling that Burton is a changing area (joining with Uttoxeter?) so maybe that’s it…