Class: MissionControl::Jobs::Theme::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/mission_control/jobs/theme/middleware.rb

Overview

Intercept HTML responses from the Mission Control Jobs engine and inject theme CSS and optional Prism.js syntax highlighting before </head>.

Only rewrites responses that match the engine mount path and have an text/html content type.

Examples:

Manual Rack usage (typically wired by Railtie)

use MissionControl::Jobs::Theme::Middleware,
    mount_path: "/admin/jobs",
    syntax_highlighting: true

See Also:

Constant Summary collapse

THEME_CSS =
'<link rel="stylesheet" href="/mission_control/css/theme.min.css">'
PRISM_CSS =
'<link rel="stylesheet" href="/mission_control/css/prism.min.css">'
PRISM_JS =
'<script src="/mission_control/js/prism.min.js" data-manual></script>'
PRISM_INIT =
'<script src="/mission_control/js/prism-init.js"></script>'

Instance Method Summary collapse

Constructor Details

#initialize(app, mount_path: RouteDiscovery::FALLBACK, syntax_highlighting: true) ⇒ Middleware

Returns a new instance of Middleware.

Parameters:

  • app (#call)

    the next Rack application in the middleware stack

  • mount_path (String) (defaults to: RouteDiscovery::FALLBACK)

    engine mount path to match against requests

  • syntax_highlighting (Boolean) (defaults to: true)

    inject Prism.js assets when true



28
29
30
31
32
33
# File 'lib/mission_control/jobs/theme/middleware.rb', line 28

def initialize(app, mount_path: RouteDiscovery::FALLBACK, syntax_highlighting: true)
  @app = app
  @mount_path = mount_path
  @mount_path_prefix = "#{mount_path}/"
  @injection = build_injection(syntax_highlighting)
end

Instance Method Details

#call(env) ⇒ Array(Integer, Hash, #each)

Process a Rack request, injecting theme assets into matching HTML responses.

Parameters:

  • env (Hash)

    Rack environment hash

Returns:

  • (Array(Integer, Hash, #each))

    Rack-compatible response triplet



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mission_control/jobs/theme/middleware.rb', line 39

def call(env)
  status, headers, response = @app.call(env)

  if inject_theme?(env, status, headers)
    body = +""
    response.each { |part| body << part }
    response.close if response.respond_to?(:close)

    body = inject_assets(body)
    headers["content-length"] = body.bytesize.to_s if headers.key?("content-length")

    [status, headers, [body]]
  else
    [status, headers, response]
  end
end