<commit_before>module Browsernizer

  class Router
    attr_reader :config

    def initialize(app, &block)
      @app = app
      @config = Config.new
      yield(@config)
    end

    def call(env)
      browser = get_browser(env)
      env["browsernizer"] = {
        "supported" => supported?(env),
        "browser" => browser.name.to_s,
        "version" => browser.version.to_s
      }
      redirect_request(env) || @app.call(@env)
    end

  private

    def redirect_request(env)
      return if path_excluded?(env)
      if !env["browsernizer"]["supported"]
        return redirect_to_specified if @config.get_location && !on_redirection_path?
      elsif on_redirection_path?
        return redirect_to_root
      end
    end

    def redirect_to_specified
      [307, {"Content-Type" => "text/plain", "Location" => @config.get_location}, []]
    end

    def redirect_to_root
      [303, {"Content-Type" => "text/plain", "Location" => "/"}, []]
    end

    def path_excluded?(env)
      @config.excluded? env["PATH_INFO"]
    end

    def on_redirection_path?
      @config.get_location && @config.get_location == @env["PATH_INFO"]
    end

    def get_raw_browser(env)
      ::Browser.new :ua => env["HTTP_USER_AGENT"]
    end

    def browser(env)
      raw_browser = get_raw_browser(env)
      Browser.new raw_browser.name.to_s, raw_browser.full_version.to_s
    end

    # supported by default
    def supported?(env)
      !@config.get_supported.any? do |requirement|
        supported = if requirement.respond_to?(:call)
          requirement.call(get_raw_browser(env))
        else
          browser.meets?(requirement)<commit_msg>Revert "Remove code smells from router"<commit_after>module Browsernizer

  class Router
    attr_reader :config

    def initialize(app, &block)
      @app = app
      @config = Config.new
      yield(@config)
    end

    def call(env)
      @env = env
      @env["browsernizer"] = {
        "supported" => true,
        "browser" => browser.name.to_s,
        "version" => browser.version.to_s
      }
      handle_request
    end

  private
    def handle_request
      @env["browsernizer"]["supported"] = false if unsupported?

      catch(:response) do
        if !path_excluded?
          if unsupported?
            if !on_redirection_path? && @config.get_location
              throw :response, redirect_to_specified
            end
          elsif on_redirection_path?
            throw :response, redirect_to_root
          end
        end
        propagate_request
      end
    end

    def propagate_request
      @app.call(@env)
    end

    def redirect_to_specified
      [307, {"Content-Type" => "text/plain", "Location" => @config.get_location}, []]
    end

    def redirect_to_root
      [303, {"Content-Type" => "text/plain", "Location" => "/"}, []]
    end

    def path_excluded?
      @config.excluded? @env["PATH_INFO"]
    end

    def on_redirection_path?
      @config.get_location && @config.get_location == @env["PATH_INFO"]
    end

    def raw_browser
      ::Browser.new :ua => @env["HTTP_USER_AGENT"]
    end

    def browser
      Browser.new raw_browser.name.to_s, raw_browser.full_version.to_s
    end

    # supported by default
    def unsupported?
      @config.get_supported.any? do |requirement|
        supported = if requirement.respond_to?(:call)
          requirement.call(raw_browser)
        else
          browser.meets?(requirement)
{'fraction_non_alphanumeric': 0.11295487627365357, 'fraction_numerical': 0.0034934497816593887, 'mean_word_length': 2.463709677419355, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '784157', 'n_tokens_mistral': 1099, 'n_tokens_neox': 1069, 'n_words': 220}