sul-dlss/dor-event-client

View on GitHub
lib/dor/event/client/rabbit_channel_factory.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
# frozen_string_literal: true

module Dor
  module Event
    class Client
      # Factory for creating Rabbit channel.
      # Note that that channel is lazily created.
      class RabbitChannelFactory
        def initialize(hostname:, vhost:, username:, password:)
          @hostname = hostname
          @vhost = vhost
          @username = username
          @password = password
        end

        delegate :topic, to: :channel

        private

        attr_reader :vhost, :username, :password

        def hostname
          @hostname || raise(Error, 'hostname has not yet been configured')
        end

        def channel
          @channel ||= begin
            connection = Bunny.new(hostname: hostname,
                                   vhost: vhost,
                                   username: username,
                                   password: password).tap(&:start)
            connection.create_channel
          end
        end
      end
    end
  end
end