Tools for Tests

Writing test code isn’t easy at first, and writing good test code is even harder. I've reviewed several tools to help write better tests here, focussing on ruby.

Code Coverage

Code coverage tools attempt to analyse how much of your code has been tested. Reports are generated based on your test code, with columns expressing how much code has been tested.

Read More →

Push Back Explored

‘Push Back’ is a simple concept succinctly explained in 37signals’ book, Getting Real. When translating requirements into code, issues often crop up that make implementation impractical. Something about the structure of your project resists a new feature.

Getting Real suggests ways to mitigate push back. I’m going to explain how to spot push back occurring in your code.

Methods or variables become hard to name

If you’re having issues naming things, perhaps you don’t fully understand the problem you’re trying to solve. If the concept is ill–defined, your code is pushing back. If you can’t rethink a cleaner approach, go back and discuss the feature with your client (or boss).

Read More →

Rails Quality Control Tip: Use Verify in Controllers

Verify used in my code

Have you found that any of your Rails projects get hits to controller methods that expect parameters? A neat way of handling this is the ActionController::Verification module.

By adding calls to verify in your controller, you can elegantly catch all kinds of unexpected but reasonable uses of your system: from mistyped URLs and browser history auto–complete to web crawlers blindly following links. This way, you can redirect people somewhere logical instead of flashing them with an error message.

Read More →

Rails Speed Tip: link_tag Caching

Have you ever tried concatenating your JavaScript and CSS files for performance improvements? The idea is that latency is a bigger issue than file size when loading web pages, so stuffing all your JavaScript into a monolithic file for deployment should improve performance.

I wrote a rake task to do this for some of my applications (such as tiktrac). This is slightly more cumbersome than a feature I spied in the ActionPack changelog:

Added caching option to AssetTagHelper#stylesheet_link_tag and AssetTagHelper#javascript_include_tag [DHH]. Examples:
stylesheet_link_tag :all, :cache => true # when ActionController::Base.perform_caching is false =>
stylesheet_link_tag :all, :cache => true # when ActionController::Base.perform_caching is true =>
...when caching is on, all.css is the concatenation of style1.css, styleB.css, and styleX2.css. Same deal for JavaScripts.

Read more in the asset_tag_helper.rb source.

Read More →

Dashboard Widgets are Great with Prototype

One of my commercial projects this year was to build an online ticket sales system for a large bar and venue company in London. The run up to Christmas was the big live test of the system, so they needed statistics on ticket sales.

Just for fun, I decided to make a Dashboard widget that used curl to fetch the current data from a script on their server. But you know what made this task 100% less painful? My old friend Prototype.js.

Using Prototype allowed me to build the widget in literally minutes, since it was a simple case of displaying the result of a system call run with PeriodicalExecuter. Most of the time was spent looking up how to enable system commands in the documentation: AllowSystem must be added to your plist file, which takes a boolean value. I haven’t yet noticed PeriodicalExecuter doing anything strange when the widget isn’t visible, and it didn’t have any CPU–draining side–effects.

Read More →

Server-Sent Events in Opera

Apparently, Opera 9 supports Server–Sent Events which you can read about in the WHATWG Web Applications 1.0 specification. Their demo application is a little chat program, which is a very obvious example of the technology.

There’s many times when I’ve wanted to add this to my applications: in Bugtagger I have to ask the service if a new message from a customer has been posted to a bug, every n seconds. It would be far more efficient for the serve to send a message using this technology.

And, since Opera’s so popular on embedded and mobile platforms, if this makes its way to mobile phones you’re going to see some seriously interesting mobile web activity.

Read More →

Easy Zip Compression in Ruby

I needed a quick way of exporting data as zlib from a controller in Rails, so I came up with this:

def export
  send_data compress_string(Document.find_all.to_xml), :filename => 'backup.xml.gz'
end

def compress_string(data)
  gz = Zlib::GzipWriter.new(StringIO.new(''))
  gz.write data
  gz.close.string
rescue
  gz.close
  raise
end

Another way would be to use tempfiles with Tempfile — I wanted to benchmark and profile using files compared to StringIO, but that’ll be an exercise for another day.

This could also work nicely with Minitar.

Read More →

Load Your .vimrc with Helipad

Helipad is a little web notepad I wrote to throw to–do lists, notes, ideas and bits of code at. My idea was to make the perfect partner for my actual physical notepad.

Like everything I do, it has an API, and that allowed me to do something that I’ve found amazingly useful: load my vimrc remotely.

I know there’s a million other ways to do this, but I think it illustrates something unexpected and interesting that you can do with web application APIs. And, on the same topic, the AmbientClock uses your Google Calendar to change its colour according to how busy you are.

Read More →

Easy Mac Applications with Camping

Camping.gif

Imagine your own little mac os app written with the Camping framework, that you can easily share with other mac–weilding friends. It’s all possible with next to no ridiculous hacking at all!

All you need is Platypus. Set it up to create a ruby app that outputs to a text window. Click on “advanced” and select “remain running after completion”, then create the app. Take a look inside the application folder at the script it creates at the script:

Read More →