session_tracker 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm ruby-1.9.3-p0@session_tracker --create
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in session_tracker.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,6 @@
1
+ guard 'rspec', :version => 2, :all_on_start => false,
2
+ :all_after_pass => false, :bundler => false,
3
+ :keep_failed => false do
4
+ watch(%r{^spec/.+_spec\.rb$})
5
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
6
+ end
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # SessionTracker
2
+
3
+ Track active sessions in redis.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'session_tracker'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install session_tracker
18
+
19
+ ## Usage
20
+
21
+ In your ApplicationController:
22
+
23
+ before_filter :track_active_sessions
24
+
25
+ def track_active_sessions
26
+ SessionTracker.new("user", $redis).track(session[:session_id])
27
+ end
28
+
29
+ Then to view the current state:
30
+
31
+ SessionTracker.new("user", $redis).active_users
32
+
33
+ If redis is accessible through $redis, you don't have to give it as an argument to SessionTracker.new.
34
+
35
+ ## Contributing
36
+
37
+ 1. Fork it
38
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
39
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
40
+ 4. Push to the branch (`git push origin my-new-feature`)
41
+ 5. Create new Pull Request
42
+
43
+ ## Credits and license
44
+
45
+ Inspired by [http://www.lukemelia.com/blog/archives/2010/01/17/redis-in-practice-whos-online/](http://www.lukemelia.com/blog/archives/2010/01/17/redis-in-practice-whos-online/).
46
+
47
+ By [Joakim Kolsjö](https://github.com/joakimk) under the MIT license:
48
+
49
+ > Copyright (c) 2012 Joakim Kolsjö
50
+ >
51
+ > Permission is hereby granted, free of charge, to any person obtaining a copy
52
+ > of this software and associated documentation files (the "Software"), to deal
53
+ > in the Software without restriction, including without limitation the rights
54
+ > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
55
+ > copies of the Software, and to permit persons to whom the Software is
56
+ > furnished to do so, subject to the following conditions:
57
+ >
58
+ > The above copyright notice and this permission notice shall be included in
59
+ > all copies or substantial portions of the Software.
60
+ >
61
+ > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
62
+ > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
63
+ > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
64
+ > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
65
+ > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
66
+ > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
67
+ > THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ desc "Run all specs"
6
+ RSpec::Core::RakeTask.new('spec') do |t|
7
+ t.pattern = 'spec/**/*.rb'
8
+ end
9
+
10
+ task :default => :spec
@@ -0,0 +1,3 @@
1
+ class SessionTracker
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,35 @@
1
+ require 'time'
2
+
3
+ class SessionTracker
4
+ ONE_HOUR = 60 * 60
5
+
6
+ def initialize(type, redis = $redis)
7
+ @type = type
8
+ @redis = redis
9
+ end
10
+
11
+ def track(id, time = Time.now)
12
+ return unless id
13
+ key = key_for(time)
14
+ @redis.sadd(key, id)
15
+ @redis.expire(key, ONE_HOUR - 60)
16
+ rescue
17
+ # This is called for every request and is probably not essential for the app
18
+ # so we don't want to raise errors just because redis is down for a few seconds.
19
+ end
20
+
21
+ def active_users(time = Time.now)
22
+ @redis.sunion(*keys_for_last_3_minutes(time)).size
23
+ end
24
+
25
+ private
26
+
27
+ def keys_for_last_3_minutes(time)
28
+ times = 0.upto(2).map { |n| time - (n * 60) }
29
+ times.map { |t| key_for(t) }
30
+ end
31
+
32
+ def key_for(time)
33
+ "active_#{@type}_sessions_minute_#{time.strftime("%M")}"
34
+ end
35
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/session_tracker', __FILE__)
3
+ require File.expand_path('../lib/session_tracker/version', __FILE__)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.authors = ["Joakim Kolsjö"]
7
+ gem.email = ["joakim.kolsjo@gmail.com"]
8
+ gem.description = %q{Track active user sessions in redis}
9
+ gem.summary = %q{Track active user sessions in redis}
10
+ gem.homepage = ""
11
+
12
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
13
+ gem.files = `git ls-files`.split("\n")
14
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ gem.name = "session_tracker"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = SessionTracker::VERSION
18
+ gem.add_development_dependency "rake"
19
+ gem.add_development_dependency 'rspec'
20
+ gem.add_development_dependency "guard"
21
+ gem.add_development_dependency "guard-rspec"
22
+ end
@@ -0,0 +1,57 @@
1
+ require 'session_tracker'
2
+
3
+ describe SessionTracker, "track" do
4
+
5
+ let(:redis) { mock.as_null_object }
6
+
7
+ it "should store the user in a set for the current minute" do
8
+ time = Time.parse("15:04")
9
+ redis.should_receive(:sadd).with("active_customer_sessions_minute_04", "abc123")
10
+ tracker = SessionTracker.new("customer", redis)
11
+ tracker.track("abc123", time)
12
+ end
13
+
14
+ it "should expire the set within an hour to prevent it wrapping around" do
15
+ time = Time.parse("15:59")
16
+ redis.should_receive(:expire).with("active_customer_sessions_minute_59", 60 * 59)
17
+ tracker = SessionTracker.new("customer", redis)
18
+ tracker.track("abc123", time)
19
+ end
20
+
21
+ it "should be able to track different types of sessions" do
22
+ time = Time.parse("15:04")
23
+ redis.should_receive(:sadd).with("active_employee_sessions_minute_04", "abc456")
24
+ tracker = SessionTracker.new("employee", redis)
25
+ tracker.track("abc456", time)
26
+ end
27
+
28
+ it "should do nothing if the session id is nil" do
29
+ redis.should_not_receive(:sadd)
30
+ redis.should_not_receive(:expire)
31
+ tracker = SessionTracker.new("employee", redis)
32
+ tracker.track(nil)
33
+ end
34
+
35
+ it "should not raise any errors" do
36
+ redis.should_receive(:expire).and_raise('fail')
37
+ tracker = SessionTracker.new("customer", redis)
38
+ tracker.track("abc123", Time.now)
39
+ end
40
+
41
+ end
42
+
43
+ describe SessionTracker, "active_users" do
44
+
45
+ let(:redis) { mock.as_null_object }
46
+
47
+ it "should do a union on the last 3 minutes to get a active user count" do
48
+ time = Time.parse("13:09")
49
+ redis.should_receive(:sunion).with("active_customer_sessions_minute_09",
50
+ "active_customer_sessions_minute_08",
51
+ "active_customer_sessions_minute_07").
52
+ and_return([ mock, mock ])
53
+
54
+ SessionTracker.new("customer", redis).active_users(time).should == 2
55
+ end
56
+
57
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: session_tracker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Joakim Kolsjö
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &2152640440 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2152640440
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &2152639960 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2152639960
36
+ - !ruby/object:Gem::Dependency
37
+ name: guard
38
+ requirement: &2152639540 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2152639540
47
+ - !ruby/object:Gem::Dependency
48
+ name: guard-rspec
49
+ requirement: &2152639080 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *2152639080
58
+ description: Track active user sessions in redis
59
+ email:
60
+ - joakim.kolsjo@gmail.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - .gitignore
66
+ - .rvmrc
67
+ - Gemfile
68
+ - Guardfile
69
+ - README.md
70
+ - Rakefile
71
+ - lib/session_tracker.rb
72
+ - lib/session_tracker/version.rb
73
+ - session_tracker.gemspec
74
+ - spec/session_tracker_spec.rb
75
+ homepage: ''
76
+ licenses: []
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 1.8.5
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: Track active user sessions in redis
99
+ test_files:
100
+ - spec/session_tracker_spec.rb