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.5.1.pre.217 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.5.1 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.5.1.pre.215 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.5.0 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.5.0.pre.213 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.5.0.pre.210 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.5.0.pre.209 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.4.0.pre.208 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.3.1.pre.201 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.3.1.pre.198 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.3.0.pre.197 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.3.0.pre.196 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.3.0 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.3.0.pre.194 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.3.0.pre.192 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.3.0.pre.170 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.2.2 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.2.1 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.2.1.pre.181 lib/rrj/rabbit/connect.rb
ruby_rabbitmq_janus-2.2.1.pre.180 lib/rrj/rabbit/connect.rb