class Barrister::Amqp::Container

Public Class Methods

new(json_path, service_name, handlers=[], options={}) click to toggle source

NOTE: A container needs to call the Barrister::Server to handle the request and send the response to the client.

# File lib/barrister/amqp.rb, line 68
def initialize(json_path, service_name, handlers=[], options={})
  conn = Bunny.new ENV.fetch('AMQP_URL')
  conn.start
  @ch = conn.create_channel
  @service_q = @ch.queue(service_name, auto_delete: false)
  @x = @ch.default_exchange

  contract = Barrister::contract_from_file(json_path)
  @server  = Barrister::Server.new(contract)

  Array(handlers).each do |handler|
    iface_name = handler.class.to_s.split('::').last
    @server.add_handler iface_name, handler
  end
end

Public Instance Methods

add_handler(iface_name, handler) click to toggle source

TODO: use delegator

# File lib/barrister/amqp.rb, line 85
def add_handler(iface_name, handler)
  @server.add_handler iface_name, handler
end
start() click to toggle source
# File lib/barrister/amqp.rb, line 89
def start
  @service_q.subscribe(block: true) do |delivery_info, properties, payload|
    payload = Config.wrapper.new(payload).unwrap
    puts "handling payload:  #{payload}" if Config.debug
    @x.publish(@server.handle_json(payload), { routing_key: properties.reply_to, correlation_id: properties.correlation_id })
  end
end