Class: Devrobber::Rack
- Inherits:
-
Object
- Object
- Devrobber::Rack
- Defined in:
- lib/devrobber/rack.rb
Instance Method Summary collapse
- #append_to_html_body(response_body, content) ⇒ Object
- #call(env) ⇒ Object
- #empty?(response) ⇒ Boolean
- #file?(headers) ⇒ Boolean
- #html_request?(headers, response) ⇒ Boolean
-
#initialize(app) ⇒ Rack
constructor
A new instance of Rack.
- #response_body(response) ⇒ Object
- #sse?(response) ⇒ Boolean
Constructor Details
#initialize(app) ⇒ Rack
Returns a new instance of Rack.
3 4 5 |
# File 'lib/devrobber/rack.rb', line 3 def initialize(app) @app = app end |
Instance Method Details
#append_to_html_body(response_body, content) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/devrobber/rack.rb', line 53 def append_to_html_body(response_body, content) if response_body.include?('<body>') position = response_body.index('<body>') response_body.insert(position, content) else response_body << content end end |
#call(env) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/devrobber/rack.rb', line 7 def call(env) status, headers, response = @app.call(env) return [status, headers, response] if file?(headers) || sse?(response) || empty?(response) if status == 200 && !response_body(response).frozen? && html_request?(headers, response) response_body = response_body(response) headers['Content-Length'] = response_body.bytesize.to_s UniformNotifier.active_notifiers.each do |notifier| append_to_html_body(response_body, notifier.inline_notify(Devrobber.).to_s) end end [status, headers, response_body ? [response_body] : response] end |
#empty?(response) ⇒ Boolean
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/devrobber/rack.rb', line 21 def empty?(response) 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
33 34 35 |
# File 'lib/devrobber/rack.rb', line 33 def file?(headers) headers["Content-Transfer-Encoding"] == "binary" end |
#html_request?(headers, response) ⇒ Boolean
41 42 43 |
# File 'lib/devrobber/rack.rb', line 41 def html_request?(headers, response) headers['Content-Type'] && headers['Content-Type'].include?('text/html') && response_body(response).include?("<html") end |
#response_body(response) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/devrobber/rack.rb', line 45 def response_body(response) if rails? Array === response.body ? response.body.first : response.body else response.first end end |
#sse?(response) ⇒ Boolean
37 38 39 |
# File 'lib/devrobber/rack.rb', line 37 def sse?(response) response.respond_to?(:stream) && response.stream.is_a?(::ActionController::Live::Buffer) end |