Sha256: e27062acfd7bc3289a662b2428946aebcb80c4188b828d0da97473e94de6ad72

Contents?: true

Size: 1.18 KB

Versions: 16

Compression:

Stored size: 1.18 KB

Contents

module Wework
  module Token
    class Base
      attr_accessor :client

      def initialize(client)
        @client = client
        raise RedisNotConfigException if redis.nil?
      end

      def token
        update_token if expired?
        redis.hget(redis_key, token_key)
      end

      def update_token
        result = refresh_token
        value = result.send(token_key)
        if value.nil?
          puts "#{self.class.name} refresh token error: #{result.inspect}"
        else
          expires_at = Time.now.to_i + result.expires_in.to_i - Wework.expired_shift_seconds

          redis.hmset(
            redis_key,
            token_key, value,
            "expires_at", expires_at
          )

          redis.expireat(redis_key, expires_at)
        end

        value
      end

      private

      def redis
        Wework.redis
      end

      def redis_key
        raise NotImplementedError
      end

      def token_key
        raise NotImplementedError
      end

      def refresh_token
        raise NotImplementedError
      end

      def expired?
        redis.hvals(redis_key).empty? || redis.hget(redis_key, 'expires_at').to_i <= Time.now.to_i
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
wework-next-1.2.6 lib/wework/token/base.rb
wework-1.2.8 lib/wework/token/base.rb
wework-1.2.5 lib/wework/token/base.rb
wework-1.2.3 lib/wework/token/base.rb
wework-1.2.2 lib/wework/token/base.rb
wework-1.2.1 lib/wework/token/base.rb
wework-1.2.0 lib/wework/token/base.rb
wework-1.1.9 lib/wework/token/base.rb
wework-1.1.8 lib/wework/token/base.rb
wework-1.1.7 lib/wework/token/base.rb
wework-1.1.6 lib/wework/token/base.rb
wework-1.1.5 lib/wework/token/base.rb
wework-1.1.3 lib/wework/token/base.rb
wework-1.1.2 lib/wework/token/base.rb
wework-1.1.1 lib/wework/token/base.rb
wework-1.1.0 lib/wework/token/base.rb