<commit_before>require "sensu/settings/validator"
require "multi_json"

module Sensu
  module Settings
    class Loader
      # @!attribute [r] warnings
      #   @return [Array] loader warnings.
      attr_reader :warnings

      def initialize
        @warnings = []
        @settings = {
          :checks => {},
          :filters => {},
          :mutators => {},
          :handlers => {}
        }
        @indifferent_access = false
        @loaded_files = []
      end

      # Access settings as an indifferent hash.
      #
      # @return [Hash] settings.
      def to_hash
        unless @indifferent_access
          indifferent_access!
        end
        @settings
      end

      # Retrieve the value object corresponding to a key, acting like
      # a Hash object.
      #
      # @param key [Object]
      # @return [Object] value for key.
      def [](key)
        to_hash[key]
      end

      # Retrieve definition objects for a setting category, or check
      # to see if a definition exists in the category.
      CATEGORIES.each do |category|
        define_method(category) do
          @settings[category].map do |name, details|
            details.merge(:name => name.to_s)
          end
        end

        method_name = category.to_s.chop + "_exists?"
        define_method(method_name.to_sym) do |name|
          @settings[category].has_key?(name.to_sym)
        end
      end

      # Load settings from the environment.
      # Loads: RABBITMQ_URL, REDIS_URL, REDISTOGO_URL, API_PORT, PORT
      def load_env
        if ENV["RABBITMQ_URL"]
          @settings[:rabbitmq] = ENV["RABBITMQ_URL"]
          warning(@settings[:rabbitmq], "using rabbitmq url environment variable")
        end
        ENV["REDIS_URL"] ||= ENV["REDISTOGO_URL"]
        if ENV["REDIS_URL"]
          @settings[:redis] = ENV["REDIS_URL"]
          warning(@settings[:redis], "using redis url environment variable")
        end
        ENV["API_PORT"] ||= ENV["PORT"]
        if ENV["API_PORT"]
          @settings[:api] ||= {}
          @settings[:api][:port] = ENV["API_PORT"].to_i
          warning(@settings[:api], "using api port environment variable")
        end
        @indifferent_access = false
      end

      # Load settings from a JSON file.
      #<commit_msg>codeclimate doesn't like assignment outside of methods<commit_after>require "sensu/settings/validator"
require "multi_json"

module Sensu
  module Settings
    class Loader
      # @!attribute [r] warnings
      #   @return [Array] loader warnings.
      attr_reader :warnings

      def initialize
        @warnings = []
        @settings = {
          :checks => {},
          :filters => {},
          :mutators => {},
          :handlers => {}
        }
        @indifferent_access = false
        @loaded_files = []
      end

      # Access settings as an indifferent hash.
      #
      # @return [Hash] settings.
      def to_hash
        unless @indifferent_access
          indifferent_access!
        end
        @settings
      end

      # Retrieve the value object corresponding to a key, acting like
      # a Hash object.
      #
      # @param key [Object]
      # @return [Object] value for key.
      def [](key)
        to_hash[key]
      end

      # Retrieve definition objects for a setting category, or check
      # to see if a definition exists in the category.
      CATEGORIES.each do |category|
        define_method(category) do
          @settings[category].map do |name, details|
            details.merge(:name => name.to_s)
          end
        end

        define_method((category.to_s.chop + "_exists?").to_sym) do |name|
          @settings[category].has_key?(name.to_sym)
        end
      end

      # Load settings from the environment.
      # Loads: RABBITMQ_URL, REDIS_URL, REDISTOGO_URL, API_PORT, PORT
      def load_env
        if ENV["RABBITMQ_URL"]
          @settings[:rabbitmq] = ENV["RABBITMQ_URL"]
          warning(@settings[:rabbitmq], "using rabbitmq url environment variable")
        end
        ENV["REDIS_URL"] ||= ENV["REDISTOGO_URL"]
        if ENV["REDIS_URL"]
          @settings[:redis] = ENV["REDIS_URL"]
          warning(@settings[:redis], "using redis url environment variable")
        end
        ENV["API_PORT"] ||= ENV["PORT"]
        if ENV["API_PORT"]
          @settings[:api] ||= {}
          @settings[:api][:port] = ENV["API_PORT"].to_i
          warning(@settings[:api], "using api port environment variable")
        end
        @indifferent_access = false
      end

      # Load settings from a JSON file.
      #
{'fraction_non_alphanumeric': 0.10735326205542221, 'fraction_numerical': 0.0, 'mean_word_length': 2.02774108322325, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '333134', 'n_tokens_mistral': 1362, 'n_tokens_neox': 1324, 'n_words': 350}