<commit_before>require 'errordite'
require 'json'

class Errordite::Serializer
  SPECIAL_CONTEXT_ATTRIBUTES = ['MachineName', 'Url', 'UserAgent', 'Version']

  attr_reader :exception, :context

  def initialize(exception, context = {})
    @exception = exception
    @context = context
  end

  def as_json
    special_attributes.merge(
      "Token" => Errordite.api_token,
      "TimestampUtc" => Time.now.strftime("%Y-%m-%d %H:%M:%S"),
      "ExceptionInfo" => {
        "Source" => source,
        "MethodName" => method_name,
        "ExceptionType" => exception.class.name,
        "StackTrace" => stack_trace,
        "Data" => exception_data
      }
    )
  end

  def to_json
    JSON.dump(as_json)
  end

  private

  def special_attributes
    context.select {|k, v| SPECIAL_CONTEXT_ATTRIBUTES.include?(k) }
  end

  def exception_data
    context.reject {|k, v| SPECIAL_CONTEXT_ATTRIBUTES.include?(k) }
  end

  def timestamp
    Time.now.strftime("%Y-%m-%d %H:%M:%S")
  end

  def stack_trace
    exception.backtrace && exception.backtrace.join("\n")
  end

  def source
    split_first_line && split_first_line[1]
  end

  def method_name
    split_first_line && split_first_line[3]<commit_msg>Use Hash[] to rebuild hashes after select/reject (for ruby 1.8.7)<commit_after>require 'errordite'
require 'json'

class Errordite::Serializer
  SPECIAL_CONTEXT_ATTRIBUTES = ['MachineName', 'Url', 'UserAgent', 'Version']

  attr_reader :exception, :context

  def initialize(exception, context = {})
    @exception = exception
    @context = context
  end

  def as_json
    special_attributes.merge(
      "Token" => Errordite.api_token,
      "TimestampUtc" => Time.now.strftime("%Y-%m-%d %H:%M:%S"),
      "ExceptionInfo" => {
        "Source" => source,
        "MethodName" => method_name,
        "ExceptionType" => exception.class.name,
        "StackTrace" => stack_trace,
        "Data" => exception_data
      }
    )
  end

  def to_json
    JSON.dump(as_json)
  end

  private

  def special_attributes
    Hash[context.select {|k, v| SPECIAL_CONTEXT_ATTRIBUTES.include?(k) }]
  end

  def exception_data
    Hash[context.reject {|k, v| SPECIAL_CONTEXT_ATTRIBUTES.include?(k) }]
  end

  def timestamp
    Time.now.strftime("%Y-%m-%d %H:%M:%S")
  end

  def stack_trace
    exception.backtrace && exception.backtrace.join("\n")
  end

  def source
    split_first_line && split_first_line[1]
  end

  def method_name
    split_first_line && split_first_line[3]
{'fraction_non_alphanumeric': 0.12943548387096773, 'fraction_numerical': 0.00282258064516129, 'mean_word_length': 3.314782608695652, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '6583598', 'n_tokens_mistral': 850, 'n_tokens_neox': 827, 'n_words': 168}