magicbus 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -0,0 +1,4 @@
1
+ magicbus
2
+ ========
3
+
4
+ Sinatra-ready RabbitMQ wrapper for message routing with AMQP.
data/lib/magicbus.rb CHANGED
@@ -2,112 +2,13 @@ module MagicBus
2
2
 
3
3
  require 'amqp'
4
4
 
5
+ require_relative 'magicbus/subscriber'
6
+ require_relative 'magicbus/publisher'
7
+
5
8
  def self.connect(params = { host: '127.0.0.1' })
6
9
  EM.next_tick do
7
10
  Subscriber.connection = AMQP.connect(*params)
8
11
  end
9
12
  end
10
-
11
- class Publisher
12
-
13
- class << self
14
- attr_accessor :conection
15
- end
16
-
17
- def self.scatter(group, action, model, &block)
18
-
19
- MagicBus::Subscriber.connected_users(group).each do |user_id|
20
- user = User.find(user_id)
21
- self.send_to(user_id, action, model, block.call(user))
22
- end
23
-
24
- end
25
-
26
- def self.broadcast(group, action, model, data = nil)
27
-
28
- MagicBus::Subscriber.connected_users(group).each do |user_id|
29
- self.send_to(user_id, action, model, data)
30
- end
31
-
32
- end
33
-
34
- def self.send_to(user_id, action, model, data)
35
-
36
- data = { action: action, model: model, data: data }
37
-
38
- self.publish(user_id, data)
39
-
40
- end
41
-
42
- private
43
-
44
- def self.publish(user_id, data)
45
- channel = MagicBus::Subscriber.channels[user_id]
46
- exchange = channel.default_exchange
47
- exchange.publish(data.to_json, routing_key: user_id)
48
- end
49
-
50
- end
51
-
52
- class Subscriber
53
-
54
- class << self
55
- attr_accessor :connection
56
- attr_accessor :channels
57
- attr_accessor :clients
58
- end
59
-
60
- self.channels, self.clients = {}, {}
61
-
62
- def self.connected_users(group)
63
-
64
- connected_users = []
65
-
66
- group.users.each do |user|
67
- if self.clients[user.id.to_s]
68
- connected_users << user.id.to_s
69
- end
70
- end
71
-
72
- connected_users
73
-
74
- end
75
-
76
- def self.subscribe(user_id, out)
77
-
78
- unless channels[user_id]
79
-
80
- channels[user_id] = AMQP::Channel.new(connection)
81
- channel = channels[user_id]
82
-
83
- queue = channel.queue(user_id, auto_delete: true)
84
-
85
- queue.subscribe do |payload|
86
- self.clients[user_id].each do |client|
87
- client << "data: #{payload}\n\n"
88
- end
89
- end
90
-
91
- end
92
-
93
- self.clients[user_id] ||= []
94
- self.clients[user_id] << out
95
- self.clients[user_id].length
96
-
97
- end
98
-
99
- def self.unsubscribe(user_id, client_id)
100
-
101
- if self.clients[user_id][client_id]
102
- self.clients[user_id].delete_at(client_id)
103
- end
104
-
105
- if self.clients[user_id].length == 0
106
- self.clients.unsubscribe(user_id)
107
- end
108
-
109
- end
110
-
111
- end
112
13
 
113
14
  end
@@ -0,0 +1,53 @@
1
+ class MagicBus::Publisher
2
+
3
+ class << self
4
+ attr_accessor :conection
5
+ end
6
+
7
+ def self.scatter(group, action, model, &block)
8
+
9
+ warn 'Scattering message'
10
+
11
+ MagicBus::Subscriber.connected_users(group).each do |user_id|
12
+ user = User.find(user_id)
13
+ self.send_to(user_id, action, model, block.call(user))
14
+ end
15
+
16
+ end
17
+
18
+ def self.broadcast(group, action, model, data = nil)
19
+
20
+ warn 'Broadcasting message'
21
+
22
+ MagicBus::Subscriber.connected_users(group).each do |user_id|
23
+ self.send_to(user_id, action, model, data)
24
+ end
25
+
26
+ end
27
+
28
+ def self.send_to(user_id, action, model, data)
29
+
30
+ warn 'Sending data to client'
31
+
32
+ data = { action: action, model: model, data: data }
33
+
34
+ self.publish(user_id, data)
35
+
36
+ end
37
+
38
+ private
39
+
40
+ def self.publish(user_id, data)
41
+
42
+ user_id = user_id.to_s
43
+
44
+ channel = MagicBus::Subscriber.channels[user_id]
45
+
46
+ if channel
47
+ exchange = channel.default_exchange
48
+ exchange.publish(data.to_json, routing_key: user_id)
49
+ end
50
+
51
+ end
52
+
53
+ end
@@ -0,0 +1,66 @@
1
+ class MagicBus::Subscriber
2
+
3
+ class << self
4
+ attr_accessor :connection
5
+ attr_accessor :channels
6
+ attr_accessor :clients
7
+ end
8
+
9
+ self.channels, self.clients = {}, {}
10
+
11
+ def self.connected_users(group)
12
+
13
+ connected_users = []
14
+
15
+ group.users.each do |user|
16
+ if self.channels[user.id.to_s]
17
+ connected_users << user.id.to_s
18
+ end
19
+ end
20
+
21
+ connected_users
22
+
23
+ end
24
+
25
+ def self.subscribe(user_id, out)
26
+
27
+ user_id = user_id.to_s
28
+
29
+ if channels[user_id].status == :closed ||
30
+ channels[user_id].nil?
31
+
32
+ channels[user_id] = AMQP::Channel.new(connection)
33
+ channel = channels[user_id]
34
+
35
+ queue = channel.queue(user_id, auto_delete: true)
36
+
37
+ queue.subscribe do |payload|
38
+ self.clients[user_id].each do |client|
39
+ client << "data: #{payload}\n\n"
40
+ end
41
+ end
42
+
43
+ end
44
+
45
+ self.clients[user_id] ||= []
46
+ self.clients[user_id] << out
47
+ self.clients[user_id].length
48
+
49
+ end
50
+
51
+ def self.unsubscribe(user_id, client_id)
52
+
53
+ user_id = user_id.to_s
54
+
55
+ if self.clients[user_id][client_id]
56
+ self.clients[user_id].delete_at(client_id)
57
+ end
58
+
59
+ if self.clients[user_id].length == 0
60
+ self.channels[user_id].close
61
+ self.clients.unsubscribe(user_id)
62
+ end
63
+
64
+ end
65
+
66
+ end
@@ -1,5 +1,5 @@
1
1
  module MagicBus
2
2
 
3
- VERSION = '0.0.2'
3
+ VERSION = '0.0.3'
4
4
 
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magicbus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -35,6 +35,8 @@ executables: []
35
35
  extensions: []
36
36
  extra_rdoc_files: []
37
37
  files:
38
+ - lib/magicbus/publisher.rb
39
+ - lib/magicbus/subscriber.rb
38
40
  - lib/magicbus/version.rb
39
41
  - lib/magicbus.rb
40
42
  - README.md