<commit_before>require "hiera/backend/aws/elasticache"
require "hiera/backend/aws/rds"

class Hiera
  module Backend
    # Hiera AWS backend
    class Aws_backend # rubocop:disable ClassAndModuleCamelCase
      def initialize
        begin
          require "aws-sdk"
        rescue LoadError
          require "rubygems"
          require "aws-sdk"
        end

        setup_aws_credentials

        Hiera.debug("AWS backend initialized")
      end

      def setup_aws_credentials
        if Config[:aws] && Config[:aws][:access_key_id] && Config[:aws][:secret_access_key]
          Hiera.debug("Using AWS credentials from backend configuration")

          AWS.config(
            :access_key_id     => Config[:aws][:access_key_id],
            :secret_access_key => Config[:aws][:secret_access_key]
          )
        else
          Hiera.debug("Using AWS credentials from environment or IAM role")
        end
      end

      def lookup(key, scope, order_override, resolution_type) # rubocop:disable CyclomaticComplexity
        Backend.datasources(scope, order_override) do |elem|
          elements = elem.split "/"
          next unless elements[0] == "aws"

          service = elements[1]

          service_class = case service
                          when "elasticache"
                            Hiera::Backend::Aws::ElastiCache.new
                          when "rds"
                            Hiera::Backend::Aws::RDS.new
                          end
          next if service_class.nil?

          value = service_class.lookup(key, scope)
          next if value.nil?

          # this only supports resolution_type => :priority at the moment.
          # TODO: implement :array and :hash merging
          value = Backend.parse_answer(value, scope)
          return value unless value.nil?
        end
        nil
      end
    end
  end
end
<commit_msg>Refactor backend lookup to be more readable<commit_after>require "hiera/backend/aws/elasticache"
require "hiera/backend/aws/rds"

class Hiera
  module Backend
    # Hiera AWS backend
    class Aws_backend # rubocop:disable ClassAndModuleCamelCase
      def initialize
        begin
          require "aws-sdk"
        rescue LoadError
          require "rubygems"
          require "aws-sdk"
        end

        setup_aws_credentials

        Hiera.debug("AWS backend initialized")
      end

      def lookup(key, scope, order_override, resolution_type)
        answer = nil

        Hiera.debug("Looking up #{key} in AWS backend")

        Backend.datasources(scope, order_override) do |source|
          Hiera.debug("Looking for data source #{source}")

          service_class = find_service_class(source)
          next unless service_class

          value = service_class.lookup(key, scope)
          next unless value

          answer = Backend.parse_answer(value, scope)
          break if answer
        end
        answer
      end

      private

      def setup_aws_credentials
        if Config[:aws] && Config[:aws][:access_key_id] && Config[:aws][:secret_access_key]
          Hiera.debug("Using AWS credentials from backend configuration")

          AWS.config(
            :access_key_id     => Config[:aws][:access_key_id],
            :secret_access_key => Config[:aws][:secret_access_key]
          )
        else
          Hiera.debug("Using AWS credentials from environment or IAM role")
        end
      end

      def find_service_class(source)
        elements = source.split "/"
        return nil unless elements[0] == "aws"

        case elements[1]
        when "elasticache"
          Hiera::Backend::Aws::ElastiCache.new
        when "rds"
          Hiera::Backend::Aws::RDS.new
        else
          nil
        end
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.08105235184693065, 'fraction_numerical': 0.0010629816635663034, 'mean_word_length': 1.9661150512214343, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '1664010', 'n_tokens_mistral': 1046, 'n_tokens_neox': 1021, 'n_words': 258}