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 |
# 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 = [] 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
221 222 223 |
# File 'lib/bunny_mock/queue.rb', line 221 def all @messages end |
#bind(exchange, opts = {}) ⇒ Object
Bind this queue to an exchange
114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/bunny_mock/queue.rb', line 114 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
166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/bunny_mock/queue.rb', line 166 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
230 231 232 233 |
# File 'lib/bunny_mock/queue.rb', line 230 def delete @channel.deregister_queue self @deleted = true end |
#message_count ⇒ Integer
Count of messages in queue
184 185 186 |
# File 'lib/bunny_mock/queue.rb', line 184 def @messages.count end |
#pop(opts = { manual_ack: false }, &block) ⇒ Hash Also known as: get
Get oldest message in queue
194 195 196 197 198 199 200 201 |
# File 'lib/bunny_mock/queue.rb', line 194 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
63 64 65 66 67 68 69 70 |
# File 'lib/bunny_mock/queue.rb', line 63 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
209 210 211 212 213 |
# File 'lib/bunny_mock/queue.rb', line 209 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
80 81 82 83 84 85 86 |
# File 'lib/bunny_mock/queue.rb', line 80 def subscribe(*args, &block) @consumers ||= [] @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.
96 97 98 99 100 101 102 |
# File 'lib/bunny_mock/queue.rb', line 96 def subscribe_with(consumer, *args) @consumers ||= [] @consumers << [consumer, args] yield_consumers self end |
#unbind(exchange, opts = {}) ⇒ Object
Unbind this queue from an exchange
139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/bunny_mock/queue.rb', line 139 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 |