Class: Bullet::Rack

Inherits:
Object show all
Includes:
Dependency
Defined in:
lib/bullet/rack.rb

Instance Method Summary collapse

Methods included from Dependency

#active_record30?, #active_record31?, #active_record32?, #active_record3?, #active_record40?, #active_record41?, #active_record42?, #active_record4?, #active_record?, #active_record_version, #mongoid2x?, #mongoid3x?, #mongoid4x?, #mongoid?, #mongoid_version, #rails?

Constructor Details

#initialize(app) ⇒ Rack

Returns a new instance of Rack.



5
6
7
# File 'lib/bullet/rack.rb', line 5

def initialize(app)
  @app = app
end

Instance Method Details

#append_to_html_body(response_body, content) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/bullet/rack.rb', line 44

def append_to_html_body(response_body, content)
  if response_body.include?('</body>')
    position = response_body.rindex('</body>')
    response_body.insert(position, content)
  else
    response_body << content
  end
end

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bullet/rack.rb', line 9

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 && !response_body(response).frozen? && html_request?(headers, response)
      response_body = response_body(response)
      append_to_html_body(response_body, footer_note) if Bullet.add_footer
      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) ⇒ Boolean

fix issue if response’s body is a Proc

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bullet/rack.rb', line 31

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) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/bullet/rack.rb', line 57

def file?(headers)
  headers["Content-Transfer-Encoding"] == "binary"
end


53
54
55
# File 'lib/bullet/rack.rb', line 53

def footer_note
  "<div #{footer_div_attributes}>" + Bullet.footer_info.uniq.join("<br>") + "</div>"
end

#html_request?(headers, response) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/bullet/rack.rb', line 65

def html_request?(headers, response)
  headers['Content-Type'] && headers['Content-Type'].include?('text/html') && response_body(response).include?("<html")
end

#response_body(response) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/bullet/rack.rb', line 69

def response_body(response)
  if rails?
    Array === response.body ? response.body.first : response.body
  else
    response.first
  end
end

#sse?(headers) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/bullet/rack.rb', line 61

def sse?(headers)
  headers["Content-Type"] == "text/event-stream"
end