Class: Koala::Facebook::API::GraphCollection
- Inherits:
-
Array
- Object
- Array
- Koala::Facebook::API::GraphCollection
- Defined in:
- lib/koala/api/graph_collection.rb
Overview
A light wrapper for collections returned from the Graph API. It extends Array to allow you to page backward and forward through result sets, and providing easy access to paging information.
Instance Attribute Summary collapse
-
#api ⇒ Koala::Facebook::GraphAPI
readonly
The api used to make requests.
-
#headers ⇒ Object
readonly
The headers from the Facebook response.
-
#paging ⇒ Object
readonly
The raw paging information from Facebook (next/previous URLs).
-
#raw_response ⇒ Object
readonly
The entire raw response from Facebook.
-
#summary ⇒ Object
readonly
The raw summary information from Facebook (total counts).
Class Method Summary collapse
-
.is_pageable?(response) ⇒ Boolean
response will always be an instance of Koala::HTTPService::Response since that is what we get from Koala::Facebook::API#api.
-
.parse_page_url(url) ⇒ Object
Parse the previous and next page URLs Facebook provides in pageable results.
Instance Method Summary collapse
-
#initialize(response, api) ⇒ Koala::Facebook::API::GraphCollection
constructor
Initialize the array of results and store various additional paging-related information.
-
#next_page(extra_params = {}) ⇒ Object
Retrieve the next page of results.
-
#next_page_params ⇒ Object
Arguments that can be sent to #graph_call to retrieve the next page of results.
-
#previous_page(extra_params = {}) ⇒ Object
Retrieve the previous page of results.
-
#previous_page_params ⇒ Object
Arguments that can be sent to #graph_call to retrieve the previous page of results.
Constructor Details
#initialize(response, api) ⇒ Koala::Facebook::API::GraphCollection
Initialize the array of results and store various additional paging-related information.
30 31 32 33 34 35 36 37 |
# File 'lib/koala/api/graph_collection.rb', line 30 def initialize(response, api) super response.data["data"] @paging = response.data["paging"] @summary = response.data["summary"] @raw_response = response.data @api = api @headers = response.headers end |
Instance Attribute Details
#api ⇒ Koala::Facebook::GraphAPI (readonly)
Returns the api used to make requests.
16 17 18 |
# File 'lib/koala/api/graph_collection.rb', line 16 def api @api end |
#headers ⇒ Object (readonly)
The headers from the Facebook response
20 21 22 |
# File 'lib/koala/api/graph_collection.rb', line 20 def headers @headers end |
#paging ⇒ Object (readonly)
The raw paging information from Facebook (next/previous URLs).
12 13 14 |
# File 'lib/koala/api/graph_collection.rb', line 12 def paging @paging end |
#raw_response ⇒ Object (readonly)
The entire raw response from Facebook.
18 19 20 |
# File 'lib/koala/api/graph_collection.rb', line 18 def raw_response @raw_response end |
#summary ⇒ Object (readonly)
The raw summary information from Facebook (total counts).
14 15 16 |
# File 'lib/koala/api/graph_collection.rb', line 14 def summary @summary end |
Class Method Details
.is_pageable?(response) ⇒ Boolean
response will always be an instance of Koala::HTTPService::Response since that is what we get from Koala::Facebook::API#api
52 53 54 |
# File 'lib/koala/api/graph_collection.rb', line 52 def self.is_pageable?(response) response.data.is_a?(Hash) && response.data["data"].is_a?(Array) end |
.parse_page_url(url) ⇒ Object
Parse the previous and next page URLs Facebook provides in pageable results. You’ll mainly need to use this when using a non-Rails framework (one without url_for); to store paging information between page loads, pass the URL (from GraphCollection#paging) and use parse_page_url to turn it into parameters useful for Koala::Facebook::API#get_page.
113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/koala/api/graph_collection.rb', line 113 def self.parse_page_url(url) uri = Addressable::URI.parse(url) base = uri.path.sub(/^\//, '') params = CGI.parse(uri.query) new_params = {} params.each_pair do |key,value| new_params[key] = value.join "," end [base,new_params] end |
Instance Method Details
#next_page(extra_params = {}) ⇒ Object
Retrieve the next page of results.
65 66 67 68 |
# File 'lib/koala/api/graph_collection.rb', line 65 def next_page(extra_params = {}) base, args = next_page_params base ? @api.get_page([base, args.merge(extra_params)]) : nil end |
#next_page_params ⇒ Object
Arguments that can be sent to Koala::Facebook::API#graph_call to retrieve the next page of results.
86 87 88 |
# File 'lib/koala/api/graph_collection.rb', line 86 def next_page_params @paging && @paging["next"] ? parse_page_url(@paging["next"]) : nil end |
#previous_page(extra_params = {}) ⇒ Object
Retrieve the previous page of results.
75 76 77 78 |
# File 'lib/koala/api/graph_collection.rb', line 75 def previous_page(extra_params = {}) base, args = previous_page_params base ? @api.get_page([base, args.merge(extra_params)]) : nil end |
#previous_page_params ⇒ Object
Arguments that can be sent to Koala::Facebook::API#graph_call to retrieve the previous page of results.
96 97 98 |
# File 'lib/koala/api/graph_collection.rb', line 96 def previous_page_params @paging && @paging["previous"] ? parse_page_url(@paging["previous"]) : nil end |