class Bullet::Rack

Public Class Methods

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

Public Instance Methods

append_to_html_body(response_body, content) click to toggle source
# File lib/bullet/rack.rb, line 46
def append_to_html_body(response_body, content)
  body = response_body.dup
  if body.include?('</body>')
    position = body.rindex('</body>')
    body.insert(position, content)
  else
    body << content
  end
end
call(env) click to toggle source
# File lib/bullet/rack.rb, line 11
def call(env)
  return @app.call(env) unless Bullet.enable?
  Bullet.start_request
  status, headers, response = @app.call(env)

  response_body = nil
  if Bullet.notification?
    if !file?(headers) && !sse?(headers) && !empty?(response) &&
       status == 200 && html_request?(headers, response)
      response_body = response_body(response)
      response_body = append_to_html_body(response_body, footer_note) if Bullet.add_footer
      response_body = append_to_html_body(response_body, Bullet.gather_inline_notifications)
      headers['Content-Length'] = response_body.bytesize.to_s
    end
    Bullet.perform_out_of_channel_notifications(env)
  end
  [status, headers, response_body ? [response_body] : response]
ensure
  Bullet.end_request
end
empty?(response) click to toggle source

fix issue if response's body is a Proc

# File lib/bullet/rack.rb, line 33
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
file?(headers) click to toggle source
# File lib/bullet/rack.rb, line 60
def file?(headers)
  headers['Content-Transfer-Encoding'] == 'binary' || headers['Content-Disposition']
end
html_request?(headers, response) click to toggle source
# File lib/bullet/rack.rb, line 68
def html_request?(headers, response)
  headers['Content-Type'] && headers['Content-Type'].include?('text/html') && response_body(response).include?('<html')
end
response_body(response) click to toggle source
# File lib/bullet/rack.rb, line 72
def response_body(response)
  if response.respond_to?(:body)
    Array === response.body ? response.body.first : response.body
  else
    response.first
  end
end
sse?(headers) click to toggle source
# File lib/bullet/rack.rb, line 64
def sse?(headers)
  headers['Content-Type'] == 'text/event-stream'
end

Private Instance Methods