class Warren::App::ExchangeConfig

Generate configuration for the various exchange types

Constants

EXCHANGE_PROMPT

Attributes

bindings[R]

@return [Array] An array of all binding configurations

Public Class Methods

ask(shell) click to toggle source

Prompts the user to configure multiple queue bindings and returns the bindings array.

@param shell [Thor::Shell::Basic] A thor shell object for user communication

@return [Array<Hash>] A configuration array

# File lib/warren/app/exchange_config.rb, line 27
def self.ask(shell)
  ExchangeConfig.new(shell).tap(&:gather_bindings).bindings
end
default_dead_letter(name) click to toggle source
# File lib/warren/app/exchange_config.rb, line 69
def self.default_dead_letter(name)
  new(nil).add_binding('fanout', name, {})
end
new(shell) click to toggle source
# File lib/warren/app/exchange_config.rb, line 46
def initialize(shell)
  @shell = shell
  @bindings = []
end
parse(shell, bindings) click to toggle source

Extracts the binding configuration from the command line parameters

@param shell [Array<String>] The binding configuration parameters

@return [Array<Hash>] A configuration array

# File lib/warren/app/exchange_config.rb, line 38
def self.parse(shell, bindings)
  return if bindings.nil?

  ExchangeConfig.new(shell).tap do |config|
    config.parse(bindings)
  end.bindings
end

Public Instance Methods

add_binding(type, name, options) click to toggle source
# File lib/warren/app/exchange_config.rb, line 73
def add_binding(type, name, options)
  @bindings << config(type, name, options)
end
gather_bindings() click to toggle source
# File lib/warren/app/exchange_config.rb, line 51
def gather_bindings
  loop do
    case ask_exchange_type
    when 'd' then ask_direct_binding
    when 'f' then ask_fanout_binding
    when 't' then ask_topic_binding
    when 'h' then ask_header_binding
    when 'n' then break
    end
  end
end
parse(bindings) click to toggle source
# File lib/warren/app/exchange_config.rb, line 63
def parse(bindings)
  bindings.each do |binding|
    add_cli_binding(*binding.split(':'))
  end
end

Private Instance Methods

add_cli_binding(type, name = nil, routing_keys = nil) click to toggle source

This could do with refactoring, but that probably means extracting each exchange type out into its own class.

# File lib/warren/app/exchange_config.rb, line 89
def add_cli_binding(type, name = nil, routing_keys = nil)
  case type.downcase
  when 'direct' then add_binding(type, name, { routing_key: routing_keys })
  when 'fanout' then add_binding(type, name, {})
  when 'topic'
    raise(Thor::Error, "Could not extract routing key from #{binding}") if routing_keys.nil?

    routing_keys.split(',').each { |key| add_binding(type, name, { routing_key: key }) }
  when 'header' then add_binding(type, name, { arguments: {} })
  else
    raise Thor::Error, "Unrecognized exchange type: #{type}"
  end
end
ask_direct_binding() click to toggle source
# File lib/warren/app/exchange_config.rb, line 103
def ask_direct_binding
  exchange = ask_exchange
  routing_key_tip
  routing_key = @shell.ask 'Specify a routing_key: '
  add_binding('direct', exchange, { routing_key: routing_key })
end
ask_exchange() click to toggle source
# File lib/warren/app/exchange_config.rb, line 83
def ask_exchange
  @shell.ask 'Specify an exchange: '
end
ask_exchange_type() click to toggle source
# File lib/warren/app/exchange_config.rb, line 79
def ask_exchange_type
  @shell.ask(EXCHANGE_PROMPT, limited_to: %w[d f t h n])
end
ask_fanout_binding() click to toggle source
# File lib/warren/app/exchange_config.rb, line 110
def ask_fanout_binding
  exchange = ask_exchange
  add_binding('fanout', exchange, {})
end
ask_header_binding() click to toggle source
# File lib/warren/app/exchange_config.rb, line 115
def ask_header_binding
  exchange = ask_exchange
  @shell.say 'Please manually configure the arguments parameter in the yaml'
  add_binding('header', exchange, { arguments: {} })
end
ask_topic_binding() click to toggle source
# File lib/warren/app/exchange_config.rb, line 121
def ask_topic_binding
  exchange = ask_exchange
  routing_key_tip
  loop do
    routing_key = @shell.ask 'Specify a routing_key [Leave blank to stop adding]: '
    break if routing_key == ''

    add_binding('topic', exchange, { routing_key: routing_key })
  end
end
config(type, name, options) click to toggle source
# File lib/warren/app/exchange_config.rb, line 132
def config(type, name, options)
  {
    'exchange' => { 'name' => name, 'options' => { type: type, durable: true } },
    'options' => options
  }
end
routing_key_tip() click to toggle source
# File lib/warren/app/exchange_config.rb, line 139
def routing_key_tip
  # Suggested cop style of %<routing_key_prefix>s but prefer suggesting the simpler option as it
  # would be all to easy to miss out the 's', resulting in varying behaviour depending on the following
  # character
  # rubocop:disable Style/FormatStringToken
  @shell.say(
    'Tip: Use %{routing_key_prefix} in routing keys to reference the routing_key_prefix specified in warren.yml'
  )
  # rubocop:enable Style/FormatStringToken
end