class FbGraph2::Collection

Attributes

after[RW]
before[RW]
next[RW]
previous[RW]
summary[RW]
total_count[RW]

Public Class Methods

new(collection = []) click to toggle source
# File lib/fb_graph2/collection.rb, line 5
def initialize(collection = [])
  collection = normalize collection
  paginate collection[:paging]
  summarize collection[:summary]
  replace Array(collection[:data])
end

Private Instance Methods

normalize(collection) click to toggle source
# File lib/fb_graph2/collection.rb, line 14
def normalize(collection)
  case collection
  when Hash
    collection
  when Array
    {
      data:  collection,
      count: collection.size
    }
  else
    raise ArgumentError.new("Invalid collection")
  end
end
paginate(paging) click to toggle source
# File lib/fb_graph2/collection.rb, line 28
def paginate(paging)
  cursors  = paging.try(:[], :cursors)
  self.before   = cursors.try(:[], :before)
  self.after    = cursors.try(:[], :after)
  self.next     = params_in paging.try(:[], :next)
  self.previous = params_in paging.try(:[], :previous)
end
params_in(url) click to toggle source
# File lib/fb_graph2/collection.rb, line 44
def params_in(url)
  if url
    Rack::Utils.parse_nested_query(
      URI.parse(url).query
    ).inject({}) do |params, (key, value)|
      params.merge! key => value
    end.with_indifferent_access
  end
end
summarize(summary) click to toggle source
# File lib/fb_graph2/collection.rb, line 36
def summarize(summary)
  self.summary     = summary
  self.total_count = summary.try(:[], :total_count)
  if summary.try(:[], :updated_time)
    self.summary[:updated_time] = Time.parse summary[:updated_time]
  end
end