kanina 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +36 -0
- data/.inch.yml +5 -0
- data/.rspec +2 -0
- data/.rubocop.yml +2 -0
- data/.ruby-version +1 -0
- data/.travis.yml +16 -0
- data/.yardopts +1 -0
- data/Gemfile +12 -0
- data/Guardfile +9 -0
- data/History.md +23 -0
- data/ISC-LICENSE +5 -0
- data/README.md +68 -0
- data/Rakefile +16 -0
- data/kanina.gemspec +43 -0
- data/lib/generators/kanina/install/USAGE +8 -0
- data/lib/generators/kanina/install/install_generator.rb +16 -0
- data/lib/generators/kanina/install/templates/amqp.yml.sample +18 -0
- data/lib/generators/kanina/message/USAGE +8 -0
- data/lib/generators/kanina/message/message_generator.rb +20 -0
- data/lib/generators/kanina/message/templates/message.rb +6 -0
- data/lib/generators/kanina/subscription/USAGE +8 -0
- data/lib/generators/kanina/subscription/subscription_generator.rb +20 -0
- data/lib/generators/kanina/subscription/templates/subscription.rb +7 -0
- data/lib/kanina.rb +11 -0
- data/lib/kanina/logger.rb +18 -0
- data/lib/kanina/message.rb +138 -0
- data/lib/kanina/railtie.rb +29 -0
- data/lib/kanina/server.rb +74 -0
- data/lib/kanina/subscription.rb +80 -0
- data/lib/kanina/version.rb +3 -0
- data/spec/dummy/README.rdoc +28 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/images/.keep +0 -0
- data/spec/dummy/app/assets/javascripts/application.js +13 -0
- data/spec/dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/dummy/app/controllers/application_controller.rb +5 -0
- data/spec/dummy/app/controllers/concerns/.keep +0 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/mailers/.keep +0 -0
- data/spec/dummy/app/models/.keep +0 -0
- data/spec/dummy/app/models/concerns/.keep +0 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/amqp.yml +11 -0
- data/spec/dummy/config/application.rb +28 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +29 -0
- data/spec/dummy/config/environments/production.rb +80 -0
- data/spec/dummy/config/environments/test.rb +36 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +12 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +56 -0
- data/spec/dummy/lib/assets/.keep +0 -0
- data/spec/dummy/log/.keep +0 -0
- data/spec/dummy/public/404.html +58 -0
- data/spec/dummy/public/422.html +58 -0
- data/spec/dummy/public/500.html +57 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/kanina/logger_spec.rb +24 -0
- data/spec/kanina/message_spec.rb +201 -0
- data/spec/kanina/server_spec.rb +39 -0
- data/spec/kanina/subscription_spec.rb +55 -0
- data/spec/spec_helper.rb +51 -0
- metadata +403 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ec8d730195a2d4c6669e0a7a3d6f221622e1d4fe
|
4
|
+
data.tar.gz: 83205977db72d11380b3d41ea71664bd56530c2d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4f8366244d463044be02f10a692ae6a32bb9d6477243e0b726ee1bed2cd883e71cc900ee47d031110c5605884614e31fd60ba5706f5c8f58c135e1b156614d55
|
7
|
+
data.tar.gz: a876042754e17c9b2390a575ce9cdcedd67ec2b1990a98df7af7b57fbf6bdf98587cf3b256b420a20be3eae403d1e174efd5b68f9a9a311fe7e620edf0fcc081
|
data/.gitignore
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# App generated
|
2
|
+
.bundle/
|
3
|
+
log/*.log
|
4
|
+
pkg
|
5
|
+
tmp
|
6
|
+
|
7
|
+
# Rails specific
|
8
|
+
spec/dummy/db/*.sqlite3
|
9
|
+
spec/dummy/db/*.sqlite3-journal
|
10
|
+
spec/dummy/log/*.log
|
11
|
+
spec/dummy/tmp/
|
12
|
+
spec/dummy/.sass-cache
|
13
|
+
|
14
|
+
# Test generated
|
15
|
+
coverage/
|
16
|
+
rails_best_practices_output.html
|
17
|
+
spec/reports/
|
18
|
+
|
19
|
+
# OS stuff
|
20
|
+
.DS_Store
|
21
|
+
.env
|
22
|
+
|
23
|
+
# IDE generated stuff
|
24
|
+
.gemtags
|
25
|
+
.tags
|
26
|
+
.tags_sorted_by_file
|
27
|
+
.idea/*
|
28
|
+
atlassian-ide-plugin.xml
|
29
|
+
.project
|
30
|
+
|
31
|
+
# Ignore, this being a gem.
|
32
|
+
Gemfile.lock
|
33
|
+
|
34
|
+
# Docs
|
35
|
+
.yardoc
|
36
|
+
doc/
|
data/.inch.yml
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.1.0
|
data/.travis.yml
ADDED
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--markup=markdown
|
data/Gemfile
ADDED
data/Guardfile
ADDED
data/History.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Changelog
|
2
|
+
=========
|
3
|
+
|
4
|
+
v0.6.0
|
5
|
+
------
|
6
|
+
- Mark individual messages as persistent or transient.
|
7
|
+
- Add better documentation throughout.
|
8
|
+
|
9
|
+
v0.5.0
|
10
|
+
------
|
11
|
+
- Add durable queues.
|
12
|
+
|
13
|
+
v0.4.0
|
14
|
+
------
|
15
|
+
- Update Bunny version to ~1.6.
|
16
|
+
- Should take care of some connection issues people have been seeing from the older version of Bunny.
|
17
|
+
- I'm not setting the patch version, so your computer will automatically install the latest minor version.
|
18
|
+
|
19
|
+
v0.3.3
|
20
|
+
------
|
21
|
+
- Started this changelog.
|
22
|
+
- Remove Spring dependency for preloading.
|
23
|
+
- Fix the way existing exchanges are found and connected to.
|
data/ISC-LICENSE
ADDED
@@ -0,0 +1,5 @@
|
|
1
|
+
Copyright (c) 2014 Clinton Judy <clinton@j-udy.com>
|
2
|
+
|
3
|
+
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
|
4
|
+
|
5
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
Kanina
|
2
|
+
====
|
3
|
+
|
4
|
+
A Rails plugin that makes it easier for your models to communicate with RabbitMQ.
|
5
|
+
|
6
|
+
[![Build Status](https://travis-ci.org/judy/kanina.svg?branch=master)](https://travis-ci.org/judy/kanina)
|
7
|
+
[![Code Climate](https://codeclimate.com/github/judy/kanina.png)](https://codeclimate.com/github/judy/kanina)
|
8
|
+
[![Coverage Status](https://coveralls.io/repos/judy/kanina/badge.png)](https://coveralls.io/r/judy/kanina)
|
9
|
+
[![Inline docs](http://inch-ci.org/github/judy/kanina.png?branch=master)](http://inch-ci.org/github/judy/kanina)
|
10
|
+
|
11
|
+
Kanina abstracts away queue and exchange creation, so you can focus on the message and subscription side of things in Rails.
|
12
|
+
|
13
|
+
Prerequisites
|
14
|
+
------------
|
15
|
+
|
16
|
+
You'll need **RabbitMQ** installed. Find instructions for your platform [here](http://www.rabbitmq.com/download.html).
|
17
|
+
|
18
|
+
|
19
|
+
Installation
|
20
|
+
------------
|
21
|
+
|
22
|
+
Put this in your `Gemfile`:
|
23
|
+
|
24
|
+
gem 'kanina'
|
25
|
+
|
26
|
+
Then run `bundle install` to install the gem and its dependencies. Finally, run `rails generate kanina:install` to create bin/kanina and an amqp.yml.sample file in your config folder.
|
27
|
+
|
28
|
+
Copy amqp.yml.sample to amqp.yml, and change it to connect to your local RabbitMQ server. By default, it will connect to the one on your development machine, as long as you haven't changed the settings on it.
|
29
|
+
|
30
|
+
Sending Messages
|
31
|
+
----------------
|
32
|
+
|
33
|
+
Run the generator:
|
34
|
+
|
35
|
+
rails generate kanina:message MessageName
|
36
|
+
|
37
|
+
Then specify the name of the exchange, OR the routing key for the intended queue. To send a message, generate an instance of your new class, add data, and hit send:
|
38
|
+
|
39
|
+
msg = MessageName.new
|
40
|
+
msg.data = {key: "value"}
|
41
|
+
msg.deliver
|
42
|
+
|
43
|
+
You can also specify the type of exchange you want to create, like so:
|
44
|
+
|
45
|
+
class WeatherMessage < Kanina::Message
|
46
|
+
fanout "reports"
|
47
|
+
end
|
48
|
+
|
49
|
+
Remember to use [RabbitMQ's documentation](http://www.rabbitmq.com/documentation.html) to understand the rules governing how to use different types of exchanges, bindings, and queues appropriately. You don't have to create those things ahead of time with Kanina, but you do have to understand how they work!
|
50
|
+
|
51
|
+
Receiving messages
|
52
|
+
------------------
|
53
|
+
|
54
|
+
Generate a subscription with this command:
|
55
|
+
|
56
|
+
rails generate kanina:subscription SubscriptionName
|
57
|
+
|
58
|
+
Then tweak the resulting file to attach to the right queue, or use bindings to watch a named exchange. Use a subscribe block to define code that you want run when a message is received:
|
59
|
+
|
60
|
+
class NotifyUserSubscription < Kanina::Subscription
|
61
|
+
subscribe queue: "notify_user" do |data|
|
62
|
+
User.where(id: data[:id]).first.notify
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
---
|
67
|
+
|
68
|
+
This project was written by [Clinton Judy](http://judy.github.io), and uses the ISC license.
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'rdoc/task'
|
3
|
+
require 'rspec/core/rake_task'
|
4
|
+
|
5
|
+
RSpec::Core::RakeTask.new(:spec)
|
6
|
+
task default: :spec
|
7
|
+
|
8
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
9
|
+
rdoc.rdoc_dir = 'rdoc'
|
10
|
+
rdoc.title = 'Kanina'
|
11
|
+
rdoc.options << '--line-numbers'
|
12
|
+
rdoc.rdoc_files.include('README.rdoc')
|
13
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
14
|
+
end
|
15
|
+
|
16
|
+
Bundler::GemHelper.install_tasks
|
data/kanina.gemspec
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
|
2
|
+
|
3
|
+
require 'kanina/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'kanina'
|
7
|
+
s.version = Kanina::VERSION
|
8
|
+
s.authors = ['Clinton Judy']
|
9
|
+
s.email = ['clinton@j-udy.com']
|
10
|
+
s.summary = 'Rails plugin for RabbitMQ'
|
11
|
+
s.description = <<-DESC
|
12
|
+
This is a Rails plugin that makes it easier for your models
|
13
|
+
to communicate via AMQP to RabbitMQ.
|
14
|
+
DESC
|
15
|
+
s.homepage = 'https://github.com/judy/kanina'
|
16
|
+
s.license = 'ISC'
|
17
|
+
|
18
|
+
s.files = `git ls-files -z`.split("\x0")
|
19
|
+
s.test_files = s.files.grep(/spec\//)
|
20
|
+
s.require_paths = %w(lib)
|
21
|
+
|
22
|
+
s.add_dependency 'railties', '> 4.0'
|
23
|
+
s.add_dependency 'bunny', '> 1.6'
|
24
|
+
s.add_dependency 'daemons', '~> 1.1'
|
25
|
+
|
26
|
+
# All used for testing and code climate.
|
27
|
+
s.add_development_dependency 'sqlite3'
|
28
|
+
s.add_development_dependency 'rails', '~> 4.2'
|
29
|
+
s.add_development_dependency 'rake'
|
30
|
+
s.add_development_dependency 'rspec', '3.2.0'
|
31
|
+
s.add_development_dependency 'guard'
|
32
|
+
s.add_development_dependency 'guard-rspec'
|
33
|
+
s.add_development_dependency 'guard-livereload'
|
34
|
+
s.add_development_dependency 'timecop'
|
35
|
+
s.add_development_dependency 'simplecov'
|
36
|
+
s.add_development_dependency 'rubocop'
|
37
|
+
|
38
|
+
# Documentation
|
39
|
+
s.add_development_dependency 'yard'
|
40
|
+
s.add_development_dependency 'redcarpet'
|
41
|
+
s.add_development_dependency 'github-markup'
|
42
|
+
s.add_development_dependency 'guard-yard'
|
43
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
module Kanina
|
4
|
+
class InstallGenerator < Rails::Generators::Base
|
5
|
+
source_root File.expand_path('../templates', __FILE__)
|
6
|
+
|
7
|
+
# This method is called automatically by rails when you 'install' Kanina. It
|
8
|
+
# copies a sample amqp.yml file to the Rails config folder, and adds amqp.yml
|
9
|
+
# to the .gitignore.
|
10
|
+
def create_amqp_config_file
|
11
|
+
filepath = Rails.root + "config"
|
12
|
+
template "amqp.yml.sample", filepath + "amqp.yml.sample"
|
13
|
+
append_file Rails.root + '.gitignore', 'config/amqp.yml'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# Refer to Bunny's new connection parameters for settings here.
|
2
|
+
# http://rubybunny.info/articles/connecting.html
|
3
|
+
|
4
|
+
defaults: &defaults
|
5
|
+
host: localhost
|
6
|
+
port: 5672
|
7
|
+
ssl: false
|
8
|
+
vhost: /
|
9
|
+
user: guest
|
10
|
+
pass: guest
|
11
|
+
heartbeat: :server
|
12
|
+
threaded: true
|
13
|
+
|
14
|
+
development:
|
15
|
+
<<: *defaults
|
16
|
+
|
17
|
+
test:
|
18
|
+
<<: *defaults
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
module Kanina
|
4
|
+
# <tt>Kanina::MessageGenerator</tt> generates a template of a message file. Change the
|
5
|
+
# resulting file with your intended exchange or routing_key information. For example:
|
6
|
+
#
|
7
|
+
# rails generate message user_notification
|
8
|
+
class MessageGenerator < Rails::Generators::NamedBase
|
9
|
+
source_root File.expand_path('../templates', __FILE__)
|
10
|
+
|
11
|
+
# This method is automatically run by Rails when generating a new message.
|
12
|
+
# It sets up the messages folder and adds the message template. Remember to
|
13
|
+
# change the template to talk to the right exchange/queue!
|
14
|
+
def create_message_file
|
15
|
+
messages_folder = Rails.root + 'app/messages'
|
16
|
+
empty_directory messages_folder
|
17
|
+
template 'message.rb', messages_folder + "#{file_name}_message.rb"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
module Kanina
|
4
|
+
# <tt>Kanina::SubscriptionGenerator</tt> generates a subscription file, for
|
5
|
+
# example:
|
6
|
+
#
|
7
|
+
# rails generate subscription user_notification
|
8
|
+
class SubscriptionGenerator < Rails::Generators::NamedBase
|
9
|
+
source_root File.expand_path('../templates', __FILE__)
|
10
|
+
|
11
|
+
# This method is called automatically by rails when you generate the
|
12
|
+
# subscription. It sets up the subscriptions folder, then adds the
|
13
|
+
# subscription to it.
|
14
|
+
def create_subscription_file
|
15
|
+
subscriptions_folder = Rails.root + 'app/subscriptions'
|
16
|
+
empty_directory subscriptions_folder
|
17
|
+
template 'subscription.rb', subscriptions_folder + "#{file_name}_subscription.rb"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/kanina.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
3
|
+
module Kanina
|
4
|
+
# Simplifies sending messages to standard output and/or the Rails log files.
|
5
|
+
module Logger
|
6
|
+
DEFAULT_LOG_LEVEL = ::Logger::INFO
|
7
|
+
|
8
|
+
def logger
|
9
|
+
Rails.logger ||= ::Logger.new(STDOUT)
|
10
|
+
@logger ||= Rails.logger
|
11
|
+
end
|
12
|
+
|
13
|
+
def say(text, level = DEFAULT_LOG_LEVEL)
|
14
|
+
puts text if @loud
|
15
|
+
logger.add level, "HARE: #{text}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
module Kanina
|
2
|
+
# `Kanina::Message` allows you to send messages to RabbitMQ, and have
|
3
|
+
# your exchanges and bindings set up on the fly.
|
4
|
+
#
|
5
|
+
# # app/messages/user_message.rb
|
6
|
+
# class UserMessage < Kanina::Message
|
7
|
+
# exchange "user.exchange"
|
8
|
+
# end
|
9
|
+
#
|
10
|
+
# message = UserMessage.new("data")
|
11
|
+
# message.deliver
|
12
|
+
#
|
13
|
+
# Messages are encapsulated in JSON format. If you use
|
14
|
+
# `Kanina::Subscription` to receive messages, they're automatically
|
15
|
+
# parsed back out of JSON into native Ruby objects.
|
16
|
+
class Message
|
17
|
+
class << self
|
18
|
+
# Helper method to return the channel opened by `Kanina::Server`.
|
19
|
+
def channel
|
20
|
+
Kanina::Server.channel or fail 'Kanina::Server.channel is not open'
|
21
|
+
end
|
22
|
+
|
23
|
+
# @overload exchange
|
24
|
+
# @return [String] the set exchange, or the default exchange.
|
25
|
+
# @overload exchange(name, *opts)
|
26
|
+
# Set the name and type of the exchange messages should be sent to.
|
27
|
+
# @param name [String] the name of the exchange
|
28
|
+
# @param type [:direct, :fanout, :topic] the type of exchange
|
29
|
+
# @return [String] the exchange
|
30
|
+
def exchange(name = nil, type: :direct)
|
31
|
+
if name.present?
|
32
|
+
@type = type
|
33
|
+
@exchange = channel.exchange(name, type: type)
|
34
|
+
else
|
35
|
+
@exchange || channel.default_exchange
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
[:direct, :fanout, :topic].each do |type|
|
40
|
+
define_method type do |name|
|
41
|
+
exchange(name, type: type)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# Set the queue to which messages at this exchange should be sent to. Note
|
46
|
+
# that you'll probably want to use this at the exchange level, OR a
|
47
|
+
# binding at the queue level.
|
48
|
+
# @overload routing_key
|
49
|
+
# @return [String] the routing key
|
50
|
+
# @overload routing_key(name)
|
51
|
+
# Verifies that the queue exists or is created, and sets the routing key to it.
|
52
|
+
# @param name [String] the routing key
|
53
|
+
# @return [String] the routing key
|
54
|
+
def routing_key(name = nil)
|
55
|
+
if name.present?
|
56
|
+
verify_queue(name)
|
57
|
+
@routing_key = name
|
58
|
+
else
|
59
|
+
@routing_key
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# Sets outgoing messages to be persistent. **Note** that messages must be
|
64
|
+
# sent to a durable queue, or they will still be lost! By default, this is
|
65
|
+
# `true`.
|
66
|
+
def persistent
|
67
|
+
@persistent.nil? ? @persistent = true : @persistent
|
68
|
+
end
|
69
|
+
|
70
|
+
# Sets outgoing messages to be transient (*not* persistent). If the receiving queues
|
71
|
+
# are taken down, messages could be lost. This might be the desired functionality,
|
72
|
+
# if messages need to be timely and queues being destroyed aren't a big deal.
|
73
|
+
def transient
|
74
|
+
@persistent = false
|
75
|
+
end
|
76
|
+
|
77
|
+
# Creates the queue, or grabs the queue that already exists. Note that if
|
78
|
+
# the queue already exists, it must exist with the same parameters (like durability),
|
79
|
+
# or the Bunny gem is very unhappy.
|
80
|
+
def verify_queue(routing_key)
|
81
|
+
Kanina::Server.channel.queue(routing_key)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# @!attribute [rw] data
|
86
|
+
# The actual contents of the message to be sent.
|
87
|
+
attr_accessor :data
|
88
|
+
|
89
|
+
# @!attribute [w] routing_key
|
90
|
+
# Set this to change the queue that this message should be sent to.
|
91
|
+
# (Overrides any routing key set at the class level.)
|
92
|
+
attr_writer :routing_key
|
93
|
+
|
94
|
+
# Create a new message to be sent.
|
95
|
+
# @param data [Hash] The payload of the message
|
96
|
+
# @param routing_key [String] Optional, to change the queue the message is sent to.
|
97
|
+
def initialize(data = nil, routing_key: nil)
|
98
|
+
@data = data || {}
|
99
|
+
@routing_key = routing_key
|
100
|
+
@persistent = persistent
|
101
|
+
end
|
102
|
+
|
103
|
+
# The exchange this message is going into
|
104
|
+
# @return [String] exchange
|
105
|
+
def exchange
|
106
|
+
self.class.exchange
|
107
|
+
end
|
108
|
+
|
109
|
+
# @return [String] routing_key
|
110
|
+
def routing_key
|
111
|
+
@routing_key || self.class.routing_key
|
112
|
+
end
|
113
|
+
|
114
|
+
# @return [Boolean] whether this message is persistent or not
|
115
|
+
# Currently only set at the class level.
|
116
|
+
def persistent
|
117
|
+
@persistent.nil? ? self.class.persistent : @persistent
|
118
|
+
end
|
119
|
+
|
120
|
+
# @return [String] The json-formatted data being sent.
|
121
|
+
def json
|
122
|
+
data.present? ? data.to_json : {}.to_json
|
123
|
+
end
|
124
|
+
|
125
|
+
# Deliver the message to the specified exchange and/or queue.
|
126
|
+
def deliver
|
127
|
+
if exchange.name == ''
|
128
|
+
if routing_key.present?
|
129
|
+
exchange.publish(json, routing_key: routing_key, persistent: @persistent)
|
130
|
+
else
|
131
|
+
fail 'Routing key must be set when using default exchange.'
|
132
|
+
end
|
133
|
+
else
|
134
|
+
exchange.publish(json, routing_key: routing_key)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|