Permalink
Browse files

implement #all pagination. it will loop through all the pages and ret…

…urn a concatenated list
  • Loading branch information...
1 parent 041078a commit 70be44e88207ad4dd7891268c2f33e3cafcf0694 @rauhryan rauhryan committed Jan 25, 2012
Showing with 59 additions and 1 deletion.
  1. +13 −1 lib/ghee/resource_proxy.rb
  2. +46 −0 spec/ghee/resource_proxy_spec.rb
View
@@ -63,13 +63,25 @@ def paginate(options)
req.params.merge! params
end
- @subject = response.body
+ if @subject.nil?
+ @subject = response.body
+ else
+ @subject = @subject.concat response.body
+ end
parse_link_header response.headers.delete("link")
return self
end
+ def all
+ return self if pagination && next_page.nil?
+
+ self.paginate :per_page => 100, :page => next_page || 1
+
+ self.all
+ end
+
# Generate first_page, last_page, next_page, prev_page convienence methods
%w{ next prev first last }.each do |term|
define_method "#{term}_page" do
@@ -18,8 +18,54 @@ def headers
end
end
+class IncrementPager
+ def initialize(times=2)
+ @times = times
+ @executed = 1
+ end
+ def headers
+
+ return {} if @executed == @times
+ @executed += 1
+ {"link" => "<https://api.github.com/users/rauhryan/repos?page=#{@executed}>; rel=\"next\""}
+ end
+ def body
+ [@executed]
+ end
+
+end
describe "Pagination" do
+ describe "#all with many pages" do
+ before(:each) do
+ @connection = mock('connection')
+ @connection.stub!(:get).and_return(IncrementPager.new 2)
+ end
+
+ subject do
+ Ghee::ResourceProxy.new(@connection, '/foo')
+ end
+
+ it "should return count of 2" do
+ subject.all.size.should == 2
+ end
+ end
+ describe "#all with 1 page" do
+ before(:each) do
+ @connection = mock('connection')
+ @connection.stub!(:get).and_return(IncrementPager.new 1)
+ end
+
+ subject do
+ Ghee::ResourceProxy.new(@connection, '/foo')
+ end
+
+ it "should return count of 1" do
+ subject.all.size.should == 1
+ end
+ end
+
+
describe "past first page" do
before(:each) do

0 comments on commit 70be44e

Please sign in to comment.