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

Added ability to pass extra parameters to next_page and previous_page #330

Merged
merged 2 commits into from
Oct 26, 2013
Merged
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
16 changes: 12 additions & 4 deletions lib/koala/api/graph_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,26 @@ def self.evaluate(response, api)

# Retrieve the next page of results.
#
# @param [Hash] extra_params Some optional extra parameters for paging. For supported parameters see https://developers.facebook.com/docs/reference/api/pagination/
#
# @example With optional extra params
# wall = api.get_connections("me", "feed", since: 1379593891)
# wall.next_page(since: 1379593891)
#
# @return a GraphCollection array of additional results (an empty array if there are no more results)
def next_page
def next_page extra_params = {}
base, args = next_page_params
base ? @api.get_page([base, args]) : nil
base ? @api.get_page([base, args.merge(extra_params)]) : nil
end

# Retrieve the previous page of results.
#
# @param [Hash] extra_params Some optional extra parameters for paging. For supported parameters see https://developers.facebook.com/docs/reference/api/pagination/
#
# @return a GraphCollection array of additional results (an empty array if there are no earlier results)
def previous_page
def previous_page extra_params = {}
base, args = previous_page_params
base ? @api.get_page([base, args]) : nil
base ? @api.get_page([base, args.merge(extra_params)]) : nil
end

# Arguments that can be sent to {Koala::Facebook::API#graph_call} to retrieve the next page of results.
Expand Down
34 changes: 32 additions & 2 deletions spec/cases/graph_collection_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
require 'spec_helper'

describe Koala::Facebook::GraphCollection do
let(:paging){ {:paging => true} }

before(:each) do
@result = {
"data" => [1, 2, :three],
"paging" => {:paging => true}
"paging" => paging
}
@api = Koala::Facebook::API.new("123")
@collection = Koala::Facebook::GraphCollection.new(@result, @api)
Expand Down Expand Up @@ -46,7 +48,7 @@
"paging" => {}
}
@base = double("base")
@args = double("args")
@args = {"a" => 1}
@page_of_results = double("page of results")
end

Expand Down Expand Up @@ -141,4 +143,32 @@
Koala::Facebook::GraphCollection.evaluate(result, @api).should == expected
end
end

describe "#next_page" do
let(:paging){ {"next" => "http://example.com/?a=2&b=3"} }

it "should get next page" do
@api.should_receive(:get_page).with(["", {"a" => "2", "b" => "3"}])
@collection.next_page
end

it "should get next page with extra parameters" do
@api.should_receive(:get_page).with(["", {"a" => "2", "b" => "3", "c" => "4"}])
@collection.next_page("c" => "4")
end
end

describe "#previous_page" do
let(:paging){ {"previous" => "http://example.com/?a=2&b=3"} }

it "should get previous page" do
@api.should_receive(:get_page).with(["", {"a" => "2", "b" => "3"}])
@collection.previous_page
end

it "should get previous page with extra parameters" do
@api.should_receive(:get_page).with(["", {"a" => "2", "b" => "3", "c" => "4"}])
@collection.previous_page("c" => "4")
end
end
end