online_manager 0.0.2 → 0.0.3

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: dc1c0b7ad1198c4e4a02cb8b797b647b919571fd
4
- data.tar.gz: e1d2a97d952633e0e68023760cd426f6b98c6a3c
3
+ metadata.gz: 24f8a91946ab2eb1b0585331886f4e70c253e70e
4
+ data.tar.gz: 793ac0de4e25815d5d2425ed462ea6e1538f480b
5
5
  SHA512:
6
- metadata.gz: f505a05f4324e4ac45ea09d940ff7e76a4da416e5ebbc91450ecf2e5bb89c53be3145cff0c10078f4c37b1c69bc4dd1791efc54a9b20036b567c0cb233840360
7
- data.tar.gz: 278a2c80e9688f6d9df62e263b2a0391f2151ab0eae0f5c94baaa86726e95ca6efa5db1822c8b9268fb68d5a16305c61583f50e83f7f6d688a7be3f112fc38ac
6
+ metadata.gz: 4da8e86883f70f5170a725e36dea4ae991d8b228bf26c238731f61fd9c0590aab6af8e6070801523dd2e18892e480aa238f5f97d30a8b60943caec7717673282
7
+ data.tar.gz: 7271777e28da50652f36a64f507ae0fb85f69548c56cadf88799197287457558d90e719c71811743b83ac1e087b98e33cf36b6c2172d3aea10f12c364f5c1ea3
data/.gitignore CHANGED
@@ -12,3 +12,4 @@
12
12
  *.o
13
13
  *.a
14
14
  mkmf.log
15
+ *.gem
data/README.md CHANGED
@@ -30,31 +30,35 @@ require 'online_manager'
30
30
  # You should set all the users offline when it first runs,
31
31
  # since otherwise you'll be left with users marked online from when it last quit.
32
32
 
33
- # This runs an eventmachine reactor, so the call to .run blocks.
34
- OnlineManager.run do |c|
33
+ class OnlineConfig
35
34
 
36
35
  # How long since a heartbeat was last received until the user is considered offline? (in seconds)
37
- c.timeout 5
36
+ def timeout
37
+ 5
38
+ end
38
39
 
39
40
  # All the blocks below will be called inside an eventmachine reactor.
40
41
 
41
- c.setup do |seen|
42
+ def setup
42
43
  # Set up any websocket server/connection to pubsub server, etc
43
44
  # This must use eventmachine, otherwise it will block the reactor.
44
- # Then call seen.call(user_id) whenever you receive a heartbeat from a user.
45
+ # Then yield(user_id) whenever you receive a heartbeat from a user.
45
46
  end
46
47
 
47
- c.online do |user_id|
48
+ def online(user_id)
48
49
  puts "Booster #{user_id} online"
49
50
  # Update the user to online in the database or whatever
50
51
  # This must use asynchronous i/o, otherwise it will block the reactor.
51
52
  end
52
53
 
53
- c.offline do |user_id|
54
+ def offline(user_id)
54
55
  puts "Booster #{user_id} offline"
55
56
  # Called when a user transitions from online -> offline
56
57
  end
57
58
  end
59
+
60
+ # This runs an eventmachine reactor, so the call to .run blocks.
61
+ OnlineManager.run(OnlineConfig.new)
58
62
  ```
59
63
 
60
64
  If you already are in the context of an eventmachine reactor, or you want to run multiple of these in the same thread,
@@ -62,15 +66,13 @@ you can use `.setup`.
62
66
 
63
67
  ```ruby
64
68
  EM.run do
65
- OnlineManager.setup do |c|
66
- # Configure it
67
- end
69
+ OnlineManager.setup(OnlineConfig.new)
68
70
  end
69
71
  ```
70
72
 
71
73
  ## TODO
72
74
 
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.
75
+ * Write tests for this. Its pretty simple (~ 50 lines of code) and I have it running in production, so it should be fine.
74
76
  Check out [this][1] for testing eventmachine.
75
77
  * Consider setting remaining users offline in an ensure block, so it always leaves the statuses in a consistent state.
76
78
  [Could this replace setting users offline on initialization?][2]
@@ -1,10 +1,6 @@
1
1
  require 'eventmachine'
2
2
 
3
- require 'online_manager/dsl'
4
-
5
3
  class OnlineManager
6
- attr_writer :online_callback, :offline_callback, :setup_callback, :timeout
7
-
8
4
  def self.run(*args, &block)
9
5
  new(*args, &block).run
10
6
  end
@@ -13,16 +9,9 @@ class OnlineManager
13
9
  new(*args, &block).setup
14
10
  end
15
11
 
16
- def initialize
17
- @timeout = nil
12
+ def initialize(config)
13
+ @config = config
18
14
  @online_users = {}
19
- @online_callback = -> (_) { }
20
- @offline_callback = -> (_) { }
21
- @setup_callback = ->(_) { raise 'You must specify a setup callback' }
22
-
23
- yield DSL.new(self) if block_given?
24
-
25
- raise 'Timeout must be specified' unless @timeout
26
15
  end
27
16
 
28
17
  def run
@@ -30,18 +19,18 @@ class OnlineManager
30
19
  end
31
20
 
32
21
  def setup
33
- @setup_callback.call(method(:seen))
22
+ @config.setup(&method(:seen))
34
23
  end
35
24
 
36
25
  private
37
26
 
38
27
  def seen(id)
39
- @online_callback.call(id) unless online?(id)
28
+ @config.online(id) unless online?(id)
40
29
 
41
30
  time = Time.now
42
31
  @online_users[id] = time
43
32
 
44
- EM.add_timer(@timeout) do
33
+ EM.add_timer(@config.timeout) do
45
34
  set_offline(id, time)
46
35
  end
47
36
  end
@@ -49,7 +38,7 @@ private
49
38
  def set_offline(id, time)
50
39
  if @online_users[id] == time
51
40
  @online_users.delete(id)
52
- @offline_callback.call(id)
41
+ @config.offline(id)
53
42
  end
54
43
  end
55
44
 
@@ -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.2'
7
+ spec.version = '0.0.3'
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.2
4
+ version: 0.0.3
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-11 00:00:00.000000000 Z
11
+ date: 2014-08-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,7 +80,6 @@ files:
80
80
  - README.md
81
81
  - Rakefile
82
82
  - lib/online_manager.rb
83
- - lib/online_manager/dsl.rb
84
83
  - online_manager.gemspec
85
84
  homepage: https://github.com/cameron-martin/online_manager
86
85
  licenses:
@@ -1,23 +0,0 @@
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