Sha256: 1fdee851bd0a80aed0ad60a7dddf61cd02004774e969ab402a9770e5212c52de

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

# encoding: utf-8

require 'koala'

module DeviseFacebookOpenGraph
  module Facebook
    class Session
      #
      # Keys found in cookie content. We are supplying reader 
      # methods for these values which are read from cookie content
      #
      FACEBOOK_SESSION_KEYS = %w(session_key expires uid sig secret access_token)

      def self.new_or_nil_if_invalid(cookies)
        session = new(cookies)
        return session if session.valid?
      end

      #
      # Creates a new Facebook session based cookies hash from a request
      #
      def initialize(cookies)
        @cookies = cookies
      end

      #
      # Returns facebook's cookie content
      #
      def cookie_content
        @cookie_content ||= parse_cookie
      end
      
      #
      # Gives access to query as user with an oauth access token fetched from the cookie content
      #
      def graph
        Koala::Facebook::GraphAPI.new(access_token)
      end


      #
      # Define reader methods for facebook session keys
      #
      FACEBOOK_SESSION_KEYS.each do |key|
        define_method key do
          cookie_content[key] if cookie_content
        end
      end

      #
      # Is this a valid session? True if we were able to parse facebook's cookie content
      #
      def valid?
        !!cookie_content
      end
      
      private
        def parse_cookie # :nodoc:
          oauth = Koala::Facebook::OAuth.new(Config.application_id, Config.application_secret)
          oauth.get_user_from_cookie(@cookies)
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
devise_facebook_open_graph-0.0.3 lib/devise_facebook_open_graph/facebook/session.rb