Sha256: 7f9c4be4c881cabe4e8c1a38126642a530303c01b705d5d93d5179b8a6f2372a

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 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

    module AuthenticatedRequest
      def self.included base
        
      end

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

        Shutl::Auth.logger.debug "requesting new access 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
        cache.read(:access_token)
      end

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

      def cache
        @cache ||= build_cache
      end

      private

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

Version data entries

1 entries across 1 versions & 1 rubygems

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