Class: Strobe::Middleware::Addons

Inherits:
Object
  • Object
show all
Defined in:
lib/strobe/middleware/addons.rb

Defined Under Namespace

Classes: Config

Constant Summary collapse

ADDON_PATH =
%r[/_strobe/([^/]+)(/.*)?]i

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ Addons

Returns a new instance of Addons.



16
17
18
# File 'lib/strobe/middleware/addons.rb', line 16

def initialize(app, opts = {})
  @app, @opts = app, opts
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/strobe/middleware/addons.rb', line 20

def call(env)
  if env['PATH_INFO'] =~ ADDON_PATH
    addon, path = $1, $2
    call_addon(env, addon, path)
  else
    @app.call(env)
  end
end

#call_addon(env, addon, path) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/strobe/middleware/addons.rb', line 29

def call_addon(env, addon, path)
  script_name, path_info = "/_strobe/#{addon}", path || ""
  env = env.merge("SCRIPT_NAME" => script_name, "PATH_INFO" => path_info)

  begin
    addon_klass = find_addon(addon)
  rescue Exception => e
    msg = "Not found"
    return [ 404, {"Content-Length" => msg.bytesize.to_s}, [msg]]
  end

  status, hdrs, body = addon_klass.new(config).call(env)

  if Proxy.proxy_response?(status, hdrs)
    is_head = env['REQUEST_METHOD'] == 'HEAD'
    Proxy.strobe_redirect(is_head, hdrs, body, env['async.callback'])
    # Warp speed mr. zulu
    throw :async
  else
    [ status, hdrs, body ]
  end
rescue Exception => e
  msg = "Something went wrong.\n#{e.message}\n" + e.backtrace.join("\n")
  [ 500, {"Content-Length" => msg.bytesize.to_s}, [msg]]
end