# frozen_string_literal: true

module RubyRabbitmqJanus
  module Rabbit
    # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>

    # @!attribute [r] correlation
    #   @return [String] Is a string uniq generated by SecureRandom
    #
    # Manage properties to message sending in rabbitmq queue
    class Propertie
      attr_reader :correlation

      # Initialize a message sending to rabbitmq
      def initialize(instance = 1)
        Tools::Log.instance.debug 'initalize a propertie to message'
        @correlation = SecureRandom.uuid
        @instance = instance
      rescue
        raise Errors::Rabbit::Propertie::Initialize
      end

      # Define options sending to rabbitmq
      def options
        base.merge(routing_key: Tools::Cluster.instance.queue_to(@instance))
      rescue
        raise Errors::Rabbit::Propertie::Options
      end

      # Define option sending to rabbitmq for janus admin message
      def options_admin(type_request)
        base.merge(routing_key: determine_routing_key(type_request))
      rescue
        raise Errors::Rabbit::Propertie::Options_admin
      end

      private

      def determine_routing_key(type_request)
        cluster = Tools::Cluster.instance

        if type_request.include?('admin')
          cluster.queue_admin_to(@instance)
        else
          cluster.queue_to(@instance)
        end
      end

      def base
        { correlation_id: @correlation, content_type: 'application/json' }
      end
    end
  end
end

# frozen_string_literal: true

module RubyRabbitmqJanus
  module Rabbit
    # @author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>

    # @!attribute [r] correlation
    #   @return [String] Is a string uniq generated by SecureRandom
    #
    # Manage properties to message sending in rabbitmq queue
    class Propertie
      attr_reader :correlation

      # Initialize a message sending to rabbitmq
      def initialize(instance = 1)
        Tools::Log.instance.debug 'initalize a propertie to message'
        @correlation = SecureRandom.uuid
        @instance = instance
      rescue
        raise Errors::Rabbit::Propertie::Initialize
      end

      # Define options sending to rabbitmq
      def options
        base.merge(routing_key: Tools::Cluster.instance.queue_to(@instance))
      rescue
        raise Errors::Rabbit::Propertie::Options
      end

      # Define option sending to rabbitmq for janus admin message
      def options_admin(type_request)
        base.merge(routing_key: Tools::Cluster.instance.queue_admin_to(@instance))
      rescue
        raise Errors::Rabbit::Propertie::Options_admin
      end

      private

      def determine_routing_key(type_request)
        cluster = Tools::Cluster.instance

        if type_request.include?('admin')
          cluster.queue_admin_to(@instance)
        else
          cluster.queue_to(@instance)
        end
      end

      def base
        { correlation_id: @correlation, content_type: 'application/json' }
      end
    end
  end
end

Fix routing_key for admin queue
Fix routing_key for admin queue
