rmsg 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: 246f136b4411c9a1526f5a1f9b9ca668151ab661
4
- data.tar.gz: 194422253b18baf54bb72789426ab3036ff6b961
3
+ metadata.gz: 0745a77f2a010b9c427abda0c18fd0d8f943e32f
4
+ data.tar.gz: 6a193f367f4a617f3ab99a38ef74d579e23bd0c3
5
5
  SHA512:
6
- metadata.gz: 8b7afe05a347007d1788ae66b61f46351a29b5840c7d524089169ed153f6b7cfc859c98ad8ab47330c16e7a4092dc5e964155ab666cd7d5a376ce5bc20884e20
7
- data.tar.gz: 0439fa8d2f64318616dbe5bf62a324740adaf4cef1f7e1afed85b3be2e2f150b4c1446ee180a860d07c3cb3a2754e388a374cad30d29887a67cdcae546b64767
6
+ metadata.gz: 3065150f870b06d1edc886ef2b60db1800ad021b4cd9ce63742219ca41b90d3eb2f38101fdb88d7e34247a94b527df094cc0c279b6f27a03f206146c75f69c8d
7
+ data.tar.gz: 2e6aef12fc29e27ecb8646d6d6afa1936a0bb03d353a56b20a95237191aff786ddf7cecc5c93ccea328d686b3202fcafb9f522d52a092d2de669a239733d8f91
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Rmsg
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -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{Topics and Tasks over RabbitMQ.}
12
- spec.description = %q{Topics and Tasks over RabbitMQ. A thin, minimal layer on top of Bunny.}
13
- spec.homepage = "https://github.com/_badshark/rmsg"
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,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rmsg'
4
+
5
+ rabbit = Rmsg::Rabbit.new
6
+ services = Rmsg::Topic.new(rabbit: rabbit, topic: 'services')
7
+
8
+ services.subscribe('users.key_changed') do |message|
9
+ p message
10
+ end
@@ -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
@@ -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.1
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-10-31 00:00:00.000000000 Z
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: Topics and Tasks over RabbitMQ. A thin, minimal layer on top of Bunny.
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
- homepage: https://github.com/_badshark/rmsg
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: Topics and Tasks over RabbitMQ.
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