Skip to content

Commit

Permalink
Extract GraphCollection#parse_page_url to a class method.
Browse files Browse the repository at this point in the history
To make it easier for non-Rails apps to use the GraphCollection's paging support, parse_page_url can now be used without having to instantiate a GraphCollection object.  (Useful for passing the whole previous/next URLs as parameters in environments lacking url_for.)
  • Loading branch information
arsduo committed Oct 25, 2011
1 parent af46a0f commit 5705d11
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions lib/koala/graph_api.rb
Expand Up @@ -205,6 +205,7 @@ def get_comments_for_urls(urls = [], args = {}, options = {})
args.merge!(:ids => urls.respond_to?(:join) ? urls.join(",") : urls)
get_object("comments", args, options)
end

def set_app_restrictions(app_id, restrictions_hash, args = {}, options = {})
graph_call(app_id, args.merge(:restrictions => MultiJson.encode(restrictions_hash)), "post", options)
end
Expand Down
6 changes: 5 additions & 1 deletion lib/koala/graph_collection.rb
Expand Up @@ -41,8 +41,12 @@ def initialize(response, api)
parse_page_url(@paging[this])
end
end

def parse_page_url(url)
GraphCollection.parse_page_url(url)
end

def self.parse_page_url(url)
match = url.match(/.com\/(.*)\?(.*)/)
base = match[1]
args = match[2]
Expand Down
28 changes: 22 additions & 6 deletions spec/cases/graph_collection_spec.rb
Expand Up @@ -74,14 +74,30 @@
end

describe "when parsing page paramters" do
it "should return the base as the first array entry" do
base = "url_path"
@collection.parse_page_url("anything.com/#{base}?anything").first.should == base
describe "#parse_page_url" do
it "should pass the url to the class method" do
url = stub("url")
Koala::Facebook::GraphCollection.should_receive(:parse_page_url).with(url)
@collection.parse_page_url(url)
end

it "should return the result of the class method" do
parsed_content = stub("parsed_content")
Koala::Facebook::GraphCollection.stub(:parse_page_url).and_return(parsed_content)
@collection.parse_page_url(stub("url")).should == parsed_content
end
end

describe ".parse_page_url" do
it "should return the base as the first array entry" do
base = "url_path"
Koala::Facebook::GraphCollection.parse_page_url("anything.com/#{base}?anything").first.should == base
end

it "should return the arguments as a hash as the last array entry" do
args_hash = {"one" => "val_one", "two" => "val_two"}
@collection.parse_page_url("anything.com/anything?#{args_hash.map {|k,v| "#{k}=#{v}" }.join("&")}").last.should == args_hash
it "should return the arguments as a hash as the last array entry" do
args_hash = {"one" => "val_one", "two" => "val_two"}
Koala::Facebook::GraphCollection.parse_page_url("anything.com/anything?#{args_hash.map {|k,v| "#{k}=#{v}" }.join("&")}").last.should == args_hash
end
end
end

Expand Down

0 comments on commit 5705d11

Please sign in to comment.