Class: PuavoRest::Session

Inherits:
Hash
  • Object
show all
Includes:
LocalStore
Defined in:
resources/sessions.rb

Constant Summary

MAX_AGE =

Clear sessions after after 12 hours if they are not manually cleared during logout. Ie. on crash

60 * 60 * 12
UUID_CHARS =
["a".."z", "A".."Z", "0".."9"].reduce([]) do |memo, range|
  memo + range.to_a
end

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from LocalStore

close_connection, included, #local_store, #local_store_del, #local_store_expire, #local_store_get, #local_store_set

Methods inherited from Hash

#recursive_symbolize_keys!, #symbolize_keys, #symbolize_keys!

Class Method Details

+ (Object) all



75
76
77
78
79
# File 'resources/sessions.rb', line 75

def self.all
  keys.map do |k|
    new.merge! JSON.parse(local_store.get(k))
  end
end

+ (Object) by_hostname!(hostname)



55
56
57
58
59
60
61
# File 'resources/sessions.rb', line 55

def self.by_hostname!(hostname)
  uuid = local_store.get(hostname_key(hostname))
  if uuid.nil?
    raise NotFound, :user => "Cannot find session for hostname: #{ hostname }"
  end
  by_uuid!(uuid)
end

+ (Object) by_uuid!(uuid)



63
64
65
66
67
68
69
# File 'resources/sessions.rb', line 63

def self.by_uuid!(uuid)
  json = local_store.get(uuid_key(uuid))
  if json.nil?
    raise NotFound, :user => "Cannot find session for uuid: #{ uuid }"
  end
  new.merge(JSON.parse(json))
end

+ (Object) generate_uuid



17
18
19
# File 'resources/sessions.rb', line 17

def self.generate_uuid
  (0...50).map{ UUID_CHARS[rand(UUID_CHARS.size)] }.join
end

+ (Object) hostname_key(hostname)



25
26
27
# File 'resources/sessions.rb', line 25

def self.hostname_key(hostname)
  "session:hostname:#{ hostname }"
end

+ (Object) keys



71
72
73
# File 'resources/sessions.rb', line 71

def self.keys
  local_store.keys("session:hostname:*")
end

+ (Object) uuid_key(uuid)



29
30
31
# File 'resources/sessions.rb', line 29

def self.uuid_key(uuid)
  "session:uuid:#{ uuid }"
end

Instance Method Details

- (Object) destroy



50
51
52
53
# File 'resources/sessions.rb', line 50

def destroy
  local_store.del(uuid_key)
  local_store.del(hostname_key) if device?
end

- (Boolean) device?

Returns:

  • (Boolean)


36
37
38
# File 'resources/sessions.rb', line 36

def device?
  !self["device"].nil?
end

- (Object) hostname_key



22
23
24
# File 'resources/sessions.rb', line 22

def hostname_key
  self.class.hostname_key("#{ self["device"]["hostname"] }")
end

- (Object) save



40
41
42
43
44
45
46
47
48
# File 'resources/sessions.rb', line 40

def save
  local_store.set(uuid_key, self.to_json)
  local_store.expire(uuid_key, MAX_AGE)

  if device?
    local_store.set(hostname_key, self["uuid"])
    local_store.expire(hostname_key, MAX_AGE)
  end
end

- (Object) uuid_key



32
33
34
# File 'resources/sessions.rb', line 32

def uuid_key
  self.class.uuid_key("#{ self["uuid"] }")
end