Class: Ooor::SessionHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/ooor/session_handler.rb

Instance Method Summary collapse

Instance Method Details

#connection_spec(config) ⇒ Object



8
9
10
# File 'lib/ooor/session_handler.rb', line 8

def connection_spec(config)
  HashWithIndifferentAccess.new(config.slice(:url, :username, :password, :database, :scope_prefix, :helper_paths)) #TODO should really password be part of it?
end

#connectionsObject



61
# File 'lib/ooor/session_handler.rb', line 61

def connections; @connections ||= {}; end

#create_new_connection(config, spec) ⇒ Object



42
43
44
45
# File 'lib/ooor/session_handler.rb', line 42

def create_new_connection(config, spec)
  config = Ooor.default_config.merge(config) if Ooor.default_config.is_a? Hash
  Connection.new(config)
end

#create_new_session(config, web_session, id = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/ooor/session_handler.rb', line 21

def create_new_session(config, web_session, id=nil)
  c_spec = connection_spec(config)
  if connections[c_spec]
    Ooor::Session.new(connections[c_spec], web_session, id)
  else
    Ooor::Session.new(create_new_connection(config, c_spec), web_session, id).tap do |s|
      connections[c_spec] = s.connection
    end
  end
end

#get_web_session(key) ⇒ Object



52
53
54
# File 'lib/ooor/session_handler.rb', line 52

def get_web_session(key)
  Ooor.cache.read(key)
end

#register_session(session) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/ooor/session_handler.rb', line 32

def register_session(session)
  if session.config[:session_sharing]
    spec = session.web_session[:session_id]
  else
    spec= session.id
  end
  set_web_session(spec, session.web_session)
  sessions[spec] = session
end

#reset!Object



47
48
49
50
# File 'lib/ooor/session_handler.rb', line 47

def reset!
  @sessions = {}
  @connections = {}
end

#retrieve_session(config, id = nil, web_session = {}) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/ooor/session_handler.rb', line 12

def retrieve_session(config, id=nil, web_session={})
  id ||= SecureRandom.hex(16)
  if config[:reload] || !s = sessions[id]
    create_new_session(config, web_session, id)
  else
    s.tap {|s| s.web_session.merge!(web_session)} #TODO merge config also?
  end
end

#sessionsObject



60
# File 'lib/ooor/session_handler.rb', line 60

def sessions; @sessions ||= {}; end

#set_web_session(key, web_session) ⇒ Object



56
57
58
# File 'lib/ooor/session_handler.rb', line 56

def set_web_session(key, web_session)
  Ooor.cache.write(key, web_session)
end