<commit_before>require 'base64'

class AsciicastDecorator < ApplicationDecorator
  decorates :asciicast

  THUMBNAIL_WIDTH = 20
  THUMBNAIL_HEIGHT = 10
  MAX_DELAY = 5.0

  def user
    @user ||= UserDecorator.new(asciicast.user)
  end

  def as_json(*args)
    data = model.as_json(*args)
    data['escaped_stdout_data'] = escaped_stdout_data
    data['stdout_timing_data'], saved_time = stdout_timing_data
    data['duration'] = data['duration'] - saved_time

    data
  end

  def escaped_stdout_data
    if data = stdout.read
      Base64.strict_encode64(data)
    else
      nil
    end
  end

  def stdout_timing_data
    saved_time = 0

    if file = stdout_timing.file<commit_msg>Use decorates_association instead of custom method<commit_after>require 'base64'

class AsciicastDecorator < ApplicationDecorator
  decorates :asciicast
  decorates_association :user

  THUMBNAIL_WIDTH = 20
  THUMBNAIL_HEIGHT = 10
  MAX_DELAY = 5.0

  def as_json(*args)
    data = model.as_json(*args)
    data['escaped_stdout_data'] = escaped_stdout_data
    data['stdout_timing_data'], saved_time = stdout_timing_data
    data['duration'] = data['duration'] - saved_time

    data
  end

  def escaped_stdout_data
    if data = stdout.read
      Base64.strict_encode64(data)
    else
      nil
    end
  end

  def stdout_timing_data
    saved_time = 0

    if file = stdout_timing.file
{'fraction_non_alphanumeric': 0.07625272331154684, 'fraction_numerical': 0.01888162672476398, 'mean_word_length': 3.547854785478548, 'pattern_counts': {'":': 0, '<': 5, '<?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': '7232653', 'n_tokens_mistral': 518, 'n_tokens_neox': 487, 'n_words': 99}