Class: Carrot::AMQP::Exchange

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(carrot, type, name, opts = {}) ⇒ Exchange

Returns a new instance of Exchange.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/carrot/amqp/exchange.rb', line 5

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

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

Instance Attribute Details

#carrotObject (readonly)

Returns the value of attribute carrot.



3
4
5
# File 'lib/carrot/amqp/exchange.rb', line 3

def carrot
  @carrot
end

#keyObject (readonly)

Returns the value of attribute key.



3
4
5
# File 'lib/carrot/amqp/exchange.rb', line 3

def key
  @key
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/carrot/amqp/exchange.rb', line 3

def name
  @name
end

#optsObject (readonly)

Returns the value of attribute opts.



3
4
5
# File 'lib/carrot/amqp/exchange.rb', line 3

def opts
  @opts
end

#serverObject (readonly)

Returns the value of attribute server.



3
4
5
# File 'lib/carrot/amqp/exchange.rb', line 3

def server
  @server
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/carrot/amqp/exchange.rb', line 3

def type
  @type
end

Instance Method Details

#delete(opts = {}) ⇒ Object



40
41
42
43
44
45
# File 'lib/carrot/amqp/exchange.rb', line 40

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

#publish(data, opts = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/carrot/amqp/exchange.rb', line 20

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

#resetObject



47
48
49
# File 'lib/carrot/amqp/exchange.rb', line 47

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