online_manager 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8e32e1d5904eb6f6501749b2a8482e9c9bdb7cb6
4
+ data.tar.gz: 6893b5355f2cb040e10303c43e45e71c47fd2e3b
5
+ SHA512:
6
+ metadata.gz: 0412b45abb095758e52abc9119dc63eaba311ea91d3df6316a40bd871ce26a8323a2e475d1adaf8b223026e081bc26d51b561e7470ca6428b972c0fe8e25cbaf
7
+ data.tar.gz: 942b82fe3985454bcd803e8efcb677055e24996f64288acf97809606364c280b01909e4383f225f55a3d6f362f3d5c2caf973a1df6fb6eb2716067d9e40eabcd
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in online_manager.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Cameron Martin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # OnlineManager
2
+
3
+ Manage whether users are online when you have a regular heartbeat, coming from a websocket for example.
4
+ It allows you to have frequent heartbeats, but you only update the user's status when they transition between online and offline states.
5
+ I have not run any benchmarks, but this is probably faster than updating your database every few seconds with a last_online timestamp.
6
+
7
+ It runs in an eventmachine reactor, and defines a simple DSL with callbacks which are executed when a user transitions between online and offline states.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'online_manager'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install online_manager
24
+
25
+ ## Usage
26
+
27
+ ```ruby
28
+ require 'online_manager'
29
+
30
+ # This runs an eventmachine reactor, so the call to .run blocks.
31
+ OnlineManager.run do |c|
32
+
33
+ c.timeout 5 # How long since a heartbeat was last received until the user is considered offline? (in seconds)
34
+
35
+ # All the blocks below will be called inside an eventmachine reactor.
36
+
37
+ c.setup do |seen|
38
+ # Set up any websocket server/connection to pubsub server, etc
39
+ # This must use eventmachine, otherwise it will block the reactor.
40
+ # Then call seen.call(user_id) whenever you receive a heartbeat from a user.
41
+ end
42
+
43
+ c.online do |user_id|
44
+ puts "Booster #{user_id} online"
45
+ # Update the user to online in the database or whatever
46
+ # This must be asynchronous i/o, otherwise it will block the reactor.
47
+ end
48
+
49
+ c.offline do |user_id|
50
+ puts "Booster #{user_id} offline"
51
+ # Called when a user transitions from online -> offline
52
+ end
53
+ end
54
+ ```
55
+
56
+ ## TODO
57
+
58
+ * Write tests for this. Its pretty simple (~ 50 lines of code excluding the DSL) and I have it running in production, so it should be fine.
59
+ Check out [this][1] for testing eventmachine.
60
+
61
+ ## Contributing
62
+
63
+ 1. Fork it ( https://github.com/cameron-martin/online_manager/fork )
64
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
65
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
66
+ 4. Push to the branch (`git push origin my-new-feature`)
67
+ 5. Create a new Pull Request
68
+
69
+
70
+ [1]: https://github.com/jcoglan/rspec-eventmachine
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,75 @@
1
+ require 'eventmachine'
2
+
3
+ class OnlineManager
4
+
5
+ attr_writer :online_callback, :offline_callback, :setup_callback, :timeout
6
+
7
+ def self.run(*args, &block)
8
+ new(*args, &block).run
9
+ end
10
+
11
+ def initialize
12
+ @timeout = nil
13
+ @online_users = {}
14
+ @online_callback = -> (_) { }
15
+ @offline_callback = -> (_) { }
16
+ @setup_callback = ->(_) { raise 'You must specify a setup callback' }
17
+
18
+ yield CallbackDSL.new(self) if block_given?
19
+
20
+ raise 'Timeout must be specified' unless @timeout
21
+ end
22
+
23
+ def run
24
+ EM.run do
25
+ @setup_callback.call(method(:seen))
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def seen(id)
32
+ @online_callback.call(id) unless online?(id)
33
+
34
+ time = Time.now
35
+ @online_users[id] = time
36
+
37
+ EM.add_timer(@timeout) do
38
+ set_offline(id, time)
39
+ end
40
+ end
41
+
42
+ def set_offline(id, time)
43
+ if @online_users[id] == time
44
+ @online_users.delete(id)
45
+ @offline_callback.call(id)
46
+ end
47
+ end
48
+
49
+ def online?(id)
50
+ @online_users.has_key?(id)
51
+ end
52
+
53
+ class CallbackDSL
54
+ def initialize(target)
55
+ @target = target
56
+ end
57
+
58
+ def timeout(timeout)
59
+ @target.timeout = timeout
60
+ end
61
+
62
+ def online(&block)
63
+ @target.online_callback = block
64
+ end
65
+
66
+ def offline(&block)
67
+ @target.offline_callback = block
68
+ end
69
+
70
+ def setup(&block)
71
+ @target.setup_callback = block
72
+ end
73
+ end
74
+
75
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'online_manager'
7
+ spec.version = '0.0.1'
8
+ spec.authors = ['Cameron Martin']
9
+ spec.email = ['cameronmartin123@gmail.com']
10
+ spec.summary = %q{Manage user's online status.}
11
+ spec.description = %q{Manage whether users are online when you have a regular heartbeat, coming from a websocket for example.}
12
+ spec.homepage = 'https://github.com/cameron-martin/online_manager'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'bundler', '~> 1.6'
21
+ spec.add_development_dependency 'rake', '~> 10.0'
22
+ spec.add_development_dependency 'rspec', '~> 3.0'
23
+
24
+ spec.add_dependency 'eventmachine', '~> 1.0'
25
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: online_manager
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Cameron Martin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: eventmachine
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ description: Manage whether users are online when you have a regular heartbeat, coming
70
+ from a websocket for example.
71
+ email:
72
+ - cameronmartin123@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - lib/online_manager.rb
83
+ - online_manager.gemspec
84
+ homepage: https://github.com/cameron-martin/online_manager
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.2.2
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Manage user's online status.
108
+ test_files: []
109
+ has_rdoc: