<commit_before>module Offshore
  class Middleware
    def initialize(app)
      @app = app
    end
    
    def init_server
      Offshore::Database.init # has it's own singleton code
    end
    
    def init_thread
      return if @init_thread
      @init_thread = true
      
      # TODO: move this to a config block
      if defined?(Rails)
        begin
          require Rails.root.join("spec","spec_helper")
        rescue
          raise
        end
      end
    end
    
    def init
      init_thread
      init_server
    end


    def call(env)
      if offshore_request?
        offshore_call($1, env)
      else
        @app.call(env)
      end
    end

    def offshore_request?
      return false unless ENV['OFFSHORE'].to_s == 'true'
      (env["PATH_INFO"] =~ /^\/offshore_tests\/(.*)/) == 0
    end
  
    def offshore_call(method, env)
      status = 500     
      headers = {}
      hash = {"error" => "Unknown method: #{method}"}
      
      Logger.info("Offshore Tests Action: #{method}")
      
      begin
        case method
        when "factory_create", "suite_start", "suite_stop", "test_start", "test_stop"
          init if method == "suite_start"
          controller = Controller.new(Rack::Request.new(env).params)
          status, hash = controller.send(method)
        end
      rescue CheckBackLater => e
        hash = {"error" => e.message}
        status = Offshore::WAIT_CODE
      rescue StandardError => e
        hash = {"error" => e.message}
        status = 500
        raise # for now
      end
      
      content = hash.to_json
      headers['Content-Type'] = "application/json"
      headers['Content-Length'] = content.length.to_s<commit_msg>forgot to pass env around<commit_after>module Offshore
  class Middleware
    def initialize(app)
      @app = app
    end
    
    def init_server
      Offshore::Database.init # has it's own singleton code
    end
    
    def init_thread
      return if @init_thread
      @init_thread = true
      
      # TODO: move this to a config block
      if defined?(Rails)
        begin
          require Rails.root.join("spec","spec_helper")
        rescue
          raise
        end
      end
    end
    
    def init
      init_thread
      init_server
    end


    def call(env)
      if offshore_request?(env)
        offshore_call($1, env)
      else
        @app.call(env)
      end
    end

    def offshore_request?(env)
      return false unless ENV['OFFSHORE'].to_s == 'true'
      (env["PATH_INFO"] =~ /^\/offshore_tests\/(.*)/) == 0
    end
  
    def offshore_call(method, env)
      status = 500     
      headers = {}
      hash = {"error" => "Unknown method: #{method}"}
      
      Logger.info("Offshore Tests Action: #{method}")
      
      begin
        case method
        when "factory_create", "suite_start", "suite_stop", "test_start", "test_stop"
          init if method == "suite_start"
          controller = Controller.new(Rack::Request.new(env).params)
          status, hash = controller.send(method)
        end
      rescue CheckBackLater => e
        hash = {"error" => e.message}
        status = Offshore::WAIT_CODE
      rescue StandardError => e
        hash = {"error" => e.message}
        status = 500
        raise # for now
      end
      
      content = hash.to_json
      headers['Content-Type'] = "application/json"
      headers['Content-Length'] = content.length.to_s
{'fraction_non_alphanumeric': 0.10473961380924517, 'fraction_numerical': 0.004681100058513751, 'mean_word_length': 1.8803706823925863, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '3083239', 'n_tokens_mistral': 1034, 'n_tokens_neox': 1009, 'n_words': 259}