Class: Lux::Current::Session
Overview
vars Lux.config.session_cookie_name Lux.config.session_cookie_max_age Lux.config.session_cookie_domain
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #delete(key) ⇒ Object
- #generate_cookie ⇒ Object
- #hash ⇒ Object
-
#initialize(request) ⇒ Session
constructor
A new instance of Session.
- #merge!(hash = {}) ⇒ Object
Constructor Details
#initialize(request) ⇒ Session
Returns a new instance of Session.
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/lux/current/lib/session.rb', line 7 def initialize request # how long will session last if BROWSER or IP change Lux.config.session_forced_validity ||= 10.minutes.to_i # name of the session cookie @cookie_name = Lux.config. ||= 'lux_' + Crypt.sha1(Lux.config.secret)[0,4].downcase @request = request @session = JSON.parse(Crypt.decrypt(request.[@cookie_name] || '{}')) rescue {} security_check end |
Instance Method Details
#[](key) ⇒ Object
19 20 21 |
# File 'lib/lux/current/lib/session.rb', line 19 def [] key @session[key.to_s.downcase] end |
#[]=(key, value) ⇒ Object
23 24 25 |
# File 'lib/lux/current/lib/session.rb', line 23 def []= key, value @session[key.to_s.downcase] = value end |
#delete(key) ⇒ Object
27 28 29 |
# File 'lib/lux/current/lib/session.rb', line 27 def delete key @session.delete key.to_s.downcase end |
#generate_cookie ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/lux/current/lib/session.rb', line 31 def encrypted = Crypt.encrypt(@session.to_json) if @request.[@cookie_name] != encrypted = [] .push [@cookie_name, encrypted].join('=') .push 'Max-Age=%s' % (Lux.config. || 1.week.to_i) .push "Path=/" .push "Domain=#{Lux.config.}" if Lux.config. .push "secure" if Lux.config.host.include?('https:') .push "HttpOnly" .join('; ') else nil end end |
#hash ⇒ Object
53 54 55 |
# File 'lib/lux/current/lib/session.rb', line 53 def hash @session.dup end |
#merge!(hash = {}) ⇒ Object
49 50 51 |
# File 'lib/lux/current/lib/session.rb', line 49 def merge! hash={} hash.keys.each { |k| self[k] = hash[k] } end |