Class: Ruby2html::HtmlBeautifierMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/gem/ruby2html/html_beautifier_middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ HtmlBeautifierMiddleware



5
6
7
# File 'lib/gem/ruby2html/html_beautifier_middleware.rb', line 5

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/gem/ruby2html/html_beautifier_middleware.rb', line 9

def call(env)
  status, headers, body = @app.call(env)

  if headers['Content-Type']&.include?('text/html')
    new_body = []
    body.each do |chunk|
      new_body << HtmlBeautifier.beautify(chunk)
    end
    headers['Content-Length'] = new_body.map(&:bytesize).sum.to_s
    body = new_body
  end

  [status, headers, body]
end