Rack::Plastic Helps You Write Rack Middleware

Introducing Rack::Plastic

Wares made of plastic.  The ones in the center would therefore be plastic middleware.

Wares made of plastic. The ones in the center would therefore be plastic middleware.

Back in November I participated in the CodeRack Rack middleware contest. Besides submitting entries that were stupid and downright dangerous I actually managed to create some middleware of value.

Some of my middleware had a common theme: change the response HTML. This is how Rack::Plastic was born. When you write Rack middleware that changes the response HTML, you have to remember to do several things, including checking that the response’s type is HTML and updating the content length. Rack::Plastic does this for you, as well as provides you with some convenience methods for writing and testing your middleware.

To get started, read the documentation and check out the source code for Rack::Gsub, which is a good example of how to write and test Rack::Plastic middleware.

All of the following middleware is powered by Rack::Plastic.



Rack::Environmental adds a configurable indicator badge to your web application so that you can tell whether you’re working with the staging, development or production version of your web app.

Example Use Case

This middleware saves your job by keeping you from accidentally testing that new nuclear-deletion feature on your production app.


Look, it's a G-sub!

Look, it's a G-sub!


Rack::Gsub will replace matching text in the web page.

Example Use Case

You are testing an alpha version of your Rails app. You’ve got a model called Worker that you’re thinking about renaming Employee. With a simple call to Rack::Gsub, you can replace every occurrence of “worker” with “employee” in your web app in order to test out the new term.



Rack::Linkify wraps plain text links like http://www.google.com in an anchor tag.

Example Use Case

User-generated content, such as comments, sometimes have links. Rack::Linkify will make them clickable.

You can skip to the end and leave a response. Pinging is currently not allowed.

Leave a Reply