<commit_before>module Gridhook
  class Event

    def self.process(body)
      parser = Yajl::Parser.new
      parser.on_parse_complete = proc do |event|
        if event.is_a?(Array)
          process_events event
        else
          process_event event
        end
      end
      parser.parse(body)
    end

    attr_reader :attributes

    def initialize(attributes)
      @attributes = attributes.with_indifferent_access
    end

    def name
      attributes[:event]
    end

    def timestamp
      Time.at (attributes[:timestamp] || Time.now).to_i
    end

    class << self
      private

      def process_events(events)
        events.each { |e| process_event e }
      end

      def process_event(event)
        processor = Gridhook.config.event_processor
        if processor.respond_to?(:call)
          processor.call Event.new(event)
        else
          raise InvalidEventProcessor, "Your event processor is nil or "\
            "does not response to a `call' method."
        end
      end
    end

  end
end<commit_msg>add Event#[] for accessing attributes<commit_after>module Gridhook
  class Event

    def self.process(body)
      parser = Yajl::Parser.new
      parser.on_parse_complete = proc do |event|
        if event.is_a?(Array)
          process_events event
        else
          process_event event
        end
      end
      parser.parse(body)
    end

    attr_reader :attributes

    def initialize(attributes)
      @attributes = attributes.with_indifferent_access
    end

    def name
      attributes[:event]
    end

    def timestamp
      Time.at (attributes[:timestamp] || Time.now).to_i
    end

    def [](key)
      attributes[key]
    end

    class << self
      private

      def process_events(events)
        events.each { |e| process_event e }
      end

      def process_event(event)
        processor = Gridhook.config.event_processor
        if processor.respond_to?(:call)
          processor.call Event.new(event)
        else
          raise InvalidEventProcessor, "Your event processor is nil or "\
            "does not response to a `call' method."
        end
      end
    end

  end
end
{'fraction_non_alphanumeric': 0.07080055529847293, 'fraction_numerical': 0.0, 'mean_word_length': 1.994459833795014, 'pattern_counts': {'":': 0, '<': 7, '<?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': '5792532', 'n_tokens_mistral': 611, 'n_tokens_neox': 592, 'n_words': 165}