Skip to content

Instantly share code, notes, and snippets.

@nakajima
Forked from reinh/format_accepts.rb
Created April 14, 2009 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nakajima/95225 to your computer and use it in GitHub Desktop.
Save nakajima/95225 to your computer and use it in GitHub Desktop.
Fork'd to follow
module Rack
#
# A Rack middleware for automatically removing the format token at the end
# of a request path and adding its media type to the HTTP_ACCEPT header.
#
# MIT-License - Rein Henrichs
#
class FormatAccepts
def initialize(app)
@app = app
end
def call(env)
req = Rack::Request.new(env)
if req.path_info =~ /(.*)(\..+)$/
env["PATH_INFO"] = $1
ext = $2
accept = env['HTTP_ACCEPT'].split(',') rescue []
media_type = Rack::Mime::MIME_TYPES[ext]
accept.unshift(media_type) if media_type
env['HTTP_ACCEPT'] = accept.join(',')
end
@app.call(env)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment