Sha256: 9615a6deff535c31f47a309966f1311a78455486b56a7617e63fc40106ae02b2

Contents?: true

Size: 791 Bytes

Versions: 10

Compression:

Stored size: 791 Bytes

Contents

module FbGraph
  class Collection < Array
    attr_reader :previous, :next

    def initialize(collection = nil)
      collection ||= {:data => []}
      result = replace(collection[:data])
      @previous, @next = {}, {}
      if (paging = collection[:paging])
        if paging[:previous]
          @previous = fetch_params(paging[:previous])
        end
        if paging[:next]
          @next = fetch_params(paging[:next])
        end
      end
    end

    private

    def fetch_params(url)
      query = URI.parse(url).query
      params = {}
      query.split('&').each do |q|
        key, value = q.split('=')
        params[key] = URI.unescape(value)
      end
      params.delete_if do |k, v|
        !['limit', 'offset', 'until', 'since'].include?(k)
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
fb_graph-1.0.2 lib/fb_graph/collection.rb
fb_graph-1.0.1 lib/fb_graph/collection.rb
fb_graph-1.0.0 lib/fb_graph/collection.rb
fb_graph-0.8.0 lib/fb_graph/collection.rb
fb_graph-0.7.3 lib/fb_graph/collection.rb
fb_graph-0.7.2 lib/fb_graph/collection.rb
fb_graph-0.7.1 lib/fb_graph/collection.rb
fb_graph-0.7.0 lib/fb_graph/collection.rb
fb_graph-0.6.0 lib/fb_graph/collection.rb
fb_graph-0.5.0 lib/fb_graph/collection.rb