<commit_before>class Mumukit::ResponseBuilder

  def add_test_results(r)
    @response = base_response(r)
  end

  def add_expectation_results(r)
    @response.merge!(expectationResults: r) if r.present?
  end


  def add_feedback(f)
    @response.merge!(feedback: f) if f.present?
  end

  def build
    @response
  end

  private

  def base_response(test_results)
    if test_results.size == 1 && test_results[0].is_a?(Array)
      structured_base_response(test_results)
    elsif test_results.size == 2 && test_results[0].is_a?(String)
      unstructured_base_response(test_results)
    else
      raise "Invalid test results format: #{test_results}. You must either return [results_array] or [results_string, status]"
    end
  end

  def structured_base_response(test_results)
    {testResults: test_results[0].map { |title, status, result|
      {title: title,
       status: status,
       result: result} }}
  end

  def unstructured_base_response(test_results)
    {exit: test_results[1],
     out: test_results[0]}
  end

end<commit_msg>Extracted methods<commit_after>class Mumukit::ResponseBuilder

  def add_test_results(r)
    @response = base_response(r)
  end

  def add_expectation_results(r)
    @response.merge!(expectationResults: r) if r.present?
  end


  def add_feedback(f)
    @response.merge!(feedback: f) if f.present?
  end

  def build
    @response
  end

  private

  def base_response(test_results)
    if structured_test_result?(test_results)
      structured_base_response(test_results)
    elsif unstructured_test_result?(test_results)
      unstructured_base_response(test_results)
    else
      raise "Invalid test results format: #{test_results}. You must either return [results_array] or [results_string, status]"
    end
  end

  def structured_test_result?(test_results)
    test_results.size == 1 && test_results[0].is_a?(Array)
  end

  def unstructured_test_result?(test_results)
    test_results.size == 2 && test_results[0].is_a?(String)
  end

  def structured_base_response(test_results)
    {testResults: test_results[0].map { |title, status, result|
      {title: title,
       status: status,
       result: result} }}
  end

  def unstructured_base_response(test_results)
    {exit: test_results[1],
     out: test_results[0]}
  end

end
{'fraction_non_alphanumeric': 0.09606986899563319, 'fraction_numerical': 0.00611353711790393, 'mean_word_length': 3.9804347826086954, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3090371', 'n_tokens_mistral': 769, 'n_tokens_neox': 740, 'n_words': 162}