BigW Consortium Gitlab

chat_name_token.rb 785 Bytes
Newer Older
1 2 3 4 5 6 7
require 'json'

module Gitlab
  class ChatNameToken
    attr_reader :token

    TOKEN_LENGTH = 50
8
    EXPIRY_TIME = 10.minutes
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

    def initialize(token = new_token)
      @token = token
    end

    def get
      Gitlab::Redis.with do |redis|
        data = redis.get(redis_key)
        JSON.parse(data, symbolize_names: true) if data
      end
    end

    def store!(params)
      Gitlab::Redis.with do |redis|
        params = params.to_json
        redis.set(redis_key, params, ex: EXPIRY_TIME)
        token
      end
    end

    def delete
      Gitlab::Redis.with do |redis|
        redis.del(redis_key)
      end
    end

    private

    def new_token
      Devise.friendly_token(TOKEN_LENGTH)
    end

    def redis_key
      "gitlab:chat_names:#{token}"
    end
  end
end