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 47
def append_to_html_body(response_body, content)
  body = response_body.dup
  content = content.html_safe if content.respond_to?(:html_safe)
  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 Bullet.inject_into_page? && !file?(headers) && !sse?(headers) && !empty?(response) && status == 200
      if 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)
        response_body = append_to_html_body(response_body, xhr_script) if Bullet.add_footer && !Bullet.skip_http_headers
        headers['Content-Length'] = response_body.bytesize.to_s
      elsif !Bullet.skip_http_headers
        set_header(headers, 'X-bullet-footer-text', Bullet.footer_info.uniq) if Bullet.add_footer
        set_header(headers, 'X-bullet-console-text', Bullet.text_notifications) if Bullet.console_enabled?
      end
    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 40
def empty?(response)
  # response may be ["Not Found"], ["Move Permanently"], etc, but
  # those should not happen if the status is 200
  body = response_body(response)
  body.nil? || body.empty?
end
file?(headers) click to toggle source
# File lib/bullet/rack.rb, line 70
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 78
def html_request?(headers, response)
  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 82
def response_body(response)
  if response.respond_to?(:body)
    Array === response.body ? response.body.first : response.body
  else
    response.first
  end
end
set_header(headers, header_name, header_array) click to toggle source
# File lib/bullet/rack.rb, line 62
def set_header(headers, header_name, header_array)
  # Many proxy applications such as Nginx and AWS ELB limit
  # the size a header to 8KB, so truncate the list of reports to
  # be under that limit
  header_array.pop while header_array.to_json.length > 8 * 1024
  headers[header_name] = header_array.to_json
end
sse?(headers) click to toggle source
# File lib/bullet/rack.rb, line 74
def sse?(headers)
  headers['Content-Type'] == 'text/event-stream'
end

Private Instance Methods

details_attributes() click to toggle source
# File lib/bullet/rack.rb, line 92
    def details_attributes
      <<~EOF
        id="bullet-footer" data-is-bullet-footer
        style="cursor: pointer; position: fixed; left: 0px; bottom: 0px; z-index: 9999; background: #fdf2f2; color: #9b1c1c; font-size: 12px; border-radius: 0px 8px 0px 0px; border: 1px solid #9b1c1c;"
      EOF
    end
summary_attributes() click to toggle source
# File lib/bullet/rack.rb, line 99
    def summary_attributes
      <<~EOF
        style="font-weight: 600; padding: 2px 8px"
      EOF
    end
xhr_script() click to toggle source

Make footer work for XHR requests by appending data to the footer

# File lib/bullet/rack.rb, line 118
def xhr_script
  "<script type='text/javascript'>#{File.read("#{__dir__}/bullet_xhr.js")}</script>"
end