<commit_before>require "rubygems"
require "bundler"
Bundler.require(:default)

require "CGI"

module Webhookr
  module Mandrill
    class Adapter
      SERVICE_NAME = "mandrill"
      EVENT_KEY = "event"
      PAYLOAD_KEY = "msg"
      
      include Webhookr::Services::Adapter::Base

      def self.process(raw_response)
        new.process(raw_response)
      end

      def process(raw_response)
        Array.wrap(parse(raw_response)).collect do |p|
          Webhookr::AdapterResponse.new(
            SERVICE_NAME, p.fetch(EVENT_KEY), RecursiveOpenStruct.new(p)
          ) if assert_valid_packet(p)
        end
      end

      private

      def parse(raw_response)
        begin
          assert_valid_packets(
            ActiveSupport::JSON.decode(
              CGI.unescape(raw_response).gsub(/mandrill_events=/,"")
            )
          )
        rescue Exception => e
          raise InvalidPayloadError.new(e)
        end
      end

      def assert_valid_packets(parsed_responses)
        raise(
          Webhookr::InvalidPayloadError, "Malformed response |#{parsed_responses.inspect}|"
        ) unless parsed_responses.is_a?(Array) && parsed_responses.first.is_a?(Hash)
        parsed_responses
      end

      def assert_valid_packet(packet)
        raise(Webhookr::InvalidPayloadError, "Unknown event #{packet[EVENT_KEY]}") unless packet[EVENT_KEY]
        raise(Webhookr::InvalidPayloadError, "No msg in the response") unless packet[PAYLOAD_KEY]
        true
      end

    end
  end
end
<commit_msg>Recursive over the parsed response<commit_after>require "rubygems"
require "bundler"
Bundler.require(:default)

require "CGI"

module Webhookr
  module Mandrill
    class Adapter
      SERVICE_NAME = "mandrill"
      EVENT_KEY = "event"
      PAYLOAD_KEY = "msg"
      
      include Webhookr::Services::Adapter::Base

      def self.process(raw_response)
        new.process(raw_response)
      end

      def process(raw_response)
        Array.wrap(parse(raw_response)).collect do |p|
          Webhookr::AdapterResponse.new(
            SERVICE_NAME,
            p.fetch(EVENT_KEY),
            RecursiveOpenStruct.new(p, :recurse_over_arrays => true)
          ) if assert_valid_packet(p)
        end
      end

      private

      def parse(raw_response)
        begin
          assert_valid_packets(
            ActiveSupport::JSON.decode(
              CGI.unescape(raw_response).gsub(/mandrill_events=/,"")
            )
          )
        rescue Exception => e
          raise InvalidPayloadError.new(e)
        end
      end

      def assert_valid_packets(parsed_responses)
        raise(
          Webhookr::InvalidPayloadError, "Malformed response |#{parsed_responses.inspect}|"
        ) unless parsed_responses.is_a?(Array) && parsed_responses.first.is_a?(Hash)
        parsed_responses
      end

      def assert_valid_packet(packet)
        raise(Webhookr::InvalidPayloadError, "Unknown event #{packet[EVENT_KEY]}") unless packet[EVENT_KEY]
        raise(Webhookr::InvalidPayloadError, "No msg in the response") unless packet[PAYLOAD_KEY]
        true
      end

    end
  end
end

{'fraction_non_alphanumeric': 0.08820709491850431, 'fraction_numerical': 0.0, 'mean_word_length': 2.4623893805309733, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '2232159', 'n_tokens_mistral': 947, 'n_tokens_neox': 939, 'n_words': 176}