Class: Surfliner::Mq::Topic
- Inherits:
-
Object
- Object
- Surfliner::Mq::Topic
- Includes:
- Util::Assert
- Defined in:
- lib/surfliner/mq/topic.rb
Overview
Encapsulates a RabbitMQ topic
Instance Attribute Summary collapse
-
#channel ⇒ Bunny::Channel
readonly
The channel being used.
-
#logger ⇒ Logger
readonly
The logger.
-
#name ⇒ String
readonly
The name of the topic.
-
#options ⇒ Hash
readonly
RabbitMQ topic options.
Class Method Summary collapse
-
.from_config(config, channel:, logger:) ⇒ Object
Creates a new
MqTopicfrom the specified configuration.
Instance Method Summary collapse
-
#bind_queue(config = QueueConfig.from_env, routing_key: default_routing_key) ⇒ Bunny::Queue
Creates or looks up the specified queue and binds it to receive messages with the specified routing key.
-
#default_routing_key ⇒ String?
Returns the default (environment-variable-based) routing key.
-
#exchange ⇒ Bunny::Exchange
The exchange for the topic.
-
#initialize(name, channel:, logger:, options: {}) ⇒ Topic
constructor
A new instance of Topic.
-
#publish(payload, routing_key: default_routing_key) ⇒ Bunny::Exchange
Publishes the specified payload.
-
#queue(config) ⇒ Bunny::Queue
Creates or looks up the specified queue.
Methods included from Util::Assert
Constructor Details
#initialize(name, channel:, logger:, options: {}) ⇒ Topic
Returns a new instance of Topic.
25 26 27 28 29 30 |
# File 'lib/surfliner/mq/topic.rb', line 25 def initialize(name, channel:, logger:, options: {}) @name = not_nil!(name, "topic name") @channel = not_nil!(channel, "channel") @logger = not_nil!(logger, "logger") @options = not_nil!(, "options") end |
Instance Attribute Details
#channel ⇒ Bunny::Channel (readonly)
Returns The channel being used.
13 14 15 |
# File 'lib/surfliner/mq/topic.rb', line 13 def channel @channel end |
#logger ⇒ Logger (readonly)
Returns The logger.
16 17 18 |
# File 'lib/surfliner/mq/topic.rb', line 16 def logger @logger end |
#name ⇒ String (readonly)
Returns The name of the topic.
10 11 12 |
# File 'lib/surfliner/mq/topic.rb', line 10 def name @name end |
#options ⇒ Hash (readonly)
Returns RabbitMQ topic options. (See Bunny::Channel#topic).
19 20 21 |
# File 'lib/surfliner/mq/topic.rb', line 19 def @options end |
Class Method Details
.from_config(config, channel:, logger:) ⇒ Object
Creates a new MqTopic from the specified configuration
78 79 80 |
# File 'lib/surfliner/mq/topic.rb', line 78 def from_config(config, channel:, logger:) new(config.name, channel:, logger:, options: config.) end |
Instance Method Details
#bind_queue(config = QueueConfig.from_env, routing_key: default_routing_key) ⇒ Bunny::Queue
Creates or looks up the specified queue and binds it to receive messages with the specified routing key.
51 52 53 |
# File 'lib/surfliner/mq/topic.rb', line 51 def bind_queue(config = QueueConfig.from_env, routing_key: default_routing_key) queue(config).tap { |q| q.bind(exchange, routing_key:) } end |
#default_routing_key ⇒ String?
Returns the default (environment-variable-based) routing key
| Variable | Sample value | Description |
|---|---|---|
RABBITMQ_PLATFORM_ROUTING_KEY |
surfliner.metadata.daylight |
RabbitMQ routing key |
69 70 71 |
# File 'lib/surfliner/mq/topic.rb', line 69 def default_routing_key ENV.fetch("RABBITMQ_PLATFORM_ROUTING_KEY") end |
#exchange ⇒ Bunny::Exchange
Returns The exchange for the topic.
33 34 35 |
# File 'lib/surfliner/mq/topic.rb', line 33 def exchange @exchange ||= channel.topic(name, ) end |
#publish(payload, routing_key: default_routing_key) ⇒ Bunny::Exchange
Publishes the specified payload
41 42 43 44 |
# File 'lib/surfliner/mq/topic.rb', line 41 def publish(payload, routing_key: default_routing_key) logger.info "Publishing to #{routing_key} with payload: #{payload}" exchange.publish(payload, routing_key:) end |
#queue(config) ⇒ Bunny::Queue
Creates or looks up the specified queue.
58 59 60 |
# File 'lib/surfliner/mq/topic.rb', line 58 def queue(config) channel.queue(config.name, config.) end |