<commit_before>module RorVsWild
  module Local
    class Middleware
      include ERB::Util

      attr_reader :app, :config

      def initialize(app, config)
        @app, @config = app, config
      end

      def call(env)
        status, headers, response = app.call(env)
        if headers["Content-Type"] && headers["Content-Type"].include?("text/html")
          inject_html_into(response)
          [status, headers, response]
        else
          [status, headers, response]
        end
      end

      def inject_html_into(response)
        html = response.instance_variable_get(:@response).body
        if index = html.index("</body>")
          html.insert(index, html_markup(RorVsWild.agent.queue.requests))
          response.instance_variable_get(:@response).body = html
        end
        if index = html.index("</head>")
          html.insert(index, "<style type='text/css'> #{concatenate_stylesheet}</style>")
          response.instance_variable_get(:@response).body = html
        end
      end

      DIRECTORY = File.expand_path(File.dirname(__FILE__))
      JS_FILES = ["mustache.js", "barber.js", "local.js"]
      CSS_FILES = ["local.css"]

      def html_markup(data)
        html = File.read(File.join(DIRECTORY, "local.html"))
        html % {data: html_escape(data.to_json), javascript_source: concatenate_javascript}
      end
<commit_msg>Do not inject HTML into response when it has been gziped.<commit_after>module RorVsWild
  module Local
    class Middleware
      include ERB::Util

      attr_reader :app, :config

      def initialize(app, config)
        @app, @config = app, config
      end

      def call(env)
        status, headers, rack_body = app.call(env)
        if response = rack_body.instance_variable_get(:@response)
          if headers["Content-Type"] && headers["Content-Type"].include?("text/html")
            inject_html_into(response)
          end
        end
        [status, headers, rack_body]
      end

      def inject_html_into(response)
        html = response.body
        if index = html.index("</body>")
          html.insert(index, html_markup(RorVsWild.agent.queue.requests))
          response.body = html
        end
        if index = html.index("</head>")
          html.insert(index, "<style type='text/css'> #{concatenate_stylesheet}</style>")
          response.body = html
        end
      end

      DIRECTORY = File.expand_path(File.dirname(__FILE__))
      JS_FILES = ["mustache.js", "barber.js", "local.js"]
      CSS_FILES = ["local.css"]

      def html_markup(data)
        html = File.read(File.join(DIRECTORY, "local.html"))
        html % {data: html_escape(data.to_json), javascript_source: concatenate_javascript}
      end

{'fraction_non_alphanumeric': 0.12045538009548293, 'fraction_numerical': 0.0, 'mean_word_length': 2.514838709677419, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '6867169', 'n_tokens_mistral': 823, 'n_tokens_neox': 796, 'n_words': 170}