Class: Rack::Plastic

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

Overview

If you are creating Rack middleware that changes the HTML response, inherit from Rack::Plastic to get a head start. Rack::Plastic takes care of the boilerplate Rack glue so that you can focus on simply changing the HTML.

There are two ways you can change the HTML: as a Nokogiri document or as a string. Simply define one of the following methods:

def change_nokogiri_doc(doc)
  ... insert code that changes the doc ...
  doc
end

def change_html_string(html)
  ... insert code that changes the html string ...
  html
end

If you define both methods, change_nokogiri_doc will be called first, then the doc will be converted to an HTML string, then the string will be passed to change_html_string.

Rack::Plastic also provides some convenience methods for interacting with Rack and Nokogiri.

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Plastic

Rack::Plastic provides an initialize method so that your middleware doesn’t have to.



35
36
37
38
39
# File 'lib/rack-plastic.rb', line 35

def initialize(app, options = {}) #:nodoc:
  @app = app
  @options = options
  @p = ::Rack::PlasticHelper.new
end

Instance Method Details

#call(env) ⇒ Object

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



44
45
46
# File 'lib/rack-plastic.rb', line 44

def call(env) #:nodoc:
  @p.send(:handle_request, env, @app, @options, self)
end