rmsg 0.0.1 → 0.0.2
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 +4 -4
- data/lib/rmsg/topic.rb +21 -0
- data/lib/rmsg/version.rb +1 -1
- data/rmsg.gemspec +3 -3
- data/test/bin/topic_consumer.rb +10 -0
- data/test/bin/topic_publisher.rb +16 -0
- data/test/spec/rabbit_spec.rb +7 -2
- data/test/spec/topic_spec.rb +43 -0
- metadata +12 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0745a77f2a010b9c427abda0c18fd0d8f943e32f
|
4
|
+
data.tar.gz: 6a193f367f4a617f3ab99a38ef74d579e23bd0c3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3065150f870b06d1edc886ef2b60db1800ad021b4cd9ce63742219ca41b90d3eb2f38101fdb88d7e34247a94b527df094cc0c279b6f27a03f206146c75f69c8d
|
7
|
+
data.tar.gz: 2e6aef12fc29e27ecb8646d6d6afa1936a0bb03d353a56b20a95237191aff786ddf7cecc5c93ccea328d686b3202fcafb9f522d52a092d2de669a239733d8f91
|
data/lib/rmsg/topic.rb
CHANGED
@@ -1,4 +1,25 @@
|
|
1
1
|
module Rmsg
|
2
2
|
class Topic
|
3
|
+
def initialize(params)
|
4
|
+
@rabbit = params[:rabbit]
|
5
|
+
@exchange = @rabbit.channel.topic(params[:topic])
|
6
|
+
end
|
7
|
+
|
8
|
+
def publish(message, key)
|
9
|
+
@exchange.publish(message.to_json, :routing_key => key)
|
10
|
+
end
|
11
|
+
|
12
|
+
def subscribe(key)
|
13
|
+
@queue = @rabbit.channel.queue("", :exclusive => true)
|
14
|
+
@queue.bind(@exchange, :routing_key => key)
|
15
|
+
begin
|
16
|
+
@queue.subscribe(:block => true) do |delivery_info, metadata, payload|
|
17
|
+
message = JSON.parse(payload, symbolize_names: true)
|
18
|
+
yield message
|
19
|
+
end
|
20
|
+
rescue Interrupt => _
|
21
|
+
@rabbit.close
|
22
|
+
end
|
23
|
+
end
|
3
24
|
end
|
4
25
|
end
|
data/lib/rmsg/version.rb
CHANGED
data/rmsg.gemspec
CHANGED
@@ -8,9 +8,9 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = Rmsg::VERSION
|
9
9
|
spec.authors = ["badshark"]
|
10
10
|
spec.email = ["info@badshark.io"]
|
11
|
-
spec.summary = %q{
|
12
|
-
spec.description = %q{
|
13
|
-
spec.homepage = "https://github.com/
|
11
|
+
spec.summary = %q{RabbitMQ messaging.}
|
12
|
+
spec.description = %q{RabbitMQ messaging with topics and tasks. A thin, minimal layer on top of Bunny.}
|
13
|
+
spec.homepage = "https://github.com/badshark/rmsg"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0")
|
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rmsg'
|
4
|
+
|
5
|
+
message = {
|
6
|
+
id: 1,
|
7
|
+
key: 'xxxccc'
|
8
|
+
}
|
9
|
+
key = 'users.key_changed'
|
10
|
+
|
11
|
+
rabbit = Rmsg::Rabbit.new
|
12
|
+
services = Rmsg::Topic.new(rabbit: rabbit, topic: 'services')
|
13
|
+
|
14
|
+
services.publish(message, key)
|
15
|
+
|
16
|
+
rabbit.close
|
data/test/spec/rabbit_spec.rb
CHANGED
@@ -18,8 +18,13 @@ describe 'Rabbit' do
|
|
18
18
|
end
|
19
19
|
|
20
20
|
it 'can close its channel and connection' do
|
21
|
+
rabbit = Rmsg::Rabbit.new
|
22
|
+
rabbit.close
|
23
|
+
rabbit.channel.closed?.must_equal true
|
24
|
+
rabbit.connection.closed?.must_equal true
|
25
|
+
end
|
26
|
+
|
27
|
+
after do
|
21
28
|
@rabbit.close
|
22
|
-
@rabbit.channel.closed?.must_equal true
|
23
|
-
@rabbit.connection.closed?.must_equal true
|
24
29
|
end
|
25
30
|
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require_relative './_init'
|
2
|
+
require 'open3'
|
3
|
+
|
4
|
+
describe 'Topic' do
|
5
|
+
before do
|
6
|
+
@rabbit = Rmsg::Rabbit.new
|
7
|
+
@topic = Rmsg::Topic.new(
|
8
|
+
rabbit: @rabbit,
|
9
|
+
topic: 'messages'
|
10
|
+
)
|
11
|
+
@message = {
|
12
|
+
id: 1,
|
13
|
+
key: 'xxxccc'
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'is a topics handler over RabbitMQ' do
|
18
|
+
@topic.must_be_instance_of Rmsg::Topic
|
19
|
+
end
|
20
|
+
|
21
|
+
# Simulate two processes communicating using a topic and a key
|
22
|
+
it 'can publish and read back a message' do
|
23
|
+
read_message = ''
|
24
|
+
consumer = 'bundle exec ruby test/bin/topic_consumer.rb'
|
25
|
+
publisher = 'bundle exec ruby test/bin/topic_publisher.rb'
|
26
|
+
|
27
|
+
Open3.popen3(consumer) do |stdin, stdout, stderr, wait_thr|
|
28
|
+
# Wait for the consumer to be up
|
29
|
+
sleep 2
|
30
|
+
spawn(publisher)
|
31
|
+
read_message = stdout.gets
|
32
|
+
# Kill long-running consumer
|
33
|
+
Process.kill('INT', wait_thr.pid)
|
34
|
+
end
|
35
|
+
|
36
|
+
output = eval read_message.chomp
|
37
|
+
output.must_equal @message
|
38
|
+
end
|
39
|
+
|
40
|
+
after do
|
41
|
+
@rabbit.close
|
42
|
+
end
|
43
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rmsg
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- badshark
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,7 +66,8 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: 1.8.1
|
69
|
-
description:
|
69
|
+
description: RabbitMQ messaging with topics and tasks. A thin, minimal layer on top
|
70
|
+
of Bunny.
|
70
71
|
email:
|
71
72
|
- info@badshark.io
|
72
73
|
executables: []
|
@@ -83,9 +84,12 @@ files:
|
|
83
84
|
- lib/rmsg/topic.rb
|
84
85
|
- lib/rmsg/version.rb
|
85
86
|
- rmsg.gemspec
|
87
|
+
- test/bin/topic_consumer.rb
|
88
|
+
- test/bin/topic_publisher.rb
|
86
89
|
- test/spec/_init.rb
|
87
90
|
- test/spec/rabbit_spec.rb
|
88
|
-
|
91
|
+
- test/spec/topic_spec.rb
|
92
|
+
homepage: https://github.com/badshark/rmsg
|
89
93
|
licenses:
|
90
94
|
- MIT
|
91
95
|
metadata: {}
|
@@ -108,7 +112,10 @@ rubyforge_project:
|
|
108
112
|
rubygems_version: 2.2.2
|
109
113
|
signing_key:
|
110
114
|
specification_version: 4
|
111
|
-
summary:
|
115
|
+
summary: RabbitMQ messaging.
|
112
116
|
test_files:
|
117
|
+
- test/bin/topic_consumer.rb
|
118
|
+
- test/bin/topic_publisher.rb
|
113
119
|
- test/spec/_init.rb
|
114
120
|
- test/spec/rabbit_spec.rb
|
121
|
+
- test/spec/topic_spec.rb
|