Sha256: b25ce780673171f4de0b4ff30b00ac4a3814708a30bd5fccebc31efd66533cf7

Contents?: true

Size: 1.87 KB

Versions: 2

Compression:

Stored size: 1.87 KB

Contents

require 'bunny'

# Overriding the Bunny gem's modules and classes.
#
# NOTE: Not everything in Bunny is stubbed here. Pull requests are welcome.
module Bunny
  def self.run(options = {})
    bunny = Bunny::Client.new(options)
    yield bunny
  end

  # Resets the class-level exchanges.
  def self.reset_exchanges
    Bunny::Client.send(:class_variable_set, :@@exchanges, {})
  end

  class Client
    @@exchanges = {} # class variable simulating exchanges in the message broker

    def initialize(options = {})
    end

    def exchange(name, options = {})
      @@exchanges[name] ||= Bunny::Exchange.new(self, name, options)
    end

    # Expose the exchanges.
    def exchanges
      @@exchanges
    end

    def queue(queue_name, options=nil)
      Bunny::Queue.new nil
    end
  end

  class Exchange
    attr_accessor :client, :type, :name

    def initialize(client, name, options = {})
      @client, @name  = client, name
      @type = options[:type]
    end

    def routed_messages
      @routed_messaged ||= []
    end

    # To facilite testing this adds a Struct containing the data
    # +message+ and routing +key+ to the +routed_messages+.
    #
    # Example usage:
    # Bunny.run do |b|
    #   topic = b.exchange("some_topic_name", SOME_EXCHANGE_OPTIONS)
    #   options = { :key => "some.routing.key" }.merge(SOME_PUBLISH_OPTIONS)
    #   topic.publish("some message", options)
    # end
    #
    # Testing:
    # ...
    # routed_message = @bunny.exchanges["some_topic_name"].routed_messages[0]
    # assert_equal "some message", routed_message.message
    # assert_equal "some.routing.key", routed_message.key
    def publish(data, options = {})
      routed_messages << Struct.new(:message, :key).new(data, options[:key])
    end
  end

  class Queue
    def initialize(channel_or_connection, name = nil, opts = {})
    end

    def delete(*args)
      :delete_ok
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
stuffed_bunny-1.0.3 lib/stuffed_bunny/bunny_overrides.rb
stuffed_bunny-1.0.2 lib/stuffed_bunny/bunny_overrides.rb