Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return DateTime objects instead of strings from temporal methods #10

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion health_graph.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Gem::Specification.new do |s|

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Kenny Ma"]
s.date = "2012-05-09"
s.date = "2012-05-11"
s.description = "This is a wrapper for RunKeeper Health Graph RESTful API."
s.email = "kenny@kennyma.me"
s.extra_rdoc_files = [
Expand All @@ -28,6 +28,7 @@ Gem::Specification.new do |s|
"lib/health_graph/api.rb",
"lib/health_graph/authentication.rb",
"lib/health_graph/configuration.rb",
"lib/health_graph/datetime.rb",
"lib/health_graph/model.rb",
"lib/health_graph/models/fitness_activities_feed.rb",
"lib/health_graph/models/fitness_activity_delete.rb",
Expand Down
1 change: 1 addition & 0 deletions lib/health_graph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'faraday_middleware'
require 'health_graph/configuration'
require 'health_graph/authentication'
require 'health_graph/datetime'
require 'health_graph/api'
require 'health_graph/model'
require 'health_graph/models/user'
Expand Down
9 changes: 9 additions & 0 deletions lib/health_graph/datetime.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "date"

module HealthGraph
class DateTime < ::DateTime
def self.coerce value
strptime value, "%a, %d %b %Y %H:%M:%S"
end
end
end
25 changes: 23 additions & 2 deletions lib/health_graph/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,41 @@ def hash_attr_accessor(*symbols)
end
end
end

def coerce_key(key, into)
define_method "unpack_#{key}" do |value|
if value.is_a? into
value

elsif into.respond_to? :coerce
into.coerce value

else
into.new value
end
end
end
end

def populate_from_hash!(hash)
return unless hash

hash.each do |key, value|
hash.each do |key, raw_value|
unpack_method = "unpack_#{key}"
set_attr_method = "#{key}="

value = if respond_to?(unpack_method)
self.__send__(unpack_method, raw_value)
else
raw_value
end

unless value.nil?
if respond_to?(set_attr_method)
self.__send__(set_attr_method, value)
end
end
end
end

end
end
17 changes: 13 additions & 4 deletions lib/health_graph/models/fitness_activities_feed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,26 @@ class Item
include Model

hash_attr_accessor :type, :start_time, :total_distance, :duration, :uri

coerce_key :start_time, HealthGraph::DateTime

def initialize(hash)
populate_from_hash! hash
end
end
end

def initialize(access_token, path, params = {})
self.access_token = access_token
response = get path, HealthGraph.accept_headers[:fitness_activity_feed], params
self.body = response.body
populate_from_hash! self.body
end
end

protected

def unpack_items value
value.map do |hash|
Item.new hash
end
end
end
end
17 changes: 13 additions & 4 deletions lib/health_graph/models/sleep_feed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,26 @@ class Item
include Model

hash_attr_accessor :timestamp, :total_sleep, :times_woken, :rem, :deep, :light, :awake, :uri

coerce_key :timestamp, HealthGraph::DateTime

def initialize(hash)
populate_from_hash! hash
end
end
end

def initialize(access_token, path)
self.access_token = access_token
response = get path, HealthGraph.accept_headers[:sleep_feed]
self.body = response.body
populate_from_hash! self.body
end
end

protected

def unpack_items value
value.map do |hash|
Item.new hash
end
end
end
end
end
29 changes: 19 additions & 10 deletions lib/health_graph/models/weight_feed.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
module HealthGraph
class WeightFeed
include Model

hash_attr_accessor :items

class Item
include Model

hash_attr_accessor :timestamp, :weight, :free_mass, :mass_weight, :fat_percent, :bmi, :uri

def initialize(hash)
coerce_key :timestamp, HealthGraph::DateTime

def initialize(hash)
populate_from_hash! hash
end
end
end

def initialize(access_token, path)
self.access_token = access_token
response = get path, HealthGraph.accept_headers[:weight_feed]
self.body = response.body
populate_from_hash! self.body
end
populate_from_hash! self.body
end

protected

def unpack_items value
value.map do |hash|
Item.new hash
end
end
end
end
end
103 changes: 42 additions & 61 deletions test/health_graph/test_fitness_activities_feed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,71 +30,53 @@ class TestFitnessActivitiesFeed < Test::Unit::TestCase
end

should "get body" do
expected = {"items"=>
[{"duration"=>7920,
"total_distance"=>62764.416,
"start_time"=>"Thu, 15 Sep 2011 13:28:59",
"type"=>"Cycling",
"uri"=>"/fitnessActivities/52367938"},
{"duration"=>1080,
"total_distance"=>2574.9504,
"start_time"=>"Thu, 15 Sep 2011 08:00:00",
"type"=>"Walking",
"uri"=>"/fitnessActivities/52367984"},
{"duration"=>2880,
"total_distance"=>7402.9824,
"start_time"=>"Wed, 14 Sep 2011 20:00:00",
"type"=>"Running",
"uri"=>"/fitnessActivities/52368041"},
{"duration"=>3060,
"total_distance"=>7724.8512,
"start_time"=>"Tue, 13 Sep 2011 20:00:00",
"type"=>"Running",
"uri"=>"/fitnessActivities/52368120"},
{"duration"=>4500,
"total_distance"=>5632.704,
"start_time"=>"Tue, 13 Sep 2011 08:00:00",
"type"=>"Running",
"uri"=>"/fitnessActivities/52126193"}],
"size"=>5}

expected = {
"items" => [
{
"duration" => 7920,
"total_distance" => 62764.416,
"start_time" => "Thu, 15 Sep 2011 13:28:59",
"type" => "Cycling",
"uri" => "/fitnessActivities/52367938"
}, {
"duration"=>1080,
"total_distance"=>2574.9504,
"start_time" => "Thu, 15 Sep 2011 08:00:00",
"type" => "Walking",
"uri" => "/fitnessActivities/52367984"
}, {
"duration"=>2880,
"total_distance"=>7402.9824,
"start_time" => "Wed, 14 Sep 2011 20:00:00",
"type" => "Running",
"uri" => "/fitnessActivities/52368041"
}, {
"duration"=>3060,
"total_distance"=>7724.8512,
"start_time" => "Tue, 13 Sep 2011 20:00:00",
"type" => "Running",
"uri" => "/fitnessActivities/52368120"
}, {
"duration"=>4500,
"total_distance"=>5632.704,
"start_time" => "Tue, 13 Sep 2011 08:00:00",
"type" => "Running",
"uri" => "/fitnessActivities/52126193"
}
],
"size"=>5
}

assert_equal expected, @user.fitness_activities.body
end

context "item" do
should "get items" do
expected = [{"duration"=>7920,
"total_distance"=>62764.416,
"start_time"=>"Thu, 15 Sep 2011 13:28:59",
"type"=>"Cycling",
"uri"=>"/fitnessActivities/52367938"},
{"duration"=>1080,
"total_distance"=>2574.9504,
"start_time"=>"Thu, 15 Sep 2011 08:00:00",
"type"=>"Walking",
"uri"=>"/fitnessActivities/52367984"},
{"duration"=>2880,
"total_distance"=>7402.9824,
"start_time"=>"Wed, 14 Sep 2011 20:00:00",
"type"=>"Running",
"uri"=>"/fitnessActivities/52368041"},
{"duration"=>3060,
"total_distance"=>7724.8512,
"start_time"=>"Tue, 13 Sep 2011 20:00:00",
"type"=>"Running",
"uri"=>"/fitnessActivities/52368120"},
{"duration"=>4500,
"total_distance"=>5632.704,
"start_time"=>"Tue, 13 Sep 2011 08:00:00",
"type"=>"Running",
"uri"=>"/fitnessActivities/52126193"}]

assert_equal expected, @user.fitness_activities.items
should "get five items" do
assert_equal 5, @user.fitness_activities.items.size
end

should "get start time" do
assert_equal "Thu, 15 Sep 2011 13:28:59", @user.fitness_activities.items[0].start_time
assert_equal DateTime.new(2011, 9, 15, 13, 28, 59), @user.fitness_activities.items[0].start_time
end

should "get duration" do
Expand All @@ -108,11 +90,10 @@ class TestFitnessActivitiesFeed < Test::Unit::TestCase
should "get type" do
assert_not_equal nil, @user.fitness_activities.items[0].type
end

should "get uri" do
assert_not_equal nil, @user.fitness_activities.items[0].uri
end

end
end
end
end
50 changes: 25 additions & 25 deletions test/health_graph/test_sleep_feed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,40 @@ class TestSleepFeed < Test::Unit::TestCase
end

should "get body" do
expected = {"items"=>
[{"timestamp"=>"Thu, 22 Sep 2011 00:00:00",
"total_sleep"=>822,
"uri"=>"/sleep/6253947"},
{"timestamp"=>"Thu, 22 Sep 2011 00:00:00",
"uri"=>"/sleep/6253948",
"times_woken"=>29}],
"size"=>2}

expected = {
"items" => [
{
"timestamp" => "Thu, 22 Sep 2011 00:00:00",
"total_sleep" => 822,
"uri" => "/sleep/6253947"
}, {
"timestamp" => "Thu, 22 Sep 2011 00:00:00",
"uri" => "/sleep/6253948",
"times_woken" => 29
}
],
"size" => 2
}

assert_equal expected, @user.sleep.body
end

context "item" do
should "get items" do
expected = [{"timestamp"=>"Thu, 22 Sep 2011 00:00:00",
"total_sleep"=>822,
"uri"=>"/sleep/6253947"},
{"timestamp"=>"Thu, 22 Sep 2011 00:00:00",
"uri"=>"/sleep/6253948",
"times_woken"=>29}]

assert_equal expected, @user.sleep.items
should "get two items" do
assert_equal 2, @user.sleep.items.size
end

should "get timestamp" do
assert_equal "Thu, 22 Sep 2011 00:00:00", @user.sleep.items[0].timestamp
assert_equal DateTime.new(2011, 9, 22, 0, 0, 0), @user.sleep.items[0].timestamp
end
should "get time sleep" do

should "get total sleep" do
assert_equal 822, @user.sleep.items[0].total_sleep
assert_equal nil, @user.sleep.items[1].total_sleep
end

should "get time woken" do

should "get times woken" do
assert_equal nil, @user.sleep.items[0].times_woken
assert_equal 29, @user.sleep.items[1].times_woken
end

Expand Down
Loading