Parent

Methods

Included Modules

Class/Module Index [+]

Quicksearch

Rack::MediaType

Constants

ACCEPTED_MEDIA_TYPES

Define supported media types here The :return key stands for content-type which will be returned The :match key stands for the matching Accept header

Public Instance Methods

call(env) click to toggle source
# File lib/sinatra/rack_accept.rb, line 112
def call(env)
  accept, index = env['rack-accept.request'], {}

  # Skip everything when 'format' parameter is set in URL
  if env['rack.request.query_hash']["format"]
     media_type = case env['rack.request.query_hash']["format"]
        when 'html' then :html
        when 'xml' then :xml
        when 'json' then :json
        when 'gv' then :gv
        when 'png' then :png
      end
    index[media_type] = 1 if media_type
  else
    # Sort all requested media types in Accept using their 'q' values
    sorted_media_types = accept.media_type.qvalues.to_a.sort{ |a,b| a[1]<=>b[1] }.collect { |t| t.first }
    # If Accept header is missing or is empty, fallback to XML format
    sorted_media_types << 'application/xml' if sorted_media_types.empty?
    # Choose the right format with the media type according to the priority
    ACCEPTED_MEDIA_TYPES.each do |format, definition|
      definition[:match].each do |media_type|
        break if index[format] = sorted_media_types.index(media_type)
      end
    end
    # Reject formats with no/nil priority
    index.reject! { |format, priority| not priority }
  end

  #puts sorted_media_types.inspect
  #puts index.inspect

  # If after all we don't have any matching format assume that client has
  # requested unknown/wrong media type and throw an 406 error with no body
  if index.keys.empty?
    status, headers, response = 406, {}, ""
  else
    env['rack-accept.formats'] = index
    status, headers, response = @app.call(env)
  end
  [status, headers, response]
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.