Sha256: 168f16fbd0b098b6dbe1195d822cf11b3215d945857d87a0e872bbc8901bd723

Contents?: true

Size: 947 Bytes

Versions: 1

Compression:

Stored size: 947 Bytes

Contents

module BunnyRpc
  class Server
    attr_accessor :reply_correlation_id, :reply_to

    def initialize(queue_name, options={})
      @queue_name = queue_name
      @options = options
    end

    def publish(payload)
      exchange.publish(
        payload,
        routing_key: @reply_to,
        correlation_id: @reply_correlation_id
      )
    end

    def subscribe
      queue.subscribe(block: true) do |_, properties, payload|
        @reply_to = properties.reply_to
        @reply_correlation_id = properties.correlation_id

        yield payload
      end
    end

    def connect
      return @connect if defined?(@connect)
      @connect = Bunny.new(@options)
      @connect.start
      @connect
    end

    def channel
      @channel ||= connect.create_channel
    end

    def exchange
      @exchange ||= channel.default_exchange
    end

    def queue
      @queue ||= channel.queue(@queue_name, exclusive: true)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bunny_rpc-0.1.1 lib/bunny_rpc/server.rb