Class: FbGraph::Collection

Inherits:
Array
  • Object
show all
Defined in:
lib/fb_graph/collection.rb

Direct Known Subclasses

Connection, Searchable::Result

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Collection) initialize(collection = nil)



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fb_graph/collection.rb', line 5

def initialize(collection = nil)
  collection = case collection
  when Array
    {:data => collection, :count => collection.size}
  when Hash
    collection[:data] ||= []

    # Fix for AdImage response data: field not being an array
    if collection[:data].is_a? Hash
      collection[:data] = collection[:data].values
    end
    collection
  when nil
    collection = {:data => [], :count => 0}
  else
    raise ArgumentError.new("Invalid collection")
  end

  # NOTE: Graph API returns {"data":{"to":[null]}} sometimes... :(
  collection[:data].delete_if(&:nil?)

  replace collection[:data]

  if (summary = collection[:summary]).present?
    @total_count = summary[:total_count]
    @unread_count = summary[:unread_count]
    @updated_time = Time.parse(summary[:updated_time]) if summary[:updated_time]
  else
    @total_count = collection[:count]
  end
  @previous, @next, @cursors = {}, {}, {}
  if (paging = collection[:paging])
    if paging[:previous]
      @previous = fetch_params(paging[:previous])
    end
    if paging[:next]
      @next = fetch_params(paging[:next])
    end
    if paging[:cursors]
      @cursors[:after] = paging[:cursors].try(:[], :after)
      @cursors[:before] = paging[:cursors].try(:[], :before)
    end
  end
end

Instance Attribute Details

- (Object) cursors (readonly)

Returns the value of attribute cursors



3
4
5
# File 'lib/fb_graph/collection.rb', line 3

def cursors
  @cursors
end

- (Object) next (readonly)

Returns the value of attribute next



3
4
5
# File 'lib/fb_graph/collection.rb', line 3

def next
  @next
end

- (Object) previous (readonly)

Returns the value of attribute previous



3
4
5
# File 'lib/fb_graph/collection.rb', line 3

def previous
  @previous
end

- (Object) total_count (readonly)

Returns the value of attribute total_count



3
4
5
# File 'lib/fb_graph/collection.rb', line 3

def total_count
  @total_count
end

- (Object) unread_count (readonly)

Returns the value of attribute unread_count



3
4
5
# File 'lib/fb_graph/collection.rb', line 3

def unread_count
  @unread_count
end

- (Object) updated_time (readonly)

Returns the value of attribute updated_time



3
4
5
# File 'lib/fb_graph/collection.rb', line 3

def updated_time
  @updated_time
end