module Bullet class Rack include Dependency def initialize(app) @app = app end def call(env) return @app.call(env) unless Bullet.enable? Bullet.start_request status, headers, response = @app.call(env) return [status, headers, response] if file?(headers) || sse?(response) || empty?(response) response_body = nil if Bullet.notification? if status == 200 && !response_body(response).frozen? && html_request?(headers, response) response_body = response_body(response) << Bullet.gather_inline_notifications add_footer_note(response_body) if Bullet.add_footer headers['Content-Length'] = response_body.bytesize.to_s end end if Bullet.enable? && Bullet.notification? Bullet.perform_out_of_channel_notifications(env) end [status, headers, response_body ? [response_body] : response] ensure Bullet.end_request end # fix issue if response's body is a Proc def empty?(response) # response may be ["Not Found"], ["Move Permanently"], etc. if rails? (response.is_a?(Array) && response.size <= 1) || !response.respond_to?(:body) || !response_body(response).respond_to?(:empty?) || response_body(response).empty? else body = response_body(response) body.nil? || body.empty? end end def add_footer_note(response_body) response_body << "
" + Bullet.footer_info.uniq.join("
") + "
" end def file?(headers) headers["Content-Transfer-Encoding"] == "binary" end def sse?(response) response.respond_to?(:stream) && response.stream.is_a?(ActionController::Live::Buffer) end def html_request?(headers, response) headers['Content-Type'] && headers['Content-Type'].include?('text/html') && response_body(response).include?("