Class: Offshore::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/offshore/server/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



3
4
5
# File 'lib/offshore/server/middleware.rb', line 3

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/offshore/server/middleware.rb', line 31

def call(env)
  if (env["PATH_INFO"] =~ /^\/offshore_tests\/(.*)/) == 0
    offshore_call($1, env)
  else
    @app.call(env)
  end
end

#initObject



25
26
27
28
# File 'lib/offshore/server/middleware.rb', line 25

def init
  init_thread
  init_server
end

#init_serverObject



7
8
9
# File 'lib/offshore/server/middleware.rb', line 7

def init_server
  Offshore::Database.init # has it's own singleton code
end

#init_threadObject



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/offshore/server/middleware.rb', line 11

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

#offshore_call(method, env) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/offshore/server/middleware.rb', line 39

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
  out = [status, headers, [content]]
  Logger.info("Offshore Tests #{method}... returns: #{out.to_s}")
  out
end