class HocusPocus::CommandLine::Middleware

Public Class Methods

new(app) click to toggle source
# File engines/command_line/lib/hocus_pocus/command_line/middleware.rb, line 8
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File engines/command_line/lib/hocus_pocus/command_line/middleware.rb, line 12
def call(env)
  status, headers, body = @app.call env

  if headers && headers['Content-Type']&.include?('text/html') && (env['REQUEST_PATH'] !~ %r[^/*hocus_pocus/])
    case body
    when ActionDispatch::Response, ActionDispatch::Response::RackBody
      body = body.body
    when Array
      body = body[0]
    end

    body.sub!(/<\/head>/i) { %Q[<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script></head>] } unless body =~ /jquery(\.min)?\.js/
    body.sub!(/<div id="#{HocusPocus::CONTAINER}" .*?>/i) { %Q[#{$~}#{command_line_link}] }

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

Private Instance Methods