Sha256: eba9ee1182a4b86c268bdaa5d2e99b64072b07e88f3879481bfc62a6f0a35b8c

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

#can be included in for example a Rails controller to enable easily
#authenticating requests
module Shutl
  module Auth
    class Cache
      def initialize
        @cache = {}
      end

      def read(key)
        @cache[key]
      end

      def write(key, value)
        @cache[key] = value
      end
    end

    def self.cache
      @cache ||= build_cache
    end

    private

    def self.build_cache
      if Kernel.const_defined?(:Rails)
        ::Rails.cache
      else
        Cache.new
      end
    end

    module AuthenticatedRequest
      def self.included base
        
      end

      def request_access_token
        return read_token if read_token
        Shutl::Auth.logger.debug "request_access_token: cached? #{!!read_token}"

        Shutl::Auth.access_token!
      end

      def access_token
        return read_token if read_token
        set_token request_access_token
      end

      def authenticated_request &blk
        begin
          yield
        rescue Shutl::UnauthorizedAccess => e
          Shutl::Auth.logger.debug "Shutl::UnauthorizedAccess - resetting access token"
          set_token nil
          access_token
          yield
        end
      end

      def read_token
        Shutl::Auth.cache.read(:access_token)
      end

      def set_token(token)
        Shutl::Auth.cache.write(:access_token, token)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shutl_auth-0.8.5 lib/shutl/auth/authenticated_request.rb