module Hermaeus::Config
Public: Provides configuration services for Hermaeus
Constants
Public Class Methods
info()
click to toggle source
Public: Accessor for the loaded and parsed information.
Returns nil if the config file has yet to be processed.
# File lib/hermaeus/config.rb, line 20 def self.info @info end
load()
click to toggle source
Public: Load a configuration file into memory
Returns the configuration file represented as a Hash with Symbol keys
# File lib/hermaeus/config.rb, line 27 def self.load @info = Tomlrb.load_file FILE, symbolize_keys: true end
validate!()
click to toggle source
Public: Performs validation checks on a configuration structure
Returns true if the configuration argument is valid
Raises a ConfigurationError
if the configuration is invalid, with an error message describing the failure.
# File lib/hermaeus/config.rb, line 37 def self.validate! # Validate the [client] section. raise ConfigurationError.new(<<-EOS) unless @info.has_key? :client Hermaeus’ configuration file must contain a [client] section. EOS # Validate the [client] section’s type field. unless @info[:client].has_key?(:type) && ALLOWED_TYPES.include?(@info[:client][:type]) raise ConfigurationError.new <<-EOS Hermaeus’ [client] section must include a type key whose value is one of: #{ALLOWED_TYPES.join(", ")}. [client] type = "one of the listed types" EOS end # Validate the [client] section’s id and secret fields. unless @info[:client].has_key?(:id) && @info[:client].has_key?(:secret) raise ConfigurationError.new <<-EOS Hermaeus’ [client] section must include keys for the ID and secret provided by reddit for your application. [client] id = "an ID from reddit" secret = "a secret from reddit" EOS end # Validate the [client] section’s username and password fields, if needed. if @info[:client][:type] == "script" client = @info[:client] unless client.has_key?(:username) && client.has_key?(:password) raise ConfigurationError.new <<-EOS When configured for `type = "script"`, Hermaeus’ [client] section must include keys for the reddit account username and password as which it will work. [client] username = "a_reddit_username" password = "hunter2" EOS end end # Validate the [archive] section. raise ConfigurationError.new(<<-EOS) unless @info.has_key? :archive Hermaeus’ configuration file must include an [archive] section to govern the storage of downloaded posts. EOS raise ConfigurationError.new(<<-EOS) unless @info[:archive].has_key? :path Hermaeus’ [archive] section must include a path field containing a relative or absolute path in which to store the downloaded posts. [archive] path = "./archive" # path = "/tmp/teslore/archive" EOS # Validate the [index] section. raise ConfigurationError.new(<<-EOS) unless @info.has_key? :index Hermaeus’ configuration file must include and [index] section to govern the processing of the subreddit’s index page. EOS raise ConfigurationError.new(<<-EOS) unless @info[:index].has_key? :path Hermaeus’ [index] section must include a path field containing the reddit page at which the index resides. [index] path = "/r/teslore/wiki/archive" EOS true end