class EasyBunnyRPC::Client

Public Class Methods

new(options={}) click to toggle source
# File lib/easy_bunny_rpc/client.rb, line 7
def initialize options={}
  @options = options
  @subscribed = false

  # The timed queue which will collect incoming messages
  @timed_queue = EasyBunnyRPC::TimedQueue.new

  # Need to set a default timeout. How about 5 seconds?
  set_timeout(5)
end

Public Instance Methods

close() click to toggle source
# File lib/easy_bunny_rpc/client.rb, line 33
def close
  if defined?(@channel)
    channel.close
    remove_instance_variable :@channel
    remove_instance_variable :@queue
  end

  if defined?(@connection)
    connection.close
    remove_instance_variable :@connection
  end

  if defined?(@exchange)
    remove_instance_variable :@exchange
  end

  @subscribed = false
end
pop() click to toggle source
# File lib/easy_bunny_rpc/client.rb, line 18
def pop
  correlation_id, payload = @timed_queue.pop_with_timeout(@timeout)

  JSON.parse(payload).merge!({ 'correlation_id' => correlation_id })
end
publish(payload, correlation_id=default_correlation_id) click to toggle source
# File lib/easy_bunny_rpc/client.rb, line 28
def publish(payload, correlation_id=default_correlation_id)
  start_subscription unless @subscribed
  exchange.publish([payload].to_json, routing_key: @options[:queue], correlation_id: correlation_id, reply_to: queue.name, expiration: (@timeout*1000).to_i)
end
set_timeout(value) click to toggle source
# File lib/easy_bunny_rpc/client.rb, line 24
def set_timeout(value)
  @timeout = value
end

Private Instance Methods

channel() click to toggle source
# File lib/easy_bunny_rpc/client.rb, line 70
def channel
  @channel ||= connection.create_channel
end
connection() click to toggle source
# File lib/easy_bunny_rpc/client.rb, line 62
def connection
  return @connection if defined?(@connection)

  @connection = Bunny.new(@options[:bunny])
  @connection.start
  @connection
end
default_correlation_id() click to toggle source
# File lib/easy_bunny_rpc/client.rb, line 87
def default_correlation_id
  ''
end
exchange() click to toggle source
# File lib/easy_bunny_rpc/client.rb, line 83
def exchange
  @exchange ||= channel.default_exchange
end
generate_queue_name() click to toggle source
# File lib/easy_bunny_rpc/client.rb, line 79
def generate_queue_name
  ['reply-', @options[:bunny][:user], '_', SecureRandom.hex].join
end
queue() click to toggle source

The exclusive no-name queue that is created for communicating back to me

# File lib/easy_bunny_rpc/client.rb, line 75
def queue
  @queue ||= channel.queue(generate_queue_name, exclusive: true)
end
start_subscription() click to toggle source
# File lib/easy_bunny_rpc/client.rb, line 54
def start_subscription
  queue.subscribe(block: false) do |delivery_info, properties, payload|
    @timed_queue.push([properties.correlation_id, payload])
  end

  @subscribed = true
end