simple_rabbit 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3d8ae9d436ec6c8acfd9b036e6155d00d786ea45
4
+ data.tar.gz: 447989d670fa24cc63c428ee7d4d8d80ae227e2b
5
+ SHA512:
6
+ metadata.gz: 5110ae25e0452bfcbb3c13cfc4f51f45b2ac4040d4e2a5224f0e5d75a7f81eaca7f9850f449e2b53e81d0000064c1e5c2b16071d282a7b5552e1c3d92ae45cfd
7
+ data.tar.gz: f4b37ed352e53dfa3bbbeae096fc3fee9ddb07c065953afe669cf6142900e4cf2231e810c052c5123e1b59e309e73821c5dcdd7c9946016f2f7a3aefaf9eed02
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'bunny'
data/Gemfile.lock ADDED
@@ -0,0 +1,15 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ amq-protocol (2.2.0)
5
+ bunny (2.7.2)
6
+ amq-protocol (>= 2.2.0)
7
+
8
+ PLATFORMS
9
+ ruby
10
+
11
+ DEPENDENCIES
12
+ bunny
13
+
14
+ BUNDLED WITH
15
+ 1.15.3
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Jared McFarland
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # SimpleRabbit
2
+
3
+ Simple utility to send / consume messages via RabbitMQ, based on [ruby-amqp/bunny](https://github.com/ruby-amqp/bunny) ruby gem.
4
+
5
+ ## Installation
6
+
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'simple_rabbit'
11
+
12
+ And then execute:
13
+
14
+ $ bundle install
15
+
16
+ Or install it manually:
17
+
18
+ $ gem install simple_rabbit
19
+
20
+ ## Usage
21
+
22
+ ### Establishing connection:
23
+
24
+ To establish connection run this once. It can go to any kind of initializer script.
25
+
26
+ ```ruby
27
+ SimpleRabbit::Connection.get(user: "rabbitmquser", pass: "rabbitpass", host: "localhost:5672")
28
+ ```
29
+
30
+ It accepts all configuration params available in *Bunny* gem.
31
+
32
+ ### Publishing message
33
+
34
+ Once connection has been established you can run following to publish message to queue called 'sample_queue'.
35
+
36
+
37
+ ```ruby
38
+ SimpleRabbit::Publisher.publish("sample_queue", "This is the message body")
39
+ ```
40
+
41
+ It creates durable queue, that gets initialized on demand.
42
+
43
+ ### Creating consumer worker
44
+
45
+ To create consumer worker that reacts to messages published in 'sample_queue' define worker as below:
46
+
47
+ ```ruby
48
+ SimpleRabbit::ConsumerWorker.run("sample_queue") do |message|
49
+ puts "Hey I've received message with body: '#{message}'"
50
+ # do sth
51
+ end
52
+ ```
53
+
54
+ All workers are set to use manual acknowledgement, so in other words if worker fails with some sort of exception, the message can be handled by different worker.
@@ -0,0 +1,17 @@
1
+ module SimpleRabbit
2
+
3
+ class Connection
4
+
5
+ def self.get(opts = {})
6
+ $simplerabbit_connection ||=
7
+ begin
8
+ conn = Bunny.new(opts)
9
+ conn.start
10
+ conn
11
+ end
12
+ $simplerabbit_channel ||= $simplerabbit_connection.create_channel
13
+ end
14
+
15
+ end
16
+
17
+ end
@@ -0,0 +1,24 @@
1
+ module SimpleRabbit
2
+
3
+ class ConsumerWorker
4
+
5
+ def self.run(queue_name, &block)
6
+ raise ConnectionNotEstablished unless $simplerabbit_connection
7
+ queue = $simplerabbit_channel.queue(queue_name, durable: true)
8
+ begin
9
+ puts " [*] Waiting for messages. To exit press CTRL+C"
10
+ queue.subscribe(block: true, manual_ack: true) do |delivery_info, properties, body|
11
+ begin
12
+ block.call(body)
13
+ $simplerabbit_channel.ack(delivery_info.delivery_tag)
14
+ rescue
15
+ end
16
+ end
17
+ rescue Interrupt => _
18
+ exit(0)
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,13 @@
1
+ module SimpleRabbit
2
+
3
+ class Publisher
4
+
5
+ def self.publish(queue_name, message)
6
+ raise ConnectionNotEstablished unless $simplerabbit_connection
7
+ queue = $simplerabbit_channel.queue(queue_name, durable: true)
8
+ queue.publish(message, persistent: true)
9
+ end
10
+
11
+ end
12
+
13
+ end
@@ -0,0 +1,15 @@
1
+ require "bunny"
2
+
3
+ BASE_PATH = File.dirname(__FILE__) + "/simple_rabbit/"
4
+
5
+ [
6
+ "connection",
7
+ "consumer_worker",
8
+ "publisher"
9
+ ].each do |utility|
10
+ require BASE_PATH + utility
11
+ end
12
+
13
+ module SimpleRabbit
14
+ class ConnectionNotEstablished < StandardError; end
15
+ end
@@ -0,0 +1,17 @@
1
+ Gem::Specification.new do |s|
2
+ s.authors = ["Dominik Sito"]
3
+ s.email = "dominik.sito@gmail.com"
4
+ s.homepage = "https://github.com/railis/simple_rabbit"
5
+
6
+ s.name = "simple_rabbit"
7
+ s.version = "1.0.0"
8
+ s.date = '2017-11-10'
9
+ s.summary = "Simple Rabbit"
10
+ s.description = "Ruby gem allowing simple RabbitMQ message sending and consuming"
11
+
12
+ s.files = `git ls-files`.split($\)
13
+
14
+ s.license = "MIT"
15
+
16
+ s.add_dependency "bunny", "~> 2.7"
17
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_rabbit
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Dominik Sito
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-11-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bunny
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.7'
27
+ description: Ruby gem allowing simple RabbitMQ message sending and consuming
28
+ email: dominik.sito@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - Gemfile
34
+ - Gemfile.lock
35
+ - LICENSE
36
+ - README.md
37
+ - lib/simple_rabbit.rb
38
+ - lib/simple_rabbit/connection.rb
39
+ - lib/simple_rabbit/consumer_worker.rb
40
+ - lib/simple_rabbit/publisher.rb
41
+ - simple_rabbit.gemspec
42
+ homepage: https://github.com/railis/simple_rabbit
43
+ licenses:
44
+ - MIT
45
+ metadata: {}
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 2.6.11
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: Simple Rabbit
66
+ test_files: []