diff --git a/lib/koala/graph_api.rb b/lib/koala/graph_api.rb index cdd68366..36f760c0 100644 --- a/lib/koala/graph_api.rb +++ b/lib/koala/graph_api.rb @@ -2,6 +2,57 @@ module Koala module Facebook GRAPH_SERVER = "graph.facebook.com" + class GraphCollection < Array + #This class is a light wrapper for collections returned + #from the Graph API. + # + #It extends Array to allow direct access to the data colleciton + #which should allow it to drop in seamlessly. + # + #It also allows access to paging information and the + #ability to get the next/previous page in the collection + #by calling next_page or previous_page. + attr_reader :paging + + def initialize(response) + super response["data"] + @paging = response["paging"] + end + + def next_page(graph) + base,args = next_page_params + GraphCollection.new graph.graph_call(base, args) + end + + def previous_page(graph) + base,args = previous_page_params + GraphCollection.new graph.graph_call(base, args) + end + + def next_page_params + parse_page_url(@paging["next"]) + end + + def previous_page_params + parse_page_url(@paging["previous"]) + end + + def parse_page_url(url) + match = url.match(/.com\/(.*)\?(.*)/) + base = match[1] + args = match[2] + params = CGI.parse(args) + new_params = {} + params.each_pair do |key,value| + new_params[key] = value.join "," + end + [base,new_params] + end + + end + + + module GraphAPIMethods # A client for the Facebook Graph API. # @@ -44,8 +95,9 @@ def get_objects(ids, args = {}) def get_connections(id, connection_name, args = {}) # Fetchs the connections for given object. - graph_call("#{id}/#{connection_name}", args)["data"] + GraphCollection.new graph_call("#{id}/#{connection_name}", args) end + def get_picture(object, args = {}) result = graph_call("#{object}/picture", args, "get", :http_component => :headers)