Sha256: deb7fb36af54ca29bab70d815dfd37adbae7a91c57dd28c9ecabc1dad94f034d

Contents?: true

Size: 810 Bytes

Versions: 10

Compression:

Stored size: 810 Bytes

Contents

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

    def initialize(collection)
      # allow nil input
      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-0.4.0 lib/fb_graph/collection.rb
fb_graph-0.3.0 lib/fb_graph/collection.rb
fb_graph-0.2.2 lib/fb_graph/collection.rb
fb_graph-0.2.1 lib/fb_graph/collection.rb
fb_graph-0.2.0 lib/fb_graph/collection.rb
fb_graph-0.1.3 lib/fb_graph/collection.rb
fb_graph-0.1.2 lib/fb_graph/collection.rb
fb_graph-0.1.1 lib/fb_graph/collection.rb
fb_graph-0.1.0 lib/fb_graph/collection.rb
fb_graph-0.0.8 lib/fb_graph/collection.rb