Class: Koala::Facebook::API::GraphCollection

Inherits:
Array
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, api) ⇒ Koala::Facebook::GraphCollection

Initialize the array of results and store various additional paging-related information.

Parameters:

  • response

    the response from Facebook (a hash whose “data” key is an array)

  • api

    the Graph API instance to use to make calls (usually the API that made the original call).



26
27
28
29
30
31
# File 'lib/koala/api/graph_collection.rb', line 26

def initialize(response, api)
  super response["data"]
  @paging = response["paging"]
  @raw_response = response
  @api = api
end

Instance Attribute Details

#apiKoala::Facebook::GraphAPI (readonly)

Returns the api used to make requests.

Returns:

  • (Koala::Facebook::GraphAPI)

    the api used to make requests.



14
15
16
# File 'lib/koala/api/graph_collection.rb', line 14

def api
  @api
end

#pagingObject (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_responseObject (readonly)

The entire raw response from Facebook.



16
17
18
# File 'lib/koala/api/graph_collection.rb', line 16

def raw_response
  @raw_response
end

Class Method Details

.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.

Parameters:

  • url

    the paging URL to turn into graph_call parameters

Returns:

  • an array of parameters that can be provided via graph_call(*parsed_params)



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/koala/api/graph_collection.rb', line 99

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.

Examples:

With optional extra params

wall = api.get_connections("me", "feed", since: 1379593891)
wall.next_page(since: 1379593891)

Parameters:

Returns:

  • a GraphCollection array of additional results (an empty array if there are no more results)



51
52
53
54
# File 'lib/koala/api/graph_collection.rb', line 51

def next_page(extra_params = {})
  base, args = next_page_params
  base ? @api.get_page([base, args.merge(extra_params)]) : nil
end

#next_page_paramsObject

Arguments that can be sent to Koala::Facebook::API#graph_call to retrieve the next page of results.

Examples:

@api.graph_call(*collection.next_page_params)

Returns:

  • an array of arguments, or nil if there are no more pages



72
73
74
# File 'lib/koala/api/graph_collection.rb', line 72

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.

Parameters:

Returns:

  • a GraphCollection array of additional results (an empty array if there are no earlier results)



61
62
63
64
# File 'lib/koala/api/graph_collection.rb', line 61

def previous_page(extra_params = {})
  base, args = previous_page_params
  base ? @api.get_page([base, args.merge(extra_params)]) : nil
end

#previous_page_paramsObject

Arguments that can be sent to Koala::Facebook::API#graph_call to retrieve the previous page of results.

Examples:

@api.graph_call(*collection.previous_page_params)

Returns:

  • an array of arguments, or nil if there are no previous pages



82
83
84
# File 'lib/koala/api/graph_collection.rb', line 82

def previous_page_params
  @paging && @paging["previous"] ? parse_page_url(@paging["previous"]) : nil
end