config.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # Class that reads the configuration file and validates and provides the values in it.
  2. class Config
  3. require 'psych'
  4. def initialize(args)
  5. # Ensure data types of arguments
  6. raise ArgumentError, 'Wrong argument format' unless args.is_a? Hash
  7. raise ArgumentError, 'Configuration file must be a string' unless args[:configuration_file].is_a? String
  8. @config_file = args[:configuration_file]
  9. end
  10. # Reads the configuration file
  11. def read
  12. # If the configuration file is read twice, there is
  13. # a logic error somewhere.
  14. # The program should not be executed any further.
  15. raise 'Configuration file already readed.' if @config
  16. # Ensure that the file exists and is readable.
  17. raise ArgumentError, 'Configuration file does not exist.' unless File.file? @config_file
  18. raise ArgumentError, 'Configuration file is not readable.' unless File.readable? @config_file
  19. # Read file and try to load it.
  20. file_cnt = File.read @config_file
  21. config = Psych.safe_load file_cnt
  22. raise 'Failed to parse configuration file' unless config.is_a? Hash
  23. # Check whether the configuration is valid - that is, whether the required
  24. # values have been specified and whether they are of the correct type.
  25. # Section Registry
  26. raise 'Configuration file has no key "registry"' unless config.key? 'registry'
  27. raise '"registry" must be of type hash' unless config['registry'].is_a? Hash
  28. ## Section Registry Directory
  29. raise 'Configuration file has no key "directory"' unless config['registry'].key? 'directory'
  30. raise '"directory" must be of type string' unless config['registry']['directory'].is_a? String
  31. ## Section Registry Reload Interval
  32. raise 'Configuration file has no key "reload_interval"' unless config['registry'].key? 'reload_interval'
  33. raise '"reload_interval" must be of type integer' unless config['registry']['reload_interval'].is_a? Integer
  34. # Section whoisd
  35. raise 'Configuration file has no key "whoisd"' unless config.key? 'whoisd'
  36. raise '"whoisd" must be of type hash' unless config['whoisd'].is_a? Hash
  37. ## Section whoisd host
  38. raise 'Configuration file has no key "host"' unless config['whoisd'].key? 'host'
  39. raise '"host" must be of type string' unless config['whoisd']['host'].is_a? String
  40. ## Section whoisd port
  41. raise 'Configuration file has no key "port"' unless config['whoisd'].key? 'port'
  42. raise '"port" must be of type integer' unless config['whoisd']['port'].is_a? Integer
  43. ## Section Logging
  44. raise 'Configuration file has no key "logging"' unless config.key? 'logging'
  45. raise '"logging" must be of type string' unless config['logging'].is_a? String
  46. # Removing accidental whitespaces
  47. config['registry']['directory'].strip!
  48. config['whoisd']['host'].strip!
  49. config['logging'].strip!
  50. config['logging'].downcase!
  51. config['registry']['directory'] = File.absolute_path(config['registry']['directory'])
  52. # After reading the configuration should
  53. # not be changed anymore
  54. config.freeze
  55. @config = config
  56. return self
  57. end
  58. # Returns the registry directory path.
  59. # If the configuration was not read, a RuntimeError is raised.
  60. #
  61. # @return [String]
  62. def registry_directory
  63. raise 'Configuration file not readed' unless @config
  64. return @config['registry']['directory']
  65. end
  66. # Returns the registry reload interval.
  67. # If the configuration was not read, a RuntimeError is raised.
  68. #
  69. # @return [String]
  70. def registry_reload_interval
  71. raise 'Configuration file not readed' unless @config
  72. return @config['registry']['reload_interval']
  73. end
  74. # Returns the whois endpoint as array.
  75. # If the configuration was not read, a RuntimeError is raised.
  76. #
  77. # @return [Array] [Host, Port]
  78. def whois_endpoint
  79. raise 'Configuration file not readed' unless @config
  80. return @config['whoisd']['host'], @config['whoisd']['port']
  81. end
  82. # Returns the logging level.
  83. # If the configuration was not read, a RuntimeError is raised.
  84. #
  85. # @return [String]
  86. def logging_level
  87. raise 'Configuration file not readed' unless @config
  88. return @config['logging']
  89. end
  90. end