module EventHub::Configuration

Configuraiton module

Attributes

config_data[R]
config_file[R]
detached[R]
environment[R]
name[R]

Public Instance Methods

data() click to toggle source
# File lib/eventhub/configuration.rb, line 132
def data
  warn "[DEPRECATION] `data` is deprecated. Please use new configuration"\
       " access method."
  stringify_keys(@config_data)
end
deep_merge!(target, data) click to toggle source

Deep merging of hashes deep_merge by Stefan Rusterholz, see www.ruby-forum.com/topic/142809

# File lib/eventhub/configuration.rb, line 84
def deep_merge!(target, data)
  return if data.nil?
  merger = proc do |_, v1, v2|
    v1.is_a?(Hash) && v2.is_a?(Hash) ? v1.merge(v2, &merger) : v2
  end
  target.merge! data, &merger
end
default_configuration() click to toggle source
# File lib/eventhub/configuration.rb, line 102
def default_configuration
  {
    server: {
      user: "guest",
      password: "guest",
      host: "localhost",
      vhost: "event_hub",
      port: 5672,
      tls: false,
      tls_cert: nil,
      tls_key: nil,
      tls_ca_certificates: [],
      verify_peer: false,
      show_bunny_logs: false
    },
    processor: {
      heartbeat_cycle_in_s: 300,
      watchdog_cycle_in_s: 60,
      restart_in_s: 15,
      listener_queues: [@name]
    }
  }
end
instance() click to toggle source
# File lib/eventhub/configuration.rb, line 126
def instance
  warn "[DEPRECATION] `instance` is deprecated. Please use new"\
       " configuration access method."
  self
end
load!(args = {}) click to toggle source

load configuration from file

# File lib/eventhub/configuration.rb, line 64
def load!(args = {})
  # for better rspec testing
  @config_file = args[:config_file] if args[:config_file]
  @environment = args[:environment] if args[:environment]

  new_data = {}
  begin
    new_data = JSON.parse(File.read(@config_file), symbolize_names: true)
  rescue => e
    EventHub.logger.warn("Exception while loading configuration file: #{e}")
    EventHub.logger.info("Using default configuration values")
  end

  deep_merge!(@config_data, default_configuration)
  new_data = new_data[@environment.to_sym]
  deep_merge!(@config_data, new_data)
end
method_missing(name, *_args, &_block) click to toggle source
# File lib/eventhub/configuration.rb, line 92
def method_missing(name, *_args, &_block)
  @config_data[name.to_sym] ||
    fail(NoMethodError, "unknown configuration [#{name}]", caller)
end
name=(value) click to toggle source

set name of processor

# File lib/eventhub/configuration.rb, line 22
def name=(value)
  @name = value
end
parse_options(argv = ARGV) click to toggle source

parse options from argument list

# File lib/eventhub/configuration.rb, line 35
def parse_options(argv = ARGV)
  @config_file = File.join(Dir.getwd, "config", "#{@name}.json")

  OptionParser.new { |opts|
    note = "Define environment"
    opts.on("-e", "--environment ENVIRONMENT", note) do |environment|
      @environment = environment
    end

    opts.on("-d", "--detached", "Run processor detached as a daemon") do
      @detached = true
    end

    note = "Define configuration file"
    opts.on("-c", "--config CONFIG", note) do |config|
      @config_file = config
    end
  }.parse!(argv)

  true
rescue OptionParser::InvalidOption => e
  EventHub.logger.warn("Argument Parsing: #{e}")
  false
rescue OptionParser::MissingArgument => e
  EventHub.logger.warn("Argument Parsing: #{e}")
  false
end
reset() click to toggle source
# File lib/eventhub/configuration.rb, line 26
def reset
  @name = "undefined"
  @environment = "development"
  @detached = false
  @config_file = File.join(Dir.getwd, "config", "#{@name}.json")
  @config_data = {}
end
respond_to_missing?(name, include_private = false) click to toggle source
# File lib/eventhub/configuration.rb, line 97
def respond_to_missing?(name, include_private = false)
  return true if @config_data[name.to_sym]
  false
end