Class: BunnyMock::Exchange

Inherits:
Object
  • Object
show all
Defined in:
lib/bunny_mock/exchange.rb

Instance Attribute Summary collapse

Bunny API collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argumentsHash (readonly)

Returns Any additional declaration arguments.

Returns:

  • (Hash)

    Any additional declaration arguments



61
62
63
# File 'lib/bunny_mock/exchange.rb', line 61

def arguments
  @arguments
end

#auto_deleteBoolean (readonly) Also known as: auto_delete?

Returns If the exchange was declared with auto deletion.

Returns:

  • (Boolean)

    If the exchange was declared with auto deletion



53
54
55
# File 'lib/bunny_mock/exchange.rb', line 53

def auto_delete
  @auto_delete
end

#channelBunnyMock::Channel (readonly)

Returns Channel used by exchange.

Returns:



37
38
39
# File 'lib/bunny_mock/exchange.rb', line 37

def channel
  @channel
end

#durableBoolean (readonly) Also known as: durable?

Returns If the exchange was declared as durable.

Returns:

  • (Boolean)

    If the exchange was declared as durable



49
50
51
# File 'lib/bunny_mock/exchange.rb', line 49

def durable
  @durable
end

#internalBoolean (readonly) Also known as: internal?

Returns If the exchange was declared as internal.

Returns:

  • (Boolean)

    If the exchange was declared as internal



57
58
59
# File 'lib/bunny_mock/exchange.rb', line 57

def internal
  @internal
end

#nameString (readonly)

Returns Exchange name.

Returns:

  • (String)

    Exchange name



40
41
42
# File 'lib/bunny_mock/exchange.rb', line 40

def name
  @name
end

#optsHash (readonly)

Returns Creation options.

Returns:

  • (Hash)

    Creation options



46
47
48
# File 'lib/bunny_mock/exchange.rb', line 46

def opts
  @opts
end

#typeString (readonly)

Returns Exchange type.

Returns:

  • (String)

    Exchange type



43
44
45
# File 'lib/bunny_mock/exchange.rb', line 43

def type
  @type
end

Class Method Details

.declare(channel, name = '', opts = {}) ⇒ BunnyMock::Exchange

Create a new BunnyMock::Exchange instance

Parameters:

  • channel (BunnyMock::Channel)

    Channel this exchange will use

  • name (String) (defaults to: '')

    Name of exchange

  • opts (Hash) (defaults to: {})

    Creation options

Options Hash (opts):

  • :durable (Boolean) — default: false

    Should this exchange be durable?

  • :auto_delete (Boolean) — default: false

    Should this exchange be automatically deleted when it is no longer used?

  • :arguments (Boolean) — default: {}

    Additional optional arguments (typically used by RabbitMQ extensions and plugins)

Returns:

See Also:



20
21
22
23
24
25
26
27
28
29
# File 'lib/bunny_mock/exchange.rb', line 20

def declare(channel, name = '', opts = {})
  # get requested type
  type = opts.fetch :type, :direct

  # get needed class type
  klazz = BunnyMock::Exchanges.const_get type.to_s.capitalize

  # create exchange of desired type
  klazz.new channel, name, type, opts
end

Instance Method Details

#bind(exchange, opts = {}) ⇒ BunnyMock::Exchange

Bind this exchange to another exchange

Parameters:

  • exchange (BunnyMock::Exchange, String)

    Exchange to bind to

  • opts (Hash) (defaults to: {})

    Binding properties

Options Hash (opts):

  • :routing_key (String)

    Custom routing key

Returns:



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/bunny_mock/exchange.rb', line 144

def bind(exchange, opts = {})
  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 look up the exchange
    @channel.xchg_bind self, opts.fetch(:routing_key, @name), exchange
  end

  self
end

#bound_to?(exchange, opts = {}) ⇒ Boolean

Check if this exchange is bound to another exchange

Parameters:

  • exchange (BunnyMock::Exchange, String)

    Exchange to check

  • opts (Hash) (defaults to: {})

    Binding properties

Options Hash (opts):

  • :routing_key (String)

    Routing key from binding

Returns:

  • (Boolean)

    true if this exchange is bound to the given exchange, false otherwise



194
195
196
197
198
199
200
201
202
203
# File 'lib/bunny_mock/exchange.rb', line 194

def bound_to?(exchange, opts = {})
  if exchange.respond_to?(:routes_to?)
    # we can find out on the exchange object
    exchange.routes_to? self, opts
  else

    # we need the channel to look up the exchange
    @channel.xchg_bound_to? self, opts.fetch(:routing_key, @name), exchange
  end
end

#deleteObject

Delete this exchange



127
128
129
130
131
# File 'lib/bunny_mock/exchange.rb', line 127

def delete(*)
  @channel.deregister_exchange self

  @deleted = true
end

#deliver(payload, opts, key) ⇒ Object

Deliver a message to routes



236
237
238
# File 'lib/bunny_mock/exchange.rb', line 236

def deliver(payload, opts, key)
  # noOp
end

#has_binding?(exchange_or_queue, opts = {}) ⇒ Boolean

rubocop:disable Style/PredicateName

Returns:

  • (Boolean)


222
223
224
225
# File 'lib/bunny_mock/exchange.rb', line 222

def has_binding?(exchange_or_queue, opts = {}) # rubocop:disable Style/PredicateName
  warn '[DEPRECATED] `has_binding?` is deprecated. Please use `routes_to?` instead.'
  routes_to?(exchange_or_queue, opts)
end

#publish(payload, opts = {}) ⇒ BunnyMock::Exchange

Publish a message

Parameters:

  • payload (Object)

    Message payload

  • opts (Hash) (defaults to: {})

    Message properties

Options Hash (opts):

  • :routing_key (String)

    Routing key

  • :persistent (Boolean)

    Should the message be persisted to disk?

  • :mandatory (Boolean)

    Should the message be returned if it cannot be routed to any queue?

  • :timestamp (Integer)

    A timestamp associated with this message

  • :expiration (Integer)

    Expiration time after which the message will be deleted

  • :type (String)

    Message type, e.g. what type of event or command this message represents. Can be any string

  • :reply_to (String)

    Queue name other apps should send the response to

  • :content_type (String)

    Message content type (e.g. application/json)

  • :content_encoding (String)

    Message content encoding (e.g. gzip)

  • :correlation_id (String)

    Message correlated to this one, e.g. what request this message is a reply for

  • :priority (Integer)

    Message priority, 0 to 9. Not used by RabbitMQ, only applications

  • :message_id (String)

    Any message identifier

  • :user_id (String)

    Optional user ID. Verified by RabbitMQ against the actual connection username

  • :app_id (String)

    Optional application ID

Returns:

See Also:

  • BunnyMock::Exchange.{BunnyMock{BunnyMock::Exchanges{BunnyMock::Exchanges::Direct{BunnyMock::Exchanges::Direct#deliver}
  • BunnyMock::Exchange.{BunnyMock{BunnyMock::Exchanges{BunnyMock::Exchanges::Topic{BunnyMock::Exchanges::Topic#deliver}
  • BunnyMock::Exchange.{BunnyMock{BunnyMock::Exchanges{BunnyMock::Exchanges::Fanout{BunnyMock::Exchanges::Fanout#deliver}
  • BunnyMock::Exchange.{BunnyMock{BunnyMock::Exchanges{BunnyMock::Exchanges::Headers{BunnyMock::Exchanges::Headers#deliver}


115
116
117
118
119
120
# File 'lib/bunny_mock/exchange.rb', line 115

def publish(payload, opts = {})
  # handle message sending, varies by type
  deliver(payload, opts.merge(exchange: name), opts.fetch(:routing_key, ''))

  self
end

#routes_to?(exchange_or_queue, opts = {}) ⇒ Boolean

Check if a queue is bound to this exchange

Parameters:

  • exchange_or_queue (BunnyMock::Queue, String)

    Exchange or queue to check

  • opts (Hash) (defaults to: {})

    Binding properties

Options Hash (opts):

  • :routing_key (String)

    Custom routing key

Returns:

  • (Boolean)

    true if the given queue or exchange matching options is bound to this exchange, false otherwise



216
217
218
219
220
# File 'lib/bunny_mock/exchange.rb', line 216

def routes_to?(exchange_or_queue, opts = {})
  route = exchange_or_queue.respond_to?(:name) ? exchange_or_queue.name : exchange_or_queue
  rk = opts.fetch(:routing_key, route)
  @routes.key?(rk) && @routes[rk].any? { |r| r == exchange_or_queue }
end

#unbind(exchange, opts = {}) ⇒ BunnyMock::Exchange

Unbind this exchange from another exchange

Parameters:

  • exchange (BunnyMock::Exchange, String)

    Exchange to unbind from

  • opts (Hash) (defaults to: {})

    Binding properties

Options Hash (opts):

  • :routing_key (String)

    Custom routing key

Returns:



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/bunny_mock/exchange.rb', line 169

def unbind(exchange, opts = {})
  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 look up the exchange
    @channel.xchg_unbind opts.fetch(:routing_key, @name), exchange, self
  end

  self
end