Class: SessionTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/session_tracker.rb,
lib/session_tracker/version.rb

Constant Summary collapse

ONE_HOUR =
60 * 60
VERSION =
"0.0.5"

Instance Method Summary collapse

Constructor Details

#initialize(type, redis = $redis) ⇒ SessionTracker

Returns a new instance of SessionTracker.



6
7
8
9
# File 'lib/session_tracker.rb', line 6

def initialize(type, redis = $redis)
  @type = type
  @redis = redis
end

Instance Method Details

#active_users(timespan_in_minutes = 5, time = Time.now) ⇒ Object



25
26
27
# File 'lib/session_tracker.rb', line 25

def active_users(timespan_in_minutes = 5, time = Time.now)
  active_users_data(timespan_in_minutes, time).size
end

#active_users_data(timespan_in_minutes, time) ⇒ Object



21
22
23
# File 'lib/session_tracker.rb', line 21

def active_users_data(timespan_in_minutes, time)
  @redis.sunion(*keys_within(timespan_in_minutes, time))
end

#track(id, time = Time.now) ⇒ Object



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

def track(id, time = Time.now)
  return unless id
  key = key_for(time)
  @redis.sadd(key, id)
  @redis.expire(key, ONE_HOUR - 60)
rescue
  # This is called for every request and is probably not essential for the app
  # so we don't want to raise errors just because redis is down for a few seconds.
end