<commit_before>require 'rack/utils'

module GaEvents
  class Middleware
    def initialize(app)
      @app = app
    end
    def call(env)
      # Handle events stored in flash
      # Parts borrowed from Rails:
      # https://github.com/rails/rails/blob/v3.2.14/actionpack/lib/action_dispatch/middleware/flash.rb
      flash = env['rack.session'] && env['rack.session']['flash'] && env['rack.session']['flash']['flashes']
      GaEvents::List.init(flash && flash['ga_events'])

      status, headers, response = @app.call(env)

      headers = Rack::Utils::HeaderHash.new(headers)
      if GaEvents::List.present?
        request = Rack::Request.new(env)

        # Can outgrow, headers might get too big
        serialized = GaEvents::List.to_s

        if request.xhr?
          # AJAX request
          headers['X-GA-Events'] = serialized<commit_msg>switch for extracting the flash for both versions rails 3 & 4<commit_after>require 'rack/utils'

module GaEvents
  class Middleware
    def initialize(app)
      @app = app
    end
    def call(env)
      # Handle events stored in flash
      # Parts borrowed from Rails:
      # https://github.com/rails/rails/blob/v3.2.14/actionpack/lib/action_dispatch/middleware/flash.rb
      if Rails::VERSION::MAJOR == 3
        flash = env['rack.session'] && env['rack.session']['flash']
      else
        flash = env['rack.session'] && env['rack.session']['flash'] && env['rack.session']['flash']['flashes']
      end
      GaEvents::List.init(flash && flash['ga_events'])

      status, headers, response = @app.call(env)

      headers = Rack::Utils::HeaderHash.new(headers)
      if GaEvents::List.present?
        request = Rack::Request.new(env)

        # Can outgrow, headers might get too big
        serialized = GaEvents::List.to_s

        if request.xhr?
          # AJAX request
          headers['X-GA-Events'] = serialized
{'fraction_non_alphanumeric': 0.13580901856763924, 'fraction_numerical': 0.005835543766578249, 'mean_word_length': 2.81010101010101, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '7120639', 'n_tokens_mistral': 591, 'n_tokens_neox': 554, 'n_words': 125}