Sha256: f20d09ebc708231c5efbdb7655dbdf5882798eb0da56770d53965755892d1a24

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

module AMQP
  class Exchange
    attr_accessor :server, :type, :name, :opts, :key

    def initialize(server, type, name, opts = {})
      @server, @type, @name, @opts = server, type, name, opts
      @key = opts[:key]

      unless name == "amq.#{type}" or name == ''
        server.send_frame(
          Protocol::Exchange::Declare.new(
            { :exchange => name, :type => type, :nowait => true }.merge(opts)
          )
        )
      end
    end
    attr_reader :name, :type, :key

    def publish(data, opts = {})
      out = []

      out << Protocol::Basic::Publish.new(
        { :exchange => name, :routing_key => opts.delete(:key) || key }.merge(opts)
      )
      data = data.to_s
      out << Protocol::Header.new(
        Protocol::Basic,
        data.length, {
          :content_type  => 'application/octet-stream',
          :delivery_mode => (opts.delete(:persistent) ? 2 : 1),
          :priority      => 0 
        }.merge(opts)
      )
      out << Frame::Body.new(data)

      server.send_frame(*out)
    end

    def delete(opts = {})
      server.send_frame(Protocol::Exchange::Delete.new({ :exchange => name, :nowait => true }.merge(opts)))
    end

    def reset
      initialize(server, type, name, opts)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
famoseagle-carrot-0.1.0 lib/amqp/exchange.rb
famoseagle-carrot-0.2.0 lib/amqp/exchange.rb