Browse files

added previous/next pagination to resource

  • Loading branch information...
1 parent cc27363 commit afcbd4d953f328ea795d3e711244d78e13fbb5f0 @vpacher vpacher committed Aug 21, 2013
Showing with 59 additions and 1 deletion.
  1. +13 −1 lib/shutl/resource/rest.rb
  2. +46 −0 spec/rest_resource_spec.rb
View
14 lib/shutl/resource/rest.rb
@@ -65,13 +65,25 @@ def respond_to? method
end
def parsed
- response.parsed_response
+ @parsed ||= response.body.with_indifferent_access
end
def status
response.code
end
+ def pagination
+ parsed[:pagination]
+ end
+
+ def next_resource
+ pagination[:next_resource] if pagination
+ end
+
+ def previous_resource
+ pagination[:previous_resource] if pagination
+ end
+
attr_accessor :errors
def valid?
View
46 spec/rest_resource_spec.rb
@@ -44,6 +44,52 @@
end
end
+ context 'pagination' do
+ let(:body) do
+ {
+ test_rest: { a: "a", b: 2 },
+ pagination: pagination
+ }.to_json
+ end
+ let(:pagination) { {previous_resource: 1, next_resource: 3} }
+
+ before do
+ @request = stub_request(:get, 'http://host/test_rests/a').
+ to_return(:status => 200, :body => body, :headers => headers)
+ end
+
+ context 'both present' do
+ specify do
+ resource = TestRest.find('a', auth: "some auth")
+ resource.previous_resource.should == 1
+ resource.next_resource .should == 3
+ end
+ end
+
+ context 'one present' do
+ let(:pagination) { {next_resource: 3} }
+ specify do
+ resource = TestRest.find('a', auth: "some auth")
+ resource.previous_resource.should be_nil
+ resource.next_resource .should == 3
+ end
+ end
+
+ context 'none present' do
+ let(:body) do
+ {
+ test_rest: { a: "a", b: 2 }
+ }.to_json
+ end
+ specify do
+ resource = TestRest.find('a', auth: "some auth")
+ resource.pagination .should be_nil
+ resource.previous_resource.should be_nil
+ resource.next_resource .should be_nil
+ end
+ end
+ end
+
context 'with no arguments' do
let(:headers_with_auth) do
headers.merge("Authorization" => "Bearer some auth")

0 comments on commit afcbd4d

Please sign in to comment.