<commit_before>require 'require_all'
require_relative 'Resource/Resource'
require_relative 'exceptions'

module Sandbox

  class Application

    DEFAULT_SETTINGS = {
      :autorequire => {
        :root => "#{File.dirname __FILE__}/../..",
        :directories => ["resources"]
      },
      :resource_methods => ["get", "post", "put", "patch", "delete", "options"]
    }

    def initialize(settings = {})
      @settings = DEFAULT_SETTINGS.merge(settings)
      @settings[:resource_methods].map! { |m| m.to_sym }

      puts @settings

      @settings[:autorequire][:directories].each do |dir|
        require_all "#{@settings[:autorequire][:root]}/#{dir}"
      end
    end

    def handle_request(request)
      begin
        request[:resource] = request[:resource].to_sym
        request[:method] = request[:method].downcase.to_sym

        # Get the requested resource class
        handler = Application::get_resource request[:resource]

        if @settings[:resource_methods].include?(request[:method]) &&
            handler.method_defined?(request[:method])
          return handler.public_send(method, request)
        else
          raise Sandbox::ResourceMethodException,
              "The %s resource does not respond to %s requests" % [
                request[:resource], request[:method]
              ]
        end
      rescue Exception => e
        raise Sandbox::SandboxException, "An unexpected error occurred"
      end
    end

    private
    def Application::get_resource(resource)
      if Module.const_defined(resource)
        it = Object.const_get(resource)

        if it.is_a(Class) &&
            it.class != Sandbox::Resource &&
            it.is_a?(Sandbox::Resource.to_sym)<commit_msg>DO not rescue exceptions from Application. Fix typo.<commit_after>require 'require_all'
require_relative 'Resource/Resource'
require_relative 'exceptions'

module Sandbox

  class Application

    DEFAULT_SETTINGS = {
      :autorequire => {
        :root => "#{File.dirname __FILE__}/../..",
        :directories => ["resources"]
      },
      :resource_methods => ["get", "post", "put", "patch", "delete", "options"]
    }

    def initialize(settings = {})
      @settings = DEFAULT_SETTINGS.merge(settings)
      @settings[:resource_methods].map! { |m| m.to_sym }

      puts @settings

      @settings[:autorequire][:directories].each do |dir|
        require_all "#{@settings[:autorequire][:root]}/#{dir}"
      end
    end

    def handle_request(request)
      request[:resource] = request[:resource].to_sym
      request[:method] = request[:method].downcase.to_sym

      # Get the requested resource class
      handler = Application::get_resource request[:resource]

      if @settings[:resource_methods].include?(request[:method]) &&
          handler.method_defined?(request[:method])
        return handler.public_send(method, request)
      else
        raise Sandbox::ResourceMethodException,
            "The %s resource does not respond to %s requests" % [
              request[:resource], request[:method]
            ]
      end
    end

    private
    def Application::get_resource(resource)
      if Module.const_defined?(resource)
        it = Object.const_get(resource)

        if it.is_a(Class) &&
            it.class != Sandbox::Resource &&
            it.is_a?(Sandbox::Resource.to_sym)
{'fraction_non_alphanumeric': 0.12799043062200957, 'fraction_numerical': 0.0, 'mean_word_length': 2.766891891891892, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, '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': '4907796', 'n_tokens_mistral': 932, 'n_tokens_neox': 924, 'n_words': 193}