Module: Rack::MasonHelper

Included in:
Mason
Defined in:
lib/mason_helper.rb

Overview

MasonHelper was created to separate Mason’s implementation from Mason’s interface.

This class basically contains all of the Rack code that you’d have to write every time you wanted to write a new middleware that modifies the HTML response.

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object

Rack::Mason provides a call method so that your middleware doesn’t have to.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mason_helper.rb', line 39

def call(env)
  status, @headers, @body = @app.call(env)
  if status == 200 && html?
    @request = Rack::Request.new(env)
    body_string = @body.length == 1 ? @body[0] : @body.join("")

    doc = body_string.respond_to?(:nokogiri) ? body_string.nokogiri : Nokogiri::HTML(body_string)
    
    if doc && update_body(doc) != false
      update_response_body(NokogiriString.new(doc))
    end
  end

  [status, @headers, @body]
end