123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- # Class that reads the configuration file and validates and provides the values in it.
- class Config
- require 'psych'
- def initialize(args)
- # Ensure data types of arguments
- raise ArgumentError, 'Wrong argument format' unless args.is_a? Hash
- raise ArgumentError, 'Configuration file must be a string' unless args[:configuration_file].is_a? String
- @config_file = args[:configuration_file]
- end
- # Reads the configuration file
- def read
- # If the configuration file is read twice, there is
- # a logic error somewhere.
- # The program should not be executed any further.
- raise 'Configuration file already readed.' if @config
- # Ensure that the file exists and is readable.
- raise ArgumentError, 'Configuration file does not exist.' unless File.file? @config_file
- raise ArgumentError, 'Configuration file is not readable.' unless File.readable? @config_file
- # Read file and try to load it.
- file_cnt = File.read @config_file
- config = Psych.safe_load file_cnt
- raise 'Failed to parse configuration file' unless config.is_a? Hash
- # Check whether the configuration is valid - that is, whether the required
- # values have been specified and whether they are of the correct type.
- # Section Registry
- raise 'Configuration file has no key "registry"' unless config.key? 'registry'
- raise '"registry" must be of type hash' unless config['registry'].is_a? Hash
- ## Section Registry Directory
- raise 'Configuration file has no key "directory"' unless config['registry'].key? 'directory'
- raise '"directory" must be of type string' unless config['registry']['directory'].is_a? String
- ## Section Registry Reload Interval
- raise 'Configuration file has no key "reload_interval"' unless config['registry'].key? 'reload_interval'
- raise '"reload_interval" must be of type integer' unless config['registry']['reload_interval'].is_a? Integer
- # Section whoisd
- raise 'Configuration file has no key "whoisd"' unless config.key? 'whoisd'
- raise '"whoisd" must be of type hash' unless config['whoisd'].is_a? Hash
- ## Section whoisd host
- raise 'Configuration file has no key "host"' unless config['whoisd'].key? 'host'
- raise '"host" must be of type string' unless config['whoisd']['host'].is_a? String
- ## Section whoisd port
- raise 'Configuration file has no key "port"' unless config['whoisd'].key? 'port'
- raise '"port" must be of type integer' unless config['whoisd']['port'].is_a? Integer
- ## Section Logging
- raise 'Configuration file has no key "logging"' unless config.key? 'logging'
- raise '"logging" must be of type string' unless config['logging'].is_a? String
- # Removing accidental whitespaces
- config['registry']['directory'].strip!
- config['whoisd']['host'].strip!
- config['logging'].strip!
- config['logging'].downcase!
- config['registry']['directory'] = File.absolute_path(config['registry']['directory'])
- # After reading the configuration should
- # not be changed anymore
- config.freeze
- @config = config
- return self
- end
- # Returns the registry directory path.
- # If the configuration was not read, a RuntimeError is raised.
- #
- # @return [String]
- def registry_directory
- raise 'Configuration file not readed' unless @config
- return @config['registry']['directory']
- end
- # Returns the registry reload interval.
- # If the configuration was not read, a RuntimeError is raised.
- #
- # @return [String]
- def registry_reload_interval
- raise 'Configuration file not readed' unless @config
- return @config['registry']['reload_interval']
- end
- # Returns the whois endpoint as array.
- # If the configuration was not read, a RuntimeError is raised.
- #
- # @return [Array] [Host, Port]
- def whois_endpoint
- raise 'Configuration file not readed' unless @config
- return @config['whoisd']['host'], @config['whoisd']['port']
- end
- # Returns the logging level.
- # If the configuration was not read, a RuntimeError is raised.
- #
- # @return [String]
- def logging_level
- raise 'Configuration file not readed' unless @config
- return @config['logging']
- end
- end
|