Sha256: 98a97320349f71cbf9822a49500f0f33832b77705f8ef6ac6d9154dc7839616f

Contents?: true

Size: 1.95 KB

Versions: 2

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

require 'socket'
require 'uri'
require 'cgi'

module Mnemosyne
  class Configuration
    attr_reader :application
    attr_reader :hostname
    attr_reader :platform
    attr_reader :amqp
    attr_reader :exchange
    attr_reader :logger
    attr_reader :server

    def initialize(config) # rubocop:disable AbcSize
      @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
      @logger      = config.fetch('logger') { Logger.new(STDOUT) }

      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

    def enabled?
      @enabled
    end

    private

    def default_hostname
      Socket.gethostname
    end

    def make_amqp_uri(amqp) # rubocop:disable AbcSize
      uri = URI('')

      uri.scheme = amqp[:scheme]
      uri.user = amqp[:user]
      uri.host = amqp[:host]
      uri.port = amqp[:port] if amqp[:port] != AMQ::URI::AMQP_PORTS[uri.scheme]
      uri.path = '/' + ::CGI.escape(amqp[:vhost]) if amqp[:vhost] != '/'

      uri
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mnemosyne-ruby-1.2.0 lib/mnemosyne/configuration.rb
mnemosyne-ruby-1.1.0 lib/mnemosyne/configuration.rb