Sha256: d13bb504af69ad2375eceea0400f94abf93d90de2818acf0756b6965222dc353

Contents?: true

Size: 1.77 KB

Versions: 3

Compression:

Stored size: 1.77 KB

Contents

module Koala
  module Facebook
    class GraphCollection < Array
      # This class is a light wrapper for collections returned
      # from the Graph API.
      #
      # It extends Array to allow direct access to the data colleciton
      # which should allow it to drop in seamlessly.
      #
      # It also allows access to paging information and the
      # ability to get the next/previous page in the collection
      # by calling next_page or previous_page.
      attr_reader :paging, :api, :raw_response

      def self.evaluate(response, api)
        # turn the response into a GraphCollection if it's pageable; if not, return the original response
        response.is_a?(Hash) && response["data"].is_a?(Array) ? self.new(response, api) : response
      end
    
      def initialize(response, api)
        super response["data"]
        @paging = response["paging"]
        @raw_response = response
        @api = api
      end

      # defines methods for NEXT and PREVIOUS pages
      %w{next previous}.each do |this|

        # def next_page
        # def previous_page
        define_method "#{this.to_sym}_page" do
          base, args = send("#{this}_page_params")
          base ? @api.get_page([base, args]) : nil
        end

        # def next_page_params
        # def previous_page_params
        define_method "#{this.to_sym}_page_params" do
          return nil unless @paging and @paging[this]
          parse_page_url(@paging[this])
        end
      end

      def parse_page_url(url)
        match = url.match(/.com\/(.*)\?(.*)/)
        base = match[1]
        args = match[2]
        params = CGI.parse(args)
        new_params = {}
        params.each_pair do |key,value|
          new_params[key] = value.join ","
        end
        [base,new_params]
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
koala-1.2.1 lib/koala/graph_collection.rb
koala-1.2.0 lib/koala/graph_collection.rb
koala-1.2.0beta4 lib/koala/graph_collection.rb