lib/landable/configuration.rb in landable-1.13.2 vs lib/landable/configuration.rb in landable-1.14.0

- old
+ new

@@ -1,7 +1,9 @@ +require 'figgy' + module Landable - class Configuration + class Configuration < HashWithIndifferentAccess attr_accessor :api_url, :public_url, :amqp_configuration, :sitemap_host attr_writer :api_namespace, :public_namespace attr_writer :api_host, :public_host attr_writer :categories attr_writer :screenshots_enabled @@ -11,10 +13,28 @@ attr_writer :publicist_url, :audit_flags attr_writer :blank_user_agent_string, :untracked_paths attr_writer :dnt_enabled, :amqp_event_mapping, :amqp_site_segment attr_writer :amqp_service_enabled, :amqp_messaging_service + def initialize(config_path = nil) + begin + # let's keep this feature optional. Not all apps + # will be using external configs for landable + app_config = Figgy.build do |config_data| + config_data.root = config_path + + config_data.define_overlay :default, nil + config_data.define_overlay(:environment) { Rails.env } + end + + # map our new configs into our local object. + config_keys(config_path).each do |key| + self[key] = app_config[key] unless app_config[key].nil? + end + end unless config_path.nil? + end + def amqp_configuration @amqp_configuration ||= {} end def authenticators @@ -168,9 +188,46 @@ attr_accessor :autorun attr_accessor :browserstack_username, :browserstack_password def initialize @autorun = true + end + end + + private + + DOTFILE_MATCHER_REGEXP = /^\.[[[:alnum:]]\.]*$/ + EXPECTED_FILETYPES = ['yml', 'yaml', 'json'] + EXPECTED_FILETYPES_REGEXP = /\.(#{ EXPECTED_FILETYPES.join('|') })\z/ + + def config_keys(base_path) + files = Dir.entries(base_path) + keys = [] + + files = remove_dotfiles(files) + + files.each do |filename| + filename = File.join(base_path, filename) + new_key = filename_to_key(filename) + + if File.file?(filename) + keys.push(new_key) + elsif File.directory?(filename) && new_key == Rails.env + # only folders matching our environment! + keys = keys.concat(config_keys(filename)) + end + end + + keys.uniq + end + + def filename_to_key(filename) + File.basename(filename).sub(EXPECTED_FILETYPES_REGEXP, '') + end + + def remove_dotfiles(filelist) + filelist.delete_if do |filename| + !DOTFILE_MATCHER_REGEXP.match(File.basename(filename)).nil? end end end end