class Towel::Configuration
Loads and merges configuration settings for Towel
.
Constants
- FILENAMES
Acceptable filenames in which to define
Towel
settings. These will be read in the defined order.
Public Class Methods
new(hash = {})
click to toggle source
Creates a new configuration from a Hash.
# File lib/towel/configuration.rb, line 60 def initialize(hash = {}) @config = { "auth" => { "account" => nil, "api_key" => nil }, "collector" => { "address" => "towel.dev", "organization" => nil, "project" => nil }, "labels" => {} } @config.merge!(hash) @config.freeze end
read()
click to toggle source
Read the configuration from files and environment variables.
# File lib/towel/configuration.rb, line 9 def self.read # Load configuration from files, starting in the current working directory # and working upwards in the filesystem hierarchy until no configuration # files are found. files = [] directory = Dir.pwd while true FILENAMES.each do |filename| possible_file = File.join(directory, filename) files << possible_file if File.exist?(possible_file) end parent = File.expand_path("..", directory) if directory == parent break else directory = parent end end # Combine all discovered files into one configuration. config = files .reverse .map { |file| TOML.load_file(file) } .inject({}) do |acc, toml| acc.merge(toml) do |key, old, new| case old when Hash then old.merge(new) else new end end end # Override with environment variables where appropriate. override = ->(env, section, key) do if ENV.key?(env) section_values = config[section] || {} section_values[key] = ENV[env] config[section] = section_values end end override.call("TOWEL_API_KEY", "auth", "api_key") override.call("TOWEL_ACCOUNT", "auth", "account") override.call("TOWEL_ADDRESS", "collector", "address") new(config) end
Public Instance Methods
[](key)
click to toggle source
Retrieves a configuration section (“auth”, “collector”, “labels”, etc).
# File lib/towel/configuration.rb, line 78 def [](key) @config[key] end