msngr 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/msngr.rb +6 -6
- data/lib/msngr/clients.rb +2 -0
- data/lib/msngr/clients/redis.rb +0 -1
- data/lib/msngr/messenger.rb +4 -3
- data/lib/msngr/receiver.rb +4 -4
- data/lib/msngr/version.rb +1 -2
- data/spec/lib/msngr/clients/redis_spec.rb +10 -11
- data/spec/lib/msngr/messenger_spec.rb +22 -23
- data/spec/lib/msngr/receiver_spec.rb +6 -7
- data/spec/lib/msngr_spec.rb +1 -2
- data/spec/spec_helper.rb +0 -5
- metadata +107 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3dda6c846ac32026c44325fb64c9d06d74b07972944a54dd19bec6a785c346b4
|
4
|
+
data.tar.gz: fc80d107f4372349a1dfcd105b9d35c82fd66e65d70bc226cad09dab2c7bbc5c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3fa3bacdbffb0325c849ace71cf41e2698486a9114ea5ac1cb7572cf2e1581072d422a3b17907f1f8b5d97678ce9e33df12b6b95e4f1acfe9d5bb66ed7e2ab81
|
7
|
+
data.tar.gz: c68e6a3bfe52b082a686016686f81b7a5976696d2abcd658b5501450e449eabfdcc1be41bfa1c9ba1403d3b1ea60d9504cba2770393705d8753d31a5d5c1b34e
|
data/lib/msngr.rb
CHANGED
@@ -1,18 +1,18 @@
|
|
1
|
-
require "msngr/version"
|
2
|
-
require "msngr/receiver"
|
3
|
-
require "msngr/messenger"
|
4
|
-
|
5
1
|
module Msngr
|
6
|
-
|
2
|
+
require "msngr/version"
|
3
|
+
require "msngr/clients"
|
4
|
+
require "msngr/receiver"
|
5
|
+
require "msngr/messenger"
|
6
|
+
|
7
7
|
extend self
|
8
8
|
|
9
9
|
# Shorthand for writing Msngr::Messenger.new(*args).
|
10
10
|
#
|
11
11
|
# @param [Array] *args
|
12
|
+
#
|
12
13
|
# @return [Msngr::Messenger]
|
13
14
|
#
|
14
15
|
def new(*args)
|
15
16
|
Messenger.new(*args)
|
16
17
|
end
|
17
18
|
end
|
18
|
-
|
data/lib/msngr/clients/redis.rb
CHANGED
data/lib/msngr/messenger.rb
CHANGED
@@ -17,18 +17,20 @@ class Msngr::Messenger
|
|
17
17
|
# Initializes a new Messenger.
|
18
18
|
#
|
19
19
|
# @param [Msngr::Clients::*] client the to receive messages from.
|
20
|
+
#
|
20
21
|
# @return [Receiver]
|
21
22
|
#
|
22
23
|
def initialize(client)
|
23
|
-
@client
|
24
|
+
@client = client
|
24
25
|
@receivers = []
|
25
|
-
@mutex
|
26
|
+
@mutex = Mutex.new
|
26
27
|
end
|
27
28
|
|
28
29
|
# Creates and returns a new Receiver and adds it to @receivers so
|
29
30
|
# it'll receive messages matching the provided pattern.
|
30
31
|
#
|
31
32
|
# @param [Regexp] pattern
|
33
|
+
#
|
32
34
|
# @return [Receiver]
|
33
35
|
#
|
34
36
|
def subscribe(pattern)
|
@@ -100,4 +102,3 @@ class Msngr::Messenger
|
|
100
102
|
callbacks.each { |callback| callback.call(*args) }
|
101
103
|
end
|
102
104
|
end
|
103
|
-
|
data/lib/msngr/receiver.rb
CHANGED
@@ -23,13 +23,14 @@ class Msngr::Receiver
|
|
23
23
|
# Initializes a new Receiver.
|
24
24
|
#
|
25
25
|
# @param [Regexp] pattern the pattern to listen for.
|
26
|
+
#
|
26
27
|
# @return [Receiver]
|
27
28
|
#
|
28
29
|
def initialize(pattern = /.+/)
|
29
|
-
@pattern
|
30
|
-
@on_message_callbacks
|
30
|
+
@pattern = pattern
|
31
|
+
@on_message_callbacks = []
|
31
32
|
@on_unsubscribe_callbacks = []
|
32
|
-
@mutex
|
33
|
+
@mutex = Mutex.new
|
33
34
|
end
|
34
35
|
|
35
36
|
# Define a callback that invokes on each received message.
|
@@ -58,4 +59,3 @@ class Msngr::Receiver
|
|
58
59
|
@mutex.synchronize { @on_unsubscribe_callbacks << block }
|
59
60
|
end
|
60
61
|
end
|
61
|
-
|
data/lib/msngr/version.rb
CHANGED
@@ -6,37 +6,36 @@ describe Msngr::Clients::Redis do
|
|
6
6
|
let(:client) { Msngr::Clients::Redis.new }
|
7
7
|
|
8
8
|
it "should initialize without redis options" do
|
9
|
-
client.
|
9
|
+
expect(client.args.count).to eq(0)
|
10
10
|
end
|
11
11
|
|
12
12
|
it "should initialize with arguments" do
|
13
13
|
client = Msngr::Clients::Redis.new(host: "127.0.0.1", port: 6379)
|
14
|
-
client.args.
|
14
|
+
expect(client.args).to eq([{host: "127.0.0.1", port: 6379}])
|
15
15
|
end
|
16
16
|
|
17
17
|
it "should establish a connection without any args" do
|
18
|
-
Redis.
|
18
|
+
expect(Redis).to receive(:new)
|
19
19
|
client.send(:connection)
|
20
20
|
end
|
21
21
|
|
22
22
|
it "should establish a connection with args" do
|
23
23
|
options = { host: "127.0.0.1", port: 6379 }
|
24
24
|
client = Msngr::Clients::Redis.new(options)
|
25
|
-
Redis.
|
25
|
+
expect(Redis).to receive(:new).with(options)
|
26
26
|
client.send(:connection)
|
27
27
|
end
|
28
28
|
|
29
29
|
it "should yield a message" do
|
30
|
-
connection, on =
|
30
|
+
connection, on = double, double
|
31
31
|
|
32
|
-
client.
|
33
|
-
connection.
|
34
|
-
on.
|
32
|
+
allow(client).to receive(:connection).and_return(connection)
|
33
|
+
expect(connection).to receive(:psubscribe).with("*").and_yield(on)
|
34
|
+
expect(on).to receive(:pmessage).and_yield("", "room.1", "John joined the room!")
|
35
35
|
|
36
36
|
client.on_message do |event, message|
|
37
|
-
event.
|
38
|
-
message.
|
37
|
+
expect(event).to eq("room.1")
|
38
|
+
expect(message).to eq("John joined the room!")
|
39
39
|
end
|
40
40
|
end
|
41
41
|
end
|
42
|
-
|
@@ -4,53 +4,53 @@ require "msngr/messenger"
|
|
4
4
|
|
5
5
|
describe Msngr::Messenger do
|
6
6
|
|
7
|
-
let(:client)
|
7
|
+
let(:client) { double }
|
8
8
|
let(:messenger) { Msngr::Messenger.new(client) }
|
9
9
|
|
10
10
|
it "should initialize with an empty array of receivers" do
|
11
|
-
messenger.receivers.
|
11
|
+
expect(messenger.receivers).to be_empty
|
12
12
|
end
|
13
13
|
|
14
14
|
it "should create a new Receiver and add it to @receivers" do
|
15
15
|
receiver = messenger.subscribe(/.+/)
|
16
|
-
messenger.
|
17
|
-
messenger.receivers.first.
|
16
|
+
expect(messenger.receivers.count).to eq(1)
|
17
|
+
expect(messenger.receivers.first).to eq(receiver)
|
18
18
|
end
|
19
19
|
|
20
20
|
it "should unsubscribe a receiver" do
|
21
21
|
receiver = messenger.subscribe(/.+/)
|
22
|
-
messenger.
|
23
|
-
with(receiver.on_unsubscribe_callbacks, messenger)
|
22
|
+
expect(messenger).to receive(:dispatch)
|
23
|
+
.with(receiver.on_unsubscribe_callbacks, messenger)
|
24
24
|
messenger.unsubscribe(receiver)
|
25
|
-
messenger.
|
25
|
+
expect(messenger.receivers.count).to eq(0)
|
26
26
|
end
|
27
27
|
|
28
28
|
it "should start listening on a separate thread" do
|
29
|
-
Thread.
|
30
|
-
messenger.
|
31
|
-
messenger.
|
29
|
+
expect(Thread).to receive(:new).and_yield
|
30
|
+
expect(messenger).to receive(:loop).and_yield
|
31
|
+
expect(messenger).to receive(:listen).once
|
32
32
|
messenger.listen!
|
33
33
|
end
|
34
34
|
|
35
35
|
it "should display a backtrace on error and restart" do
|
36
|
-
Thread.
|
37
|
-
messenger.
|
38
|
-
messenger.
|
39
|
-
messenger.
|
40
|
-
messenger.
|
36
|
+
expect(Thread).to receive(:new).and_yield
|
37
|
+
expect(messenger).to receive(:loop).and_yield
|
38
|
+
expect(messenger).to receive(:listen).and_raise
|
39
|
+
expect(messenger).to receive(:puts).exactly(4).times
|
40
|
+
expect(messenger).to receive(:sleep)
|
41
41
|
messenger.listen!
|
42
42
|
end
|
43
43
|
|
44
44
|
it "should listen to the client but not dispatch" do
|
45
45
|
messenger.subscribe(/room\.2/)
|
46
|
-
client.
|
47
|
-
messenger.
|
46
|
+
expect(client).to receive(:on_message).and_yield("room.1", "Hi John")
|
47
|
+
expect(messenger).to receive(:dispatch).never
|
48
48
|
messenger.send(:listen)
|
49
49
|
end
|
50
50
|
|
51
51
|
it "should listen to the client and dispatch a message" do
|
52
|
-
messenger.subscribe(/room\.1/).on_message { |m| m.
|
53
|
-
client.
|
52
|
+
messenger.subscribe(/room\.1/).on_message { |m| expect(m).to eq("Hi John") }
|
53
|
+
expect(client).to receive(:on_message).and_yield("room.1", "Hi John")
|
54
54
|
messenger.send(:listen)
|
55
55
|
end
|
56
56
|
|
@@ -58,7 +58,7 @@ describe Msngr::Messenger do
|
|
58
58
|
3.times do
|
59
59
|
messenger.subscribe(/room\.1/).tap do |receiver|
|
60
60
|
cb = proc {}
|
61
|
-
cb.
|
61
|
+
expect(cb).to receive(:call).with("Hi John")
|
62
62
|
receiver.on_message(&cb)
|
63
63
|
end
|
64
64
|
end
|
@@ -66,13 +66,12 @@ describe Msngr::Messenger do
|
|
66
66
|
2.times do
|
67
67
|
messenger.subscribe(/room\.2/).tap do |receiver|
|
68
68
|
cb = proc {}
|
69
|
-
cb.
|
69
|
+
expect(cb).to receive(:call).never
|
70
70
|
receiver.on_message(&cb)
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
74
|
-
client.
|
74
|
+
expect(client).to receive(:on_message).and_yield("room.1", "Hi John")
|
75
75
|
messenger.send(:listen)
|
76
76
|
end
|
77
77
|
end
|
78
|
-
|
@@ -6,12 +6,12 @@ describe Msngr::Receiver do
|
|
6
6
|
let(:receiver) { Msngr::Receiver.new }
|
7
7
|
|
8
8
|
it "should initialize with a default wildcard pattern" do
|
9
|
-
receiver.pattern.
|
9
|
+
expect(receiver.pattern).to eq(/.+/)
|
10
10
|
end
|
11
11
|
|
12
12
|
it "should initialize with a custom pattern" do
|
13
13
|
pattern = /rooms\.1/
|
14
|
-
Msngr::Receiver.new(pattern).pattern.
|
14
|
+
expect(Msngr::Receiver.new(pattern).pattern).to eq(pattern)
|
15
15
|
end
|
16
16
|
|
17
17
|
it "should define an message callback" do
|
@@ -19,8 +19,8 @@ describe Msngr::Receiver do
|
|
19
19
|
receiver.on_message(&callback)
|
20
20
|
|
21
21
|
callbacks = receiver.on_message_callbacks
|
22
|
-
callbacks.size.
|
23
|
-
callbacks.first.call("message").
|
22
|
+
expect(callbacks.size).to eq(1)
|
23
|
+
expect(callbacks.first.call("message")).to eq("invoked: message")
|
24
24
|
end
|
25
25
|
|
26
26
|
it "should define an unsubscribe callback" do
|
@@ -28,10 +28,9 @@ describe Msngr::Receiver do
|
|
28
28
|
receiver.on_unsubscribe(&callback)
|
29
29
|
|
30
30
|
callbacks = receiver.on_unsubscribe_callbacks
|
31
|
-
callbacks.size.
|
31
|
+
expect(callbacks.size).to eq(1)
|
32
32
|
|
33
33
|
object = Object.new
|
34
|
-
callbacks.first.call(object).
|
34
|
+
expect(callbacks.first.call(object)).to eq("unsubscribed from: #{object}")
|
35
35
|
end
|
36
36
|
end
|
37
|
-
|
data/spec/lib/msngr_spec.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,58 +1,151 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: msngr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael van Rooijen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-02-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
16
30
|
requirements:
|
17
31
|
- - "~>"
|
18
32
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
33
|
+
version: 10.4.2
|
20
34
|
type: :development
|
21
35
|
prerelease: false
|
22
36
|
version_requirements: !ruby/object:Gem::Requirement
|
23
37
|
requirements:
|
24
38
|
- - "~>"
|
25
39
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
40
|
+
version: 10.4.2
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
42
|
+
name: rspec
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
30
44
|
requirements:
|
31
45
|
- - "~>"
|
32
46
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
34
|
-
|
47
|
+
version: 3.1.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.1.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
35
60
|
- !ruby/object:Gem::Version
|
36
|
-
version: 10.1
|
61
|
+
version: 0.10.1
|
37
62
|
type: :development
|
38
63
|
prerelease: false
|
39
64
|
version_requirements: !ruby/object:Gem::Requirement
|
40
65
|
requirements:
|
41
66
|
- - "~>"
|
42
67
|
- !ruby/object:Gem::Version
|
43
|
-
version: 10.1
|
44
|
-
|
68
|
+
version: 0.10.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.9.1
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.9.1
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: yard
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.9.20.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.9.20.0
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: redis
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 3.2.0
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 3.2.0
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: hiredis
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 0.5.2
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 0.5.2
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: parallel
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: 1.3.3
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
45
137
|
- !ruby/object:Gem::Version
|
46
|
-
version:
|
138
|
+
version: 1.3.3
|
47
139
|
description: A light-weight Ruby library for multi-threaded Ruby applications that
|
48
140
|
allows threads to share a single service connection for more efficient messaging.
|
49
141
|
email:
|
50
|
-
-
|
142
|
+
- michael@vanrooijen.io
|
51
143
|
executables: []
|
52
144
|
extensions: []
|
53
145
|
extra_rdoc_files: []
|
54
146
|
files:
|
55
147
|
- "./lib/msngr.rb"
|
148
|
+
- "./lib/msngr/clients.rb"
|
56
149
|
- "./lib/msngr/clients/redis.rb"
|
57
150
|
- "./lib/msngr/messenger.rb"
|
58
151
|
- "./lib/msngr/receiver.rb"
|
@@ -62,7 +155,7 @@ files:
|
|
62
155
|
- "./spec/lib/msngr/receiver_spec.rb"
|
63
156
|
- "./spec/lib/msngr_spec.rb"
|
64
157
|
- "./spec/spec_helper.rb"
|
65
|
-
homepage: https://github.com/
|
158
|
+
homepage: https://github.com/mrrooijen/msngr/
|
66
159
|
licenses:
|
67
160
|
- MIT
|
68
161
|
metadata: {}
|
@@ -81,8 +174,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
81
174
|
- !ruby/object:Gem::Version
|
82
175
|
version: '0'
|
83
176
|
requirements: []
|
84
|
-
|
85
|
-
rubygems_version: 2.2.0
|
177
|
+
rubygems_version: 3.1.2
|
86
178
|
signing_key:
|
87
179
|
specification_version: 4
|
88
180
|
summary: A light-weight Ruby library for multi-threaded Ruby applications that allows
|