<commit_before># Run API used by the morph command-line client
class ApiController < ApplicationController
  include ActionController::Live

  # The run_remote method will be secured with a key so shouldn't need csrf
  # token authentication
  skip_before_filter :verify_authenticity_token, only: [:run_remote]
  before_filter :authenticate_api_key

  # Receive code from a remote client, run it and return the result.
  # This will be a long running request
  # TODO: Document this API
  def run_remote
    response.headers['Content-Type'] = 'text/event-stream'

    if !current_user.ability.can? :create, Run
      stream_message('internalerr', "You currently can't start a scraper run." \
                                   ' See https://morph.io for more details')
    else
      run = Run.create(queued_at: Time.now, auto: false, owner: current_user)
      # TODO: Shouldn't need to untar here because it just gets retarred
      Archive::Tar::Minitar.unpack(params[:code].tempfile, run.repo_path)

      Morph::Runner.new(run).go { |s, text| stream_message(s, text) }
    end
  ensure
    response.stream.close

    # Cleanup run
    FileUtils.rm_rf(run.data_path)
    FileUtils.rm_rf(run.repo_path)
  end

  private

  def stream_message(stream, text)
    response.stream.write({ stream: stream, text: text }.to_json + "\n")
  end

  def authenticate_api_key
    render(text: 'API key is not valid', status: 401) if current_user.nil?
  end

  def current_user
    @current_user ||= User.find_by_api_key(params[:api_key])
  end
end<commit_msg>No need to render error streamed<commit_after># Run API used by the morph command-line client
class ApiController < ApplicationController
  include ActionController::Live

  # The run_remote method will be secured with a key so shouldn't need csrf
  # token authentication
  skip_before_filter :verify_authenticity_token, only: [:run_remote]
  before_filter :authenticate_api_key

  # Receive code from a remote client, run it and return the result.
  # This will be a long running request
  # TODO: Document this API
  def run_remote
    if !current_user.ability.can? :create, Run
      render json: {
        stream: 'internalerr',
        text: "You currently can't start a scraper run." \
              ' See https://morph.io for more details'
      }
    else
      response.headers['Content-Type'] = 'text/event-stream'
      run = Run.create(queued_at: Time.now, auto: false, owner: current_user)
      # TODO: Shouldn't need to untar here because it just gets retarred
      Archive::Tar::Minitar.unpack(params[:code].tempfile, run.repo_path)

      Morph::Runner.new(run).go { |s, text| stream_message(s, text) }
      response.stream.close
    end
  ensure
    # Cleanup run
    FileUtils.rm_rf(run.data_path)
    FileUtils.rm_rf(run.repo_path)
  end

  private

  def stream_message(stream, text)
    response.stream.write({ stream: stream, text: text }.to_json + "\n")
  end

  def authenticate_api_key
    render(text: 'API key is not valid', status: 401) if current_user.nil?
  end

  def current_user
    @current_user ||= User.find_by_api_key(params[:api_key])
  end
end
{'fraction_non_alphanumeric': 0.09282296650717703, 'fraction_numerical': 0.0019138755980861245, 'mean_word_length': 3.44822695035461, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4917399', 'n_tokens_mistral': 1001, 'n_tokens_neox': 967, 'n_words': 296}