Class: LimitedSessions::Expiry
- Inherits:
-
Object
- Object
- LimitedSessions::Expiry
- Defined in:
- lib/limited_sessions/expiry.rb
Overview
Rack middleware that should be installed after the session handling middleware
Constant Summary collapse
- DEFAULT_OPTIONS =
{ recent_activity: nil, # eg: 2.hours max_session: nil # eg: 24.hours }
Instance Method Summary collapse
- #call(env) ⇒ Object
- #clear_session ⇒ Object
-
#initialize(app, options = {}) ⇒ Expiry
constructor
A new instance of Expiry.
- #logger ⇒ Object
- #session ⇒ Object
Constructor Details
#initialize(app, options = {}) ⇒ Expiry
Returns a new instance of Expiry.
9 10 11 12 |
# File 'lib/limited_sessions/expiry.rb', line 9 def initialize(app, ={}) @app = app = DEFAULT_OPTIONS.merge() end |
Instance Method Details
#call(env) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/limited_sessions/expiry.rb', line 14 def call(env) @env = env if [:recent_activity] if session[:last_visit] && (session[:last_visit] + [:recent_activity]) < Time.now.to_i logger.info "Session expired: no recent activity" clear_session end if [:recent_activity] > 600 # Rounds to the nearest 5 minutes to minimize writes when a DB is in use session[:last_visit] = (Time.now.to_f/300).ceil*300 else session[:last_visit] = (Time.now.to_f/10).ceil*10 end end if [:max_session] session[:first_visit] ||= Time.now.to_i if (session[:first_visit] + [:max_session]) < Time.now.to_i logger.info "Session expired: max session length reached" clear_session session[:first_visit] ||= Time.now.to_i end end @app.call(env) end |
#clear_session ⇒ Object
42 43 44 |
# File 'lib/limited_sessions/expiry.rb', line 42 def clear_session @env['rack.session'].clear end |
#logger ⇒ Object
45 46 47 |
# File 'lib/limited_sessions/expiry.rb', line 45 def logger (Rails.logger rescue nil) || @env['rack.logger'] end |
#session ⇒ Object
39 40 41 |
# File 'lib/limited_sessions/expiry.rb', line 39 def session @env['rack.session'] || {} end |