Class: HotBunnies::Exchange

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(channel, name, options = {}) ⇒ Exchange

Returns a new instance of Exchange.



7
8
9
10
11
# File 'lib/hot_bunnies/exchange.rb', line 7

def initialize(channel, name, options={})
  @channel = channel
  @name = name
  @options = {:type => :fanout, :durable => false, :auto_delete => false, :internal => false, :passive => false}.merge(options)
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



5
6
7
# File 'lib/hot_bunnies/exchange.rb', line 5

def channel
  @channel
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/hot_bunnies/exchange.rb', line 5

def name
  @name
end

Instance Method Details

#bind(exchange, options = {}) ⇒ Object



22
23
24
25
# File 'lib/hot_bunnies/exchange.rb', line 22

def bind(exchange, options={})
  exchange_name = if exchange.respond_to?(:name) then exchange.name else exchange.to_s end
  @channel.exchange_bind(@name, exchange_name, options.fetch(:routing_key, ''))
end

#declare!Object



31
32
33
34
35
36
37
38
# File 'lib/hot_bunnies/exchange.rb', line 31

def declare!
  unless predefined?
    if @options[:passive]
    then @channel.exchange_declare_passive(@name)
    else @channel.exchange_declare(@name, @options[:type].to_s, @options[:durable], @options[:auto_delete], @options[:internal], @options[:arguments])
    end
  end
end

#delete(options = {}) ⇒ Object



18
19
20
# File 'lib/hot_bunnies/exchange.rb', line 18

def delete(options={})
  @channel.exchange_delete(@name, options.fetch(:if_unused, false))
end

#predefined?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/hot_bunnies/exchange.rb', line 27

def predefined?
  @name.empty? || @name.start_with?("amq.")
end

#publish(body, options = {}) ⇒ Object



13
14
15
16
# File 'lib/hot_bunnies/exchange.rb', line 13

def publish(body, options={})
  options = {:routing_key => '', :mandatory => false, :immediate => false}.merge(options)
  @channel.basic_publish(@name, options[:routing_key], options[:mandatory], options[:immediate], build_properties_from(options.fetch(:properties, Hash.new)), body.to_java_bytes)
end