Class: BunnyMock::Queue
- Inherits:
-
Object
- Object
- BunnyMock::Queue
- Defined in:
- lib/bunny_mock/queue.rb
Instance Attribute Summary collapse
-
#channel ⇒ BunnyMock::Channel
readonly
Channel used by queue.
-
#name ⇒ String
readonly
Queue name.
-
#opts ⇒ Hash
readonly
Creation options.
Bunny API collapse
-
#bind(exchange, opts = {}) ⇒ Object
Bind this queue to an exchange.
-
#publish(payload, opts = {}) ⇒ BunnyMock::Queue
Publish a message.
-
#subscribe(*args, &block) ⇒ Object
Adds a consumer to the queue (subscribes for message deliveries).
-
#subscribe_with(consumer, *args) ⇒ Object
Adds a specific consumer object to the queue (subscribes for message deliveries).
-
#unbind(exchange, opts = {}) ⇒ Object
Unbind this queue from an exchange.
Instance Method Summary collapse
-
#all ⇒ Array
Get all messages in queue.
-
#bound_to?(exchange, opts = {}) ⇒ Boolean
Check if this queue is bound to the exchange.
-
#delete ⇒ Object
Deletes this queue.
-
#initialize(channel, name = '', opts = {}) ⇒ Queue
constructor
Create a new [BunnyMock::Queue] instance.
-
#message_count ⇒ Integer
Count of messages in queue.
-
#pop(opts = { manual_ack: false }, &block) ⇒ Hash
(also: #get)
Get oldest message in queue.
-
#purge ⇒ Object
Clear all messages in queue.
Constructor Details
#initialize(channel, name = '', opts = {}) ⇒ Queue
Create a new [BunnyMock::Queue] instance
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/bunny_mock/queue.rb', line 26 def initialize(channel, name = '', opts = {}) # Store creation information @channel = channel @name = name @opts = opts # Store messages @messages = [] # Store consumers subsribed to that queue @consumers = [] # marks if this queue is deleted @deleted = false end |
Instance Attribute Details
#channel ⇒ BunnyMock::Channel (readonly)
Returns Channel used by queue.
9 10 11 |
# File 'lib/bunny_mock/queue.rb', line 9 def channel @channel end |
#name ⇒ String (readonly)
Returns Queue name.
12 13 14 |
# File 'lib/bunny_mock/queue.rb', line 12 def name @name end |
#opts ⇒ Hash (readonly)
Returns Creation options.
15 16 17 |
# File 'lib/bunny_mock/queue.rb', line 15 def opts @opts end |
Instance Method Details
#all ⇒ Array
Get all messages in queue
225 226 227 |
# File 'lib/bunny_mock/queue.rb', line 225 def all @messages end |
#bind(exchange, opts = {}) ⇒ Object
Bind this queue to an exchange
118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/bunny_mock/queue.rb', line 118 def bind(exchange, opts = {}) check_queue_deleted! if exchange.respond_to?(:add_route) # we can do the binding ourselves exchange.add_route opts.fetch(:routing_key, @name), self else # we need the channel to lookup the exchange @channel.queue_bind self, opts.fetch(:routing_key, @name), exchange end self end |
#bound_to?(exchange, opts = {}) ⇒ Boolean
Check if this queue is bound to the exchange
170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/bunny_mock/queue.rb', line 170 def bound_to?(exchange, opts = {}) check_queue_deleted! if exchange.respond_to?(:routes_to?) # we can do the check ourselves exchange.routes_to? self, opts else # we need the channel to lookup the exchange @channel.xchg_routes_to? self, opts.fetch(:routing_key, @name), exchange end end |
#delete ⇒ Object
Deletes this queue
234 235 236 237 |
# File 'lib/bunny_mock/queue.rb', line 234 def delete @channel.deregister_queue self @deleted = true end |
#message_count ⇒ Integer
Count of messages in queue
188 189 190 |
# File 'lib/bunny_mock/queue.rb', line 188 def @messages.count end |
#pop(opts = { manual_ack: false }, &block) ⇒ Hash Also known as: get
Get oldest message in queue
198 199 200 201 202 203 204 205 |
# File 'lib/bunny_mock/queue.rb', line 198 def pop(opts = { manual_ack: false }, &block) if BunnyMock.use_bunny_queue_pop_api bunny_pop(opts, &block) else warn '[DEPRECATED] This behavior is deprecated - please set `BunnyMock::use_bunny_queue_pop_api` to true to use Bunny Queue#pop behavior' @messages.shift end end |
#publish(payload, opts = {}) ⇒ BunnyMock::Queue
Publish a message
69 70 71 72 73 74 75 76 |
# File 'lib/bunny_mock/queue.rb', line 69 def publish(payload, opts = {}) check_queue_deleted! # add to messages @messages << { message: payload, options: opts } yield_consumers self end |
#purge ⇒ Object
Clear all messages in queue
213 214 215 216 217 |
# File 'lib/bunny_mock/queue.rb', line 213 def purge @messages = [] self end |
#subscribe(*args, &block) ⇒ Object
Adds a consumer to the queue (subscribes for message deliveries).
Params are so they can be used when the message is processed. Takes a block which is called when a message is delivered to the queue
86 87 88 89 90 91 |
# File 'lib/bunny_mock/queue.rb', line 86 def subscribe(*args, &block) @consumers << [block, args] yield_consumers self end |
#subscribe_with(consumer, *args) ⇒ Object
Adds a specific consumer object to the queue (subscribes for message deliveries).
Secondary params are so they can be used when the message is processed.
101 102 103 104 105 106 |
# File 'lib/bunny_mock/queue.rb', line 101 def subscribe_with(consumer, *args) @consumers << [consumer, args] yield_consumers self end |
#unbind(exchange, opts = {}) ⇒ Object
Unbind this queue from an exchange
143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/bunny_mock/queue.rb', line 143 def unbind(exchange, opts = {}) check_queue_deleted! if exchange.respond_to?(:remove_route) # we can do the unbinding ourselves exchange.remove_route opts.fetch(:routing_key, @name), self else # we need the channel to lookup the exchange @channel.queue_unbind self, opts.fetch(:routing_key, @name), exchange end end |