Refactor heartbeat generation to reduce duplication.

This also extends the integration test to invoke the rake task which makes it more of an end-to-end test.