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 offshore_request?(env)
    offshore_call(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 its 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(env) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/offshore/server/middleware.rb', line 49

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



44
45
46
47
# File 'lib/offshore/server/middleware.rb', line 44

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

#offshore_request?(env) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/offshore/server/middleware.rb', line 39

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