0.01
No commit activity in last 3 years
No release in over 3 years
If you are creating Rack middleware that changes the HTML response, use 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.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.6
>= 0

Runtime

~> 1.4
~> 1.0
 Project Readme

Rack::Plastic

Description

If you are creating Rack middleware that changes the HTML response, use 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.

Usage

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 as well as some convenience methods for testing.

Examples

The examples/middlewares directory has examples of writing middleware using Rack::Plastic.

Rails Streaming

This middleware is not compatible with Rails HTTP streaming. According to Railscast #266: "Also, streaming is incompatible with some middleware. If the middleware modifies the response then it will not work with streaming."

Testing

Rack::Plastic comes with some test helpers that make it easier for you to write tests for your Rack middleware. Refer to the documentation for PlasticTestHelper for more details.

If you want to test Rack::Plastic itself, this gem doesn't come with automated tests, but it provides manual tests. Each of the example middlewares is inserted into a Sinatra, Rails, and Rack test app.

To run the Rails test app:

To run the Sinatra test app:

To run the Rack test app: