online_manager 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8e32e1d5904eb6f6501749b2a8482e9c9bdb7cb6
4
- data.tar.gz: 6893b5355f2cb040e10303c43e45e71c47fd2e3b
3
+ metadata.gz: dc1c0b7ad1198c4e4a02cb8b797b647b919571fd
4
+ data.tar.gz: e1d2a97d952633e0e68023760cd426f6b98c6a3c
5
5
  SHA512:
6
- metadata.gz: 0412b45abb095758e52abc9119dc63eaba311ea91d3df6316a40bd871ce26a8323a2e475d1adaf8b223026e081bc26d51b561e7470ca6428b972c0fe8e25cbaf
7
- data.tar.gz: 942b82fe3985454bcd803e8efcb677055e24996f64288acf97809606364c280b01909e4383f225f55a3d6f362f3d5c2caf973a1df6fb6eb2716067d9e40eabcd
6
+ metadata.gz: f505a05f4324e4ac45ea09d940ff7e76a4da416e5ebbc91450ecf2e5bb89c53be3145cff0c10078f4c37b1c69bc4dd1791efc54a9b20036b567c0cb233840360
7
+ data.tar.gz: 278a2c80e9688f6d9df62e263b2a0391f2151ab0eae0f5c94baaa86726e95ca6efa5db1822c8b9268fb68d5a16305c61583f50e83f7f6d688a7be3f112fc38ac
data/README.md CHANGED
@@ -27,10 +27,14 @@ Or install it yourself as:
27
27
  ```ruby
28
28
  require 'online_manager'
29
29
 
30
+ # You should set all the users offline when it first runs,
31
+ # since otherwise you'll be left with users marked online from when it last quit.
32
+
30
33
  # This runs an eventmachine reactor, so the call to .run blocks.
31
34
  OnlineManager.run do |c|
32
35
 
33
- c.timeout 5 # How long since a heartbeat was last received until the user is considered offline? (in seconds)
36
+ # How long since a heartbeat was last received until the user is considered offline? (in seconds)
37
+ c.timeout 5
34
38
 
35
39
  # All the blocks below will be called inside an eventmachine reactor.
36
40
 
@@ -43,7 +47,7 @@ OnlineManager.run do |c|
43
47
  c.online do |user_id|
44
48
  puts "Booster #{user_id} online"
45
49
  # Update the user to online in the database or whatever
46
- # This must be asynchronous i/o, otherwise it will block the reactor.
50
+ # This must use asynchronous i/o, otherwise it will block the reactor.
47
51
  end
48
52
 
49
53
  c.offline do |user_id|
@@ -53,10 +57,23 @@ OnlineManager.run do |c|
53
57
  end
54
58
  ```
55
59
 
60
+ If you already are in the context of an eventmachine reactor, or you want to run multiple of these in the same thread,
61
+ you can use `.setup`.
62
+
63
+ ```ruby
64
+ EM.run do
65
+ OnlineManager.setup do |c|
66
+ # Configure it
67
+ end
68
+ end
69
+ ```
70
+
56
71
  ## TODO
57
72
 
58
73
  * 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
74
  Check out [this][1] for testing eventmachine.
75
+ * Consider setting remaining users offline in an ensure block, so it always leaves the statuses in a consistent state.
76
+ [Could this replace setting users offline on initialization?][2]
60
77
 
61
78
  ## Contributing
62
79
 
@@ -67,4 +84,5 @@ end
67
84
  5. Create a new Pull Request
68
85
 
69
86
 
70
- [1]: https://github.com/jcoglan/rspec-eventmachine
87
+ [1]: https://github.com/jcoglan/rspec-eventmachine
88
+ [2]: http://stackoverflow.com/questions/25235089/when-will-rubys-ensure-not-run
@@ -1,13 +1,18 @@
1
1
  require 'eventmachine'
2
2
 
3
- class OnlineManager
3
+ require 'online_manager/dsl'
4
4
 
5
+ class OnlineManager
5
6
  attr_writer :online_callback, :offline_callback, :setup_callback, :timeout
6
7
 
7
8
  def self.run(*args, &block)
8
9
  new(*args, &block).run
9
10
  end
10
11
 
12
+ def self.setup(*args, &block)
13
+ new(*args, &block).setup
14
+ end
15
+
11
16
  def initialize
12
17
  @timeout = nil
13
18
  @online_users = {}
@@ -15,18 +20,20 @@ class OnlineManager
15
20
  @offline_callback = -> (_) { }
16
21
  @setup_callback = ->(_) { raise 'You must specify a setup callback' }
17
22
 
18
- yield CallbackDSL.new(self) if block_given?
23
+ yield DSL.new(self) if block_given?
19
24
 
20
25
  raise 'Timeout must be specified' unless @timeout
21
26
  end
22
27
 
23
28
  def run
24
- EM.run do
25
- @setup_callback.call(method(:seen))
26
- end
29
+ EM.run { setup }
27
30
  end
28
31
 
29
- private
32
+ def setup
33
+ @setup_callback.call(method(:seen))
34
+ end
35
+
36
+ private
30
37
 
31
38
  def seen(id)
32
39
  @online_callback.call(id) unless online?(id)
@@ -49,27 +56,4 @@ class OnlineManager
49
56
  def online?(id)
50
57
  @online_users.has_key?(id)
51
58
  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
59
  end
@@ -0,0 +1,23 @@
1
+ class OnlineManager
2
+ class DSL
3
+ def initialize(target)
4
+ @target = target
5
+ end
6
+
7
+ def timeout(timeout)
8
+ @target.timeout = timeout
9
+ end
10
+
11
+ def online(&block)
12
+ @target.online_callback = block
13
+ end
14
+
15
+ def offline(&block)
16
+ @target.offline_callback = block
17
+ end
18
+
19
+ def setup(&block)
20
+ @target.setup_callback = block
21
+ end
22
+ end
23
+ end
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'online_manager'
7
- spec.version = '0.0.1'
7
+ spec.version = '0.0.2'
8
8
  spec.authors = ['Cameron Martin']
9
9
  spec.email = ['cameronmartin123@gmail.com']
10
10
  spec.summary = %q{Manage user's online status.}
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: online_manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cameron Martin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-10 00:00:00.000000000 Z
11
+ date: 2014-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,6 +80,7 @@ files:
80
80
  - README.md
81
81
  - Rakefile
82
82
  - lib/online_manager.rb
83
+ - lib/online_manager/dsl.rb
83
84
  - online_manager.gemspec
84
85
  homepage: https://github.com/cameron-martin/online_manager
85
86
  licenses: