Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Rack middleware that abstracts format (extension) away from the path (into env)

branch: master
3 comments

Don't strip unrecognized extensions

Thanks to jamesarosen for pointing this out
latest commit 79164a7d8d
Martin authored April 09, 2010
Octocat-spinner-32 examples Update example app January 06, 2010
Octocat-spinner-32 lib Don't strip unrecognized extensions April 09, 2010
Octocat-spinner-32 test Don't strip unrecognized extensions April 09, 2010
Octocat-spinner-32 .gitignore v0.9.9 January 07, 2010
Octocat-spinner-32 LICENSE Initial commit. May 21, 2009
Octocat-spinner-32 Manifest Update README January 07, 2010
Octocat-spinner-32 README.md Update README February 01, 2010
Octocat-spinner-32 Rakefile Accept a default format January 07, 2010
Octocat-spinner-32 rack-abstract-format.gemspec v0.9.9 January 07, 2010
Octocat-spinner-32 specs.watchr Add specs.watchr script January 07, 2010
README.md

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

Something went wrong with that request. Please try again.