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 49 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 60 def accepting_formats request.env['rack-accept.formats'] end
# File lib/sinatra/rack_accept.rb, line 52 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 70 def respond_to(&block) wants = {} def wants.method_missing(type, *args, &handler) self[type] = handler end yield wants if Deltacloud.default_frontend.name == :cimi @media_type = (accepting_formats.has_key?(:xml) ? [:xml, accepting_formats[:xml]] : nil) end if Deltacloud.respond_to? :default_frontend @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.kind_of? Symbol @media_type = [ @media_type ] end if @media_type and @media_type[0] @media_type = @media_type[0] if Rack::MediaType::ACCEPTED_MEDIA_TYPES[@media_type] headers 'Content-Type' => Rack::MediaType::ACCEPTED_MEDIA_TYPES[@media_type][:return] else headers 'Content-Type' => 'application/xml' end wants[@media_type.to_sym].call if wants[@media_type.to_sym] else headers 'Content-Type' => nil status 406 end end
# File lib/sinatra/rack_accept.rb, line 64 def static_file?(path) public_dir = File.expand_path(settings.public) path = File.expand_path(File.join(public_dir, unescape(path))) path[0, public_dir.length] == public_dir && File.file?(path) end