class Mnemosyne::Configuration

Constants

DEFAULT_PORTS

Attributes

amqp[R]
application[R]
exchange[R]
hostname[R]
platform[R]
server[R]

Public Class Methods

new(config) click to toggle source
# File lib/mnemosyne/configuration.rb, line 16
def initialize(config)
  @platform    = config.fetch('platform', 'default').to_s.strip.freeze
  @application = config.fetch('application', nil).to_s.strip.freeze
  @enabled     = config.fetch('enabled', true)
  @exchange    = config.fetch('exchange', 'mnemosyne').to_s.freeze

  hostname  = config.fetch('hostname') { default_hostname }
  @hostname = hostname.to_s.strip.freeze

  server       = config.fetch('server', 'amqp://localhost')
  @amqp        = AMQ::Settings.configure(server).freeze
  @server      = make_amqp_uri(@amqp).to_s.freeze

  raise ArgumentError.new 'Platform is required' if platform.blank?

  if @platform =~ /[^a-zA-Z0-9\-]/
    raise ArgumentError.new \
      'Platform may only contain alphanumeric characters'
  end

  unless @platform =~ /\A[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*\z/
    raise ArgumentError.new \
      'Platform must start and end with a alphanumeric characters'
  end

  raise ArgumentError.new('Application is required') if application.blank?
  raise ArgumentError.new('Hostname is required') if hostname.blank?
end

Public Instance Methods

enabled?() click to toggle source
# File lib/mnemosyne/configuration.rb, line 45
def enabled?
  @enabled
end

Private Instance Methods

default_hostname() click to toggle source
# File lib/mnemosyne/configuration.rb, line 51
def default_hostname
  Socket.gethostname
end
make_amqp_uri(amqp) click to toggle source
# File lib/mnemosyne/configuration.rb, line 60
def make_amqp_uri(amqp)
  uri = URI('')

  uri.scheme = amqp[:scheme]
  uri.user = amqp[:user]
  uri.host = amqp[:host]
  uri.port = amqp[:port] if amqp[:port] != DEFAULT_PORTS[uri.scheme]
  uri.path = "/#{::CGI.escape(amqp[:vhost])}" if amqp[:vhost] != '/'

  uri
end