Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add pagination to acitities feed #9

Open
wants to merge 2 commits into from

1 participant

@adammck

This adds :next_page and :previous_page methods to FitnessActitiesFeed, which return new instances of the same class. The existing :next and :previous accessors work as before, although they're not very useful now that pagination is available.

For example, to fetch every item ever posted:
(This would obviously be a very bad idea in production)

feed = @user.fitness_activities
items = []

while feed
  items += feed.items
  feed = feed.next_page
end

This involved a minor refactoring of the test fixtures. Hope you don't mind.

/cc @andrewpbrett

adammck added some commits
@adammck adammck Add pagination to acitities feed.
This adds :next_page and :previous_page methods to FitnessActitiesFeed,
which return new instances. The existing :next and :previous accessors
work as before, although they're not very useful now that pagination
is available.

For example, to fetch every item ever posted:

  feed = @user.fitness_activities
  items = []

  while feed
    items += feed.items
    feed = feed.next_page
  end

This would obviously be a very bad idea in production.
11cb163
@adammck adammck Add new fixtures to gemspec. 953dc36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 10, 2012
  1. @adammck

    Add pagination to acitities feed.

    adammck authored
    This adds :next_page and :previous_page methods to FitnessActitiesFeed,
    which return new instances. The existing :next and :previous accessors
    work as before, although they're not very useful now that pagination
    is available.
    
    For example, to fetch every item ever posted:
    
      feed = @user.fitness_activities
      items = []
    
      while feed
        items += feed.items
        feed = feed.next_page
      end
    
    This would obviously be a very bad idea in production.
Commits on May 11, 2012
  1. @adammck

    Add new fixtures to gemspec.

    adammck authored
This page is out of date. Refresh to see the latest.
View
6 health_graph.gemspec
@@ -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 = [
@@ -39,7 +39,9 @@ Gem::Specification.new do |s|
"lib/health_graph/models/weight_feed.rb",
"test/fixtures/background_activities_feed.json",
"test/fixtures/background_activity.json",
- "test/fixtures/fitness_activities_feed.json",
+ "test/fixtures/fitness_activities_feed/all.json",
+ "test/fixtures/fitness_activities_feed/page_0.json",
+ "test/fixtures/fitness_activities_feed/page_1.json",
"test/fixtures/fitness_activity.json",
"test/fixtures/new_fitness_activity_params.json",
"test/fixtures/profile_get.json",
View
4 lib/health_graph/api.rb
@@ -1,7 +1,7 @@
module HealthGraph
module API
- attr_accessor :access_token
-
+ attr_accessor :access_token, :path, :params
+
def get(path, accept_header, params = {})
request(:get, accept_header, path, params)
end
View
24 lib/health_graph/models/fitness_activities_feed.rb
@@ -13,12 +13,30 @@ def initialize(hash)
populate_from_hash! hash
end
end
-
- def initialize(access_token, path, params = {})
+
+ def initialize(access_token, path, params = {})
self.access_token = access_token
+ self.path = path
+ self.params = params
+
response = get path, HealthGraph.accept_headers[:fitness_activity_feed], params
self.body = response.body
+
populate_from_hash! self.body
- end
+ end
+
+ def next_page
+ page(self.next) if self.next
+ end
+
+ def previous_page
+ page(self.previous) if self.previous
+ end
+
+ protected
+
+ def page path
+ self.class.new self.access_token, path
+ end
end
end
View
1  test/fixtures/fitness_activities_feed.json
@@ -1 +0,0 @@
-{"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}
View
34 test/fixtures/fitness_activities_feed/all.json
@@ -0,0 +1,34 @@
+{
+ "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
+}
View
23 test/fixtures/fitness_activities_feed/page_0.json
@@ -0,0 +1,23 @@
+{
+ "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"
+ }],
+ "size": 5,
+ "next": "/fitnessActivities?page=1&pageSize=3"
+}
View
17 test/fixtures/fitness_activities_feed/page_1.json
@@ -0,0 +1,17 @@
+{
+ "items": [{
+ "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,
+ "previous": "/fitnessActivities?page=0&pageSize=3"
+}
View
33 test/health_graph/test_fitness_activities_feed.rb
@@ -9,11 +9,17 @@ class TestFitnessActivitiesFeed < Test::Unit::TestCase
stub_request(:get, HealthGraph.endpoint + '/fitnessActivities'
).with(:header => {'Authorization' => 'Bearer ' + TEST_USER_TOKEN, 'Accept' => HealthGraph.accept_headers[:fitness_activities_feed]}
- ).to_return(:body => fixture('fitness_activities_feed.json'))
+ ).to_return(:body => fixture('fitness_activities_feed/all.json'))
stub_request(:get, HealthGraph.endpoint + '/fitnessActivities?pageSize=3'
).with(:header => {'Authorization' => 'Bearer ' + TEST_USER_TOKEN, 'Accept' => HealthGraph.accept_headers[:fitness_activities_feed]}
- ).to_return(:body => fixture('fitness_activities_feed.json'))
+ ).to_return(:body => fixture('fitness_activities_feed/page_0.json'))
+
+ stub_request(:get, HealthGraph.endpoint + '/fitnessActivities?pageSize=3&page=0'
+ ).to_return(:body => fixture('fitness_activities_feed/page_0.json'))
+
+ stub_request(:get, HealthGraph.endpoint + '/fitnessActivities?pageSize=3&page=1'
+ ).to_return(:body => fixture('fitness_activities_feed/page_1.json'))
@user = HealthGraph::User.new(TEST_USER_TOKEN)
end
@@ -59,8 +65,27 @@ class TestFitnessActivitiesFeed < Test::Unit::TestCase
"size"=>5}
assert_equal expected, @user.fitness_activities.body
- end
-
+ end
+
+ context "pagination" do
+ should "get the next page" do
+ page_1 = @user.fitness_activities(:pageSize=>3).next_page
+ assert_requested :get, HealthGraph.endpoint + '/fitnessActivities?pageSize=3&page=1'
+ assert_equal 2, page_1.items.length
+ end
+
+ should "get the previous page" do
+ page_0 = @user.fitness_activities(:pageSize=>3, :page=>1).previous_page
+ assert_requested :get, HealthGraph.endpoint + '/fitnessActivities?pageSize=3&page=0'
+ assert_equal 3, page_0.items.length
+ end
+
+ should "return nil when there are no more pages" do
+ assert_nil @user.fitness_activities.previous_page
+ assert_nil @user.fitness_activities.next_page
+ end
+ end
+
context "item" do
should "get items" do
expected = [{"duration"=>7920,
Something went wrong with that request. Please try again.