rabid_mq 0.1.1

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: 4f29fae6619404a7e32b70f54503daa684cb4a99
4
+ data.tar.gz: f899f98e7a15370216b9819969d688840c900f23
5
+ SHA512:
6
+ metadata.gz: 95035fde4312aee4356b96e9e002002bc073f6c021a240ddfff03aacae216f59131a995aa4db2edf5d8fd0deac81e9f162b862bfdad1f93e37ee0e8537c80f9f
7
+ data.tar.gz: ab9ab838228b06881d7558ae83d6f3a5a9ca3094946ec4e87b7c16d3ca0268d9f2826df9b90faa8b4054f1887abc8dd374d108557a8a223602bb558624a2867a
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rbenv-gemsets ADDED
@@ -0,0 +1 @@
1
+ rabid_mq
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.4.1
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rabid_mq.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Black Swan Data
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,126 @@
1
+ # RabidMQ
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rabid_mq`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rabid_mq'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ ```
18
+ $ bundle
19
+ ```
20
+
21
+ Or install it yourself as:
22
+
23
+ ```
24
+ $ gem install rabid_mq
25
+ ```
26
+
27
+ ## Configuration
28
+
29
+ To configure simply add a yaml file `config/rabid_mq.yml` to your project and adjust the settings to match your requirements. These options are used verbatim to initialize `bunny` so any option supported by bunny is also supported in the rabid_mq config.
30
+
31
+ ```yaml
32
+ # config/rabid_mq.yml
33
+
34
+ default: &default
35
+ host: localhost
36
+ port: 5672
37
+ ssl: false
38
+ vhost: "/"
39
+ user: guest
40
+ pass: guest
41
+ heartbeat: server
42
+ frame_max: 131072
43
+ auth_mechanism: PLAIN
44
+
45
+ development:
46
+ <<: *default
47
+
48
+ test:
49
+ <<: *default
50
+
51
+ production:
52
+ <<: *default
53
+ ```
54
+
55
+ ## Usage
56
+
57
+ ### AMQP Producer
58
+
59
+ To create a RabbitMQ "Producer" you can include the RabidMQ::Publisher concern in your producer class
60
+
61
+ `app/models/my_class.rb`
62
+
63
+ ```ruby
64
+ class MyClass
65
+ include RabidMQ::Publisher
66
+
67
+ # code ...
68
+ end
69
+ ```
70
+
71
+ `app/controllers/some_controller.rb`
72
+
73
+ ```ruby
74
+ class SomeController < ApplicationController
75
+ def create
76
+ # code ...
77
+ MyClass.broadcast('some.amqp.topic', "Hello RabbitMQ")
78
+
79
+ # OR
80
+
81
+ # if you already have a `broadcast` method you can use the method `amqp_broadcast`
82
+ MyClass.amqp_broadcast('some.amqp.topic', "Hello RabbitMQ")
83
+
84
+ # OR
85
+
86
+ # IF you have an instance of MyClass
87
+ my_class_instance.broadcast('some.amqp.topic', "Hellow RabbitMQ from an instance")
88
+
89
+ # code ...
90
+ end
91
+ end
92
+ ```
93
+
94
+ ### Consumer
95
+
96
+ `app/models/my_class`
97
+
98
+ ```ruby
99
+ class MyClass
100
+ include RabidMQ::Listener
101
+
102
+ amqp 'some_queue_name', 'some.exchange.name', exclusive: false, routing_key: 'some.*.key.*.definition'
103
+
104
+ # OR (for finer grained control with more options)
105
+ # queue_name 'name', **options
106
+ # exchange 'name', **options
107
+
108
+ subscribe do |info, meta, data|
109
+ # Do your logic with the data here
110
+ end
111
+
112
+ # Or (for finer control on the exchange binding) you can pass in an explicitly created
113
+ # Bunny::Exchange object with options
114
+ # bind(exchange, **options).subscribe do |info, meta, data|
115
+ # # code ...
116
+ # end
117
+ end
118
+ ```
119
+
120
+ ## Contributing
121
+
122
+ Bug reports and pull requests are welcome on GitHub at <https://github.com/[USERNAME]/rabid_mq>. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
123
+
124
+ ## License
125
+
126
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rabid_mq"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,35 @@
1
+ module RabidMQ
2
+ module Config
3
+ class << self
4
+ def load_config
5
+ @config ||= YAML.load(File.read(file_name))
6
+ rescue Errno::ENOENT, NameError => e
7
+ puts "[WARN] #{e.message} in #{__FILE__}. Falling back to default config"
8
+ default_config
9
+ end
10
+
11
+ def default_config
12
+ {
13
+ :host => "localhost",
14
+ :port => 5672,
15
+ :ssl => false,
16
+ :vhost => "/",
17
+ :user => "guest",
18
+ :pass => "guest",
19
+ :heartbeat => :server, # will use RabbitMQ setting
20
+ :frame_max => 131072,
21
+ :auth_mechanism => "PLAIN"
22
+ }
23
+ end
24
+
25
+ private
26
+ def file_name
27
+ if defined? ::Rails
28
+ ::Rails.root.join('config/rabid_mq.yml')
29
+ else
30
+ 'config/rabid_mq.yml'
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,61 @@
1
+ # Provides including class with ability to subscribe and react to events that
2
+ # arrive via RabidMQ
3
+ # Example:
4
+ # class MyClass
5
+ # include RabidMQ::Listener
6
+ #
7
+ # amqp 'queue_name', 'exchange.name', exclusive: false, routing_key: 'some.routing.*.key'
8
+ #
9
+ # subscribe do |info, meta, data|
10
+ # # code to do on message receipt
11
+ # end
12
+ # end
13
+ #
14
+
15
+ module RabidMQ
16
+ module Listener
17
+ extend ::ActiveSupport::Concern
18
+
19
+ included do
20
+ class << self
21
+ attr_reader :amqp_queue, :amqp_exchange
22
+
23
+ def amqp(queue, exchange, exclusive: false, routing_key: '#')
24
+ self.queue_name queue, exclusive: exclusive
25
+ self.exchange(exchange, routing_key: routing_key)
26
+ end
27
+
28
+ # Use this as a macro in including classes like
29
+ # class MyClass
30
+ # include RabidMQ::Listener
31
+ # queue_name 'some.queue_name', exclusive: true
32
+ # end
33
+ #
34
+ def queue_name(name, **options)
35
+ @amqp_queue = RabidMQ.channel.queue(name, **options)
36
+ end
37
+
38
+ # Use this as a macro in including classes like
39
+ # class MyClass
40
+ # include RabidMQ::Listener
41
+ # exchange 'exchange.name'
42
+ # end
43
+ #
44
+ def exchange(topic, **options)
45
+ @amqp_exchange = RabidMQ.topic_exchange topic, options
46
+ end
47
+
48
+ def bind(exchange=@amqp_exchange, routing_key: '#', **options)
49
+ amqp_queue.bind(exchange, routing_key: routing_key, **options)
50
+ end
51
+
52
+ delegate :subscribe, to: :bind
53
+
54
+ def amqp_connection
55
+ amqp_exchange.channel.connection
56
+ end
57
+
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,42 @@
1
+ # Provides any including class with the ability to publish via RabidMQ
2
+ # Example:
3
+ # class MyClass
4
+ # include RabidMQ::Publisher
5
+ # end
6
+ #
7
+ # MyClass.amqp_broadcast('some_topic', "Class Hello")
8
+ # instance = MyClass.new
9
+ # instance.amqp_broadcast('some_topic', "Instance Hello")
10
+ #
11
+
12
+ module RabidMQ
13
+ module Publisher
14
+ extend ActiveSupport::Concern
15
+
16
+ included do
17
+ def amqp_broadcast(*args, **options)
18
+ self.class.amqp_broadcast *args, **options
19
+ end
20
+
21
+ alias_method :broadcast, :amqp_broadcast
22
+
23
+ class << self
24
+ def amqp_broadcast(topic, payload, routing_key: self.default_amqp_routing_key)
25
+ exchange = topic_exchange(topic)
26
+ exchange.publish(payload, routing_key: routing_key)
27
+ rescue => e
28
+ Rails.logger.error e.message
29
+ end
30
+
31
+ alias_method :broadcast, :amqp_broadcast
32
+
33
+ delegate :topic_exchange, to: RabidMQ
34
+
35
+ def default_amqp_routing_key
36
+ self.name.underscore.gsub(/\//, '.')
37
+ end
38
+
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,7 @@
1
+ require 'rails'
2
+
3
+ module RabidMQ
4
+ class Railtie < Rails::Railtie
5
+
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module RabidMQ
2
+ VERSION = "0.1.1"
3
+ end
data/lib/rabid_mq.rb ADDED
@@ -0,0 +1,48 @@
1
+ require 'bunny'
2
+ require 'yaml'
3
+ require 'active_support/concern'
4
+ require 'active_support/core_ext/module/delegation'
5
+ require 'rabid_mq/version'
6
+ require 'rabid_mq/railtie' if defined?(::Rails::Railtie)
7
+ require 'rabid_mq/config'
8
+ require 'rabid_mq/listener'
9
+ require 'rabid_mq/publisher'
10
+ # Module to abstract the boilerplate of connecting to rabbitMQ
11
+ # This will also abstract how the credentials are supplied etc
12
+ module RabidMQ
13
+ class << self
14
+
15
+ # Provide a topic exchange on demand connected to the existing channel
16
+ def topic_exchange(topic, **options)
17
+ channel.topic(topic, **options)
18
+ end
19
+
20
+ # Provide fanout exchange
21
+ def fanout_exchange(topic, **options)
22
+ channel.fanout(topic, **options)
23
+ end
24
+
25
+ # Get a channel with the Bunny::Session
26
+ def channel
27
+ @channel ||= connect.create_channel
28
+ rescue Bunny::ChannelAlreadyClosed => e
29
+ @channel = nil
30
+ channel
31
+ end
32
+
33
+ # Start a new connection
34
+ def connect
35
+ connection.tap do |c|
36
+ c.start
37
+ at_exit do
38
+ c.close
39
+ end
40
+ end
41
+ end
42
+
43
+ # Provide a new or existing Bunny::Session
44
+ def connection
45
+ @connection ||= Bunny.new RabidMQ::Config.load_config
46
+ end
47
+ end
48
+ end
data/rabid_mq.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rabid_mq/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rabid_mq"
8
+ spec.version = RabidMQ::VERSION
9
+ spec.authors = ["Tyrone Wilson"]
10
+ spec.email = ["tyrone.wilson@blackswan.com"]
11
+
12
+ spec.summary = %q{Provides easy way to configure and use RabbitMQ in Ruby on Rails}
13
+ spec.description = %q{If you want to just get up and going in making your rails app into a producer or consumer of RabbitMQ events then use this gem}
14
+ spec.homepage = "https://github.com/container-streams/rabid_mq"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_dependency 'bunny', '~> 2.7.1'
34
+ spec.add_dependency 'activesupport'
35
+
36
+ spec.add_development_dependency "bundler", "~> 1.14"
37
+ spec.add_development_dependency "rake", "~> 11.0"
38
+ spec.add_development_dependency "rspec", "~> 3.0"
39
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rabid_mq
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Tyrone Wilson
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-09-28 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.1
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.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '11.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '11.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: If you want to just get up and going in making your rails app into a
84
+ producer or consumer of RabbitMQ events then use this gem
85
+ email:
86
+ - tyrone.wilson@blackswan.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rbenv-gemsets"
93
+ - ".rspec"
94
+ - ".ruby-version"
95
+ - ".travis.yml"
96
+ - Gemfile
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - bin/console
101
+ - bin/setup
102
+ - lib/rabid_mq.rb
103
+ - lib/rabid_mq/config.rb
104
+ - lib/rabid_mq/listener.rb
105
+ - lib/rabid_mq/publisher.rb
106
+ - lib/rabid_mq/railtie.rb
107
+ - lib/rabid_mq/version.rb
108
+ - rabid_mq.gemspec
109
+ homepage: https://github.com/container-streams/rabid_mq
110
+ licenses:
111
+ - MIT
112
+ metadata:
113
+ allowed_push_host: https://rubygems.org
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.6.11
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Provides easy way to configure and use RabbitMQ in Ruby on Rails
134
+ test_files: []