Sha256: 2eb7e50c33bd4c020b14d7b3df6da169c6575ff19c128afb93fcbf68fd545df9

Contents?: true

Size: 1.86 KB

Versions: 24

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

# :reek:TooManyStatements

module RubyRabbitmqJanus
  module Rabbit
    # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>
    #
    # Class for manage connection with RabbitMQ
    class Connect
      # Initialize connection to server RabbitMQ
      def initialize
        @rabbit = Bunny.new(read_options_server.merge!(option_log_rabbit))
      rescue => error
        raise Errors::Rabbit::Connect::Initialize, error
      end

      # Create an transaction with rabbitmq and close after response is received
      def transaction_short
        response = transaction_long { yield }
        close
        response
      rescue => error
        raise Errors::Rabbit::Connect::TransactionShort, error
      end

      # Create an transaction with rabbitmq and not close
      def transaction_long
        start
        yield
      rescue => error
        raise Errors::Rabbit::Connect::TransactionLong, error
      end

      # Openning a connection with Rabbitmq
      def start
        @rabbit.start
      rescue => error
        raise Errors::Rabbit::Connect::Start, error
      end

      # Close connection to server RabbitMQ
      def close
        @rabbit.close
      rescue => error
        raise Errors::Rabbit::Connect::Close, error
      end

      # Create an channel
      def channel
        @rabbit.create_channel
      rescue => error
        raise Errors::Rabbit::Connect::Channel, error
      end

      private

      def read_options_server
        conn = %w[host port pass user vhost]
        cfg = Tools::Config.instance.options['rabbit']
        Hash[conn.map { |value| [value.to_sym, cfg[value]] }]
      end

      def option_log_rabbit
        lvl = Tools::Config.instance.log_level_rabbit.upcase.to_sym
        {
          log_level: Tools::Log::LEVELS[lvl],
          log_file: Tools::Log.instance.logdev
        }
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
ruby_rabbitmq_janus-2.2.0.pre.179 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.2.0 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.2.0.pre.177 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.2.0.pre.173 lib/rrj/rabbit/connect.rb