<commit_before>module Kwipper
  class Application
    include RendersViews
    include ControllerHelpers

    attr_reader :request, :response, :action

    def respond_to(request)
      @request = request
      @response = Response.new request

      begin
        start_time = Time.now.to_f
        process!

        log.debug "#{"Processed #{request.info}".blue} in #{sprintf '%.8f', Time.now.to_f - start_time}s"
      rescue Kwipper::AuthenticationRequired
        redirect '/'
        log.debug "401 Not Authorized".yellow
      rescue Kwipper::NotFoundError
        render_not_found
        log.warn @response.info.yellow
      rescue => e
        render_error e
        log.fatal "#{@response.info.red}\n#{verbose_error(e)}"
      end
      @response
    end

    def process!
      if Controller::ROUTES.key? request.route_key
        response.set_status :ok
        response.content_type = 'text/html'

        controller_class, @action = Controller::ROUTES[request.route_key]
        controller = controller_class.new request, response

        if controller.respond_to? @action
          @view = controller.process @action
          response.body = render :layout
        else
          raise Kwipper::NotFoundError, "#{self} does not know #{@action}"
        end
      elsif (file_name = public_file_request?)
        response.set_status :ok
        response.content_type = get_content_type file_name
        response.body = File.read file_name
      else
        raise Kwipper::NotFoundError
      end
    end

    private

    def public_file_request?
      file = File.join(Kwipper::ROOT, 'public', request.path)
      File.exists?(file) && file
    end

    def get_content_type(file_name)
      if (mime = MIME::Types.of(file_name).first)
        mime.content_type
      else
        log.warn "Unknown content type for file: #{file_name}"
        'text/plain'
      end
    end
<commit_msg>Update application.rb<commit_after>module Kwipper
  class Application
    include RendersViews
    include ControllerHelpers

    attr_reader :request, :response, :action

    def respond_to(request)
      @request = request
      @response = Response.new request

      begin
        start_time = Time.now.to_f
        process!

        log.debug "#{"Processed #{request.info}".blue} in #{sprintf '%.8f', Time.now.to_f - start_time}s"
      rescue Kwipper::AuthenticationRequired
        redirect '/'
        log.debug "401 Not Authorized".yellow
      rescue Kwipper::NotFoundError
        render_not_found
        log.warn @response.info.yellow
      rescue => e
        render_error e
        log.fatal "#{@response.info.red}\n#{verbose_error(e)}"
      end
      @response
    end

    def process!
      if Controller::ROUTES.key? request.route_key
        response.set_status :ok
        response.content_type = 'text/html'

        controller_class, @action = Controller::ROUTES[request.route_key]
        controller = controller_class.new request, response

        if controller.respond_to? @action
          @view = controller.process @action
          response.body = render :layout
        else
          raise Kwipper::NotFoundError, "#{self} does not know #{@action}"
        end
      elsif (file_name = get_file_name)
        response.set_status :ok
        response.content_type = get_content_type file_name
        response.body = File.read file_name
      else
        raise Kwipper::NotFoundError
      end
    end

    private

    def get_file_name
      file = File.join(Kwipper::ROOT, 'public', request.path)
      File.exists?(file) && file
    end

    def get_content_type(file_name)
      if (mime = MIME::Types.of(file_name).first)
        mime.content_type
      else
        log.warn "Unknown content type for file: #{file_name}"
        'text/plain'
      end
    end

{'fraction_non_alphanumeric': 0.09164915966386554, 'fraction_numerical': 0.0021008403361344537, 'mean_word_length': 2.367816091954023, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '5869868', 'n_tokens_mistral': 1121, 'n_tokens_neox': 1100, 'n_words': 259}