Sha256: ef8625d6844b35b527ebb74871ecb02f9c69c5bb462561dcf76b10c84a99303b

Contents?: true

Size: 1.89 KB

Versions: 9

Compression:

Stored size: 1.89 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
        cfg = Tools::Config.instance.options['rabbit']
        opts = {}
        %w[host port pass user vhost].each do |val|
          opts.merge!(val.to_sym => cfg[val])
        end
        opts
      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

9 entries across 9 versions & 1 rubygems

Version Path
ruby_rabbitmq_janus-2.2.0.pre.170 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.2.0.pre.168 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.2.0.pre.167 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.2.0.pre.166 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.2.0.pre.165 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.2.0.pre.164 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.2.0.pre.161 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.2.0.pre.159 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.2.0.pre.42 lib/rrj/rabbit/connect.rb