<commit_before>require 'eventmachine'
require 'fsr/listener/response'

module FSR
  module Listener
    # Stupid name. I know.
    class Base < EventMachine::Protocols::HeaderAndContentProtocol
      class << self
        def hooks
          @hooks ||= []
        end

        def add_event_hook(event, &block)
          hooks << [event, block]
        end
      end

      attr_accessor :response

      def receive_request(header, content)
        @response = Response.new(header, content)

        if @response.event?
          find_and_invoke_event @response.content[:event_name]
        end
      end

      private
      def find_and_invoke_event(event_name)
        hooks = self.class.hooks.select {|name,_| name == event_name.to_sym}
        hooks.each {|_,block| instance_eval(&block)}
      end
    end
  end
end<commit_msg>don't do stuff twice<commit_after>require 'eventmachine'
require 'fsr/listener/response'

module FSR
  module Listener
    # Stupid name. I know.
    class Base < EventMachine::Protocols::HeaderAndContentProtocol
      class << self
        def hooks
          @hooks ||= []
        end

        def add_event_hook(event, &block)
          hooks << [event, block]
        end
      end

      attr_accessor :response

      def receive_request(header, content)
        @response = Response.new(header, content)

        if @response.event?
          find_and_invoke_event @response.content[:event_name]
        end
      end

      private
      def find_and_invoke_event(event_name)
        self.class.hooks.each do |name,block| 
          instance_eval(&block) if name == event_name.to_sym
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08528528528528528, 'fraction_numerical': 0.0, 'mean_word_length': 2.2475633528265107, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, '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': '738322', 'n_tokens_mistral': 493, 'n_tokens_neox': 488, 'n_words': 118}