This code was inherited from respond_to plugin github.com/cehoffman/sinatra-respond_to
This method is used to overide the default content_type returned from rack-accept middleware.
# File lib/sinatra/rack_accept.rb, line 53 def self.included(klass) klass.class_eval do alias :content_type_without_save :content_type def content_type(*args) content_type_without_save *args request.env['rack-accept.formats'] = { args.first.to_sym => 1 } response['Content-Type'] end end end
# File lib/sinatra/rack_accept.rb, line 64 def accepting_formats request.env['rack-accept.formats'] end
# File lib/sinatra/rack_accept.rb, line 56 def content_type(*args) content_type_without_save *args request.env['rack-accept.formats'] = { args.first.to_sym => 1 } response['Content-Type'] end
# File lib/sinatra/rack_accept.rb, line 74 def respond_to(&block) wants = {} def wants.method_missing(type, *args, &handler) self[type] = handler end yield wants @media_type = accepting_formats.to_a.sort { |a,b| a[1]<=>b[1] }.reverse.select do |format, priority| wants.keys.include?(format) == true end.first if @media_type and @media_type[0] @media_type = @media_type[0] headers 'Content-Type' => Rack::MediaType::ACCEPTED_MEDIA_TYPES[@media_type][:return] wants[@media_type.to_sym].call if wants[@media_type.to_sym] else headers 'Content-Type' => nil status 406 end end
Generated with the Darkfish Rdoc Generator 2.