No commit activity in last 3 years
No release in over 3 years
Rack middleware that abstracts format (extension) away from the path (into env).
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
 Project Readme

Summary

Strips the extension from the requested path (env['PATH_INFO']), casts it to media type, and prepends it to env['HTTP_ACCEPT']. This allows dealing with path information separately from format information, which are often different concerns.

This is especially useful when dealing with routes as it allows a resource to always point to the same action independently of the requested format.

Install

gem install rack-abstract-format

Usage

require 'rack'
require 'rack/abstract_format'

use Rack::AbstractFormat
run app

The request:

GET /path/resource.xml
Accept: text/html

will become:

GET /path/resource
env['HTTP_ACCEPT'] #=> 'application/xml,text/html'

AbstractFormat also accepts an optional argument, used to set the default format that should be assumed when none is specified on the URL:

use Rack::AbstractFormat, 'text/html'
#=> GET /path/resource      # requested
#=> GET /path/resource.html # assumed

Tip

The abstracted media type can then be easily retrieved with the Rack::AcceptMediaTypes convenience middleware, which parses env['HTTP_ACCEPT'].

request.accept_media_types          #=> ['application/xml', 'text/html']
request.accept_media_types.prefered #=>  'application/xml'

See Also

Links