kennyma/health_graph — pull request #10 by adammck (2012-05-10 22:46:17+00:00)
Title: Return DateTime objects instead of strings from temporal methods

This is a fairly major change, which breaks backwards compatibility (albeit in an easily-fixable way). So if it seems useful, please check it carefully before merging.

Currently, the time-related accessors like `FitnessActivitiesFeed::Item#start_time` return strings straight from RunKeeper. They look something like `"Thu, 15 Sep 2011 13:28:59"`. This is fine for displaying as-is, but not a lot of use for anything else. This patch intercepts the values before they're stored (in the `Model#populate_from_hash!` method), and coerces them into `DateTime` objects.

The implementation is a little more complex than it *needs* to be, because I've tried to model it after [`Hashie::Extensions::Coercion`] [coercion], so we can easily replace this minimal solution with theirs, once a stable release is available. Faraday already uses Hashie, so it isn't a new dependency.

Coercion looks like (from `weight_feed.rb`):

```ruby
hash_attr_accessor :timestamp, :weight, :uri # etc
coerce_key :timestamp, HealthGraph::DateTime
```

The `HealthGraph::DateTime` class is just a wrapper around `::DateTime` which knows how to parse RunKeeper-style timestamps. Any class with a `coerce` method can be used.

[coercion]: https://github.com/intridea/hashie/blob/master/lib/hashie/extensions/coercion.rb