Sha256: be380f35949ce668ad69a49f6a1a524fdc151467267ebf26f925366229150e77

Contents?: true

Size: 1.99 KB

Versions: 1

Compression:

Stored size: 1.99 KB

Contents

module Twitter
  module Rails
    module Controller
      def self.included?(controller)
        controller.helper_method :twitter_session, :logged_in_via_twitter?
      end

      protected
      def twitter_session
        @twitter_session
      end

      def create_twitter_session
        request_access_token if request_token_available?

        if access_token_available?
          twitter_oauth.authorize_from_access(session['twitter_atoken'], session['twitter_asecret'])
          @twitter_session = Twitter::Base.new(twitter_oauth)
        end
      end

      def logged_in_via_twitter?
        !!twitter_session
      end

      def create_twitter_session_from_user_tokens(user=current_user)
        if user && !logged_in_via_twitter? && user.twitter_atoken && user.twitter_asecret
          twitter_oauth.authorize_from_access(user.twitter_atoken, user.twitter_asecret)
          twitter_session = Twitter::Base.new(twitter_oauth)
        end
      end

      def clear_twitter_session_information
        session[:twitter_atoken] = nil
        session[:twitter_asecret] = nil
      end

      def clear_user_twitter_tokens(user=current_user)
        user.update_attributes(:twitter_atoken => nil,
                               :twitter_asecret => nil)
      end

      def request_access_token
        oauth = self.twitter_oauth
        oauth.authorize_from_request(session['twitter_rtoken'], session['twitter_rsecret'])

        session['twitter_rtoken'] = nil
        session['twitter_rsecret'] = nil
        session['twitter_atoken'] = oauth.access_token.token
        session['twitter_asecret'] = oauth.access_token.secret
      end

      def request_token_available?
        session['twitter_rtoken'] && session['twitter_rsecret']
      end

      def access_token_available?
        session['twitter_atoken'] && session['twitter_asecret']
      end

      def twitter_oauth
        @twitter_oauth ||= Twitter::OAuth.new(ENV['TWITTER_CONSUMER_TOKEN'], ENV['TWITTER_CONSUMER_SECRET'])
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
szimek-twitter_rails-0.0.3 lib/twitter/rails/controller.rb