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



15
16
17
18
19
20
21
# File 'lib/offshore/server/middleware.rb', line 15

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

#initObject



11
12
13
# File 'lib/offshore/server/middleware.rb', line 11

def init
  init_server
end

#init_serverObject



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

def init_server
  Offshore::Database.init # has its own singleton code
end

#offshore_call(env) ⇒ Object



33
34
35
36
37
38
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
# File 'lib/offshore/server/middleware.rb', line 33

def offshore_call(env)
  status = 500     
  headers = {}
  method = offshore_method(env)
  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

#offshore_method(env) ⇒ Object



28
29
30
31
# File 'lib/offshore/server/middleware.rb', line 28

def offshore_method(env)
  env["PATH_INFO"] =~ /^\/offshore_tests\/(.*)/
  $1
end

#offshore_request?(env) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
# File 'lib/offshore/server/middleware.rb', line 23

def offshore_request?(env)
  return false unless Offshore.enabled?
  !!offshore_method(env)
end