class HocusPocus::Middleware

Public Class Methods

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

Public Instance Methods

call(env) click to toggle source
# File lib/hocus_pocus/middleware.rb, line 11
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!(/<\/body>/i) { %Q[<div id="#{HocusPocus::CONTAINER}" style="position:absolute; top:0; right: 0;"><link href="/assets/recorder.css" media="all" rel="stylesheet" type="text/css"></div>#{$~}] }

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