Class: Sinapse::Authentication

Inherits:
Struct
  • Object
show all
Defined in:
lib/sinapse/authentication.rb

Overview

TODO: #get to return the token (if any)

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#recordObject

Returns the value of attribute record

Returns:

  • (Object)

    the current value of record



9
10
11
# File 'lib/sinapse/authentication.rb', line 9

def record
  @record
end

Instance Method Details

#clearObject



27
28
29
30
31
32
33
34
# File 'lib/sinapse/authentication.rb', line 27

def clear
  Sinapse.redis do |redis|
    if token = redis.get(key)
      redis.del(token_key(token))
      redis.del(key)
    end
  end
end

#generateObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/sinapse/authentication.rb', line 15

def generate
  Sinapse.redis do |redis|
    loop do
      token = Sinapse.generate_token
      if redis.setnx(token_key(token), record.to_param)
        redis.set(key, token)
        return token
      end
    end
  end
end

#keyObject



40
41
42
# File 'lib/sinapse/authentication.rb', line 40

def key
  "sinapse:#{record.class.name}:#{record.to_param}"
end

#resetObject



10
11
12
13
# File 'lib/sinapse/authentication.rb', line 10

def reset
  clear
  generate
end

#token_key(token) ⇒ Object



36
37
38
# File 'lib/sinapse/authentication.rb', line 36

def token_key(token)
  "sinapse:tokens:#{token}"
end