Class: EnoughFields::Rack

Inherits:
Object
  • Object
show all
Defined in:
lib/enough_fields/rack.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Rack

Returns a new instance of Rack.



3
4
5
# File 'lib/enough_fields/rack.rb', line 3

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/enough_fields/rack.rb', line 7

def call(env)
  return @app.call(env) unless EnoughFields.enable?

  EnoughFields.start_request
  status, headers, response = @app.call(env)
  return [status, headers, response] if empty?(response)

  if EnoughFields.notification?
    if status == 200 and !response.body.frozen? and check_html?(headers, response)
      response_body = response.body << EnoughFields.gather_inline_notifications
      headers['Content-Length'] = response_body.length.to_s
    end
    EnoughFields.perform_out_of_channel_notifications
  end
  response_body ||= response.body
  EnoughFields.end_request
  no_browser_cache(headers) if EnoughFields.disable_browser_cache
  [status, headers, [response_body]]
end

#check_html?(headers, response) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/enough_fields/rack.rb', line 34

def check_html?(headers, response)
  headers['Content-Type'] and headers['Content-Type'].include? 'text/html' and response.body =~ %r{<html.*</html>}m
end

#empty?(response) ⇒ Boolean

fix issue if response’s body is a Proc

Returns:

  • (Boolean)


28
29
30
31
32
# File 'lib/enough_fields/rack.rb', line 28

def empty?(response)
  # response may be ["Not Found"], ["Move Permanently"], etc.
  (response.is_a?(Array) && response.size <= 1) ||
  !response.body.is_a?(String) || response.body.empty?
end

#no_browser_cache(headers) ⇒ Object



38
39
40
41
42
# File 'lib/enough_fields/rack.rb', line 38

def no_browser_cache(headers)
  headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
  headers["Pragma"] = "no-cache"
  headers["Expires"] = "Wed, 09 Sep 2009 09:09:09 GMT"
end