Sha256: d00834a4cb314e767102fa8b9df80b9a56708374a5d8200e3dd0537aac6432b4

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true
require 'socket'
require 'uri'
require 'cgi'

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

    # rubocop:disable Metrics/AbcSize
    def initialize(config)
      @platform    = config.fetch('platform', 'default').freeze
      @application = config.fetch('application').freeze
      @enabled     = config.fetch('enabled', true)
      @hostname    = config.fetch('hostname') { default_hostname }.freeze
      @exchange    = config.fetch('exchange', 'mnemosyne').freeze
      @logger      = config.fetch('logger') { Logger.new($stdout) }

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

      raise 'Application must be configured' unless application.present?
      raise 'Hostname must be configured' unless hostname.present?
    end

    def enabled?
      @enabled
    end

    private

    def default_hostname
      Socket.gethostbyname(Socket.gethostname).first
    end

    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] != AMQ::URI::AMQP_PORTS[uri.scheme]
      uri.path = '/' + ::CGI.escape(amqp[:vhost]) if amqp[:vhost] != '/'

      uri
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mnemosyne-ruby-0.2.0 lib/mnemosyne/config.rb