module Sinatra::RespondTo::Helpers
Public Instance Methods
charset(val=nil)
click to toggle source
# File lib/sinatra/respond_to.rb, line 190 def charset(val=nil) fail "Content-Type must be set in order to specify a charset" if response['Content-Type'].nil? if response['Content-Type'] =~ /charset=[^;]+/ response['Content-Type'].sub!(/charset=[^;]+/, (val == '' && '') || "charset=#{val}") else response['Content-Type'] += ";charset=#{val}" end unless val.nil? response['Content-Type'][/charset=([^;]+)/, 1] end
content_type(*args)
click to toggle source
Patch the #content_type function to remember the set type This helps cut down on time in the format helper so it doesn't have to do a reverse lookup on the header
Calls superclass method
# File lib/sinatra/respond_to.rb, line 164 def content_type(*args) @_format = args.first.to_sym super end
format(val=nil)
click to toggle source
# File lib/sinatra/respond_to.rb, line 169 def format(val=nil) unless val.nil? mime_type = ::Sinatra::Base.mime_type(val) fail "Unknown media type #{val}\nTry registering the extension with a mime type" if mime_type.nil? @_format = val.to_sym response['Content-Type'] ? response['Content-Type'].sub!(/^[^;]+/, mime_type) : content_type(@_format) end @_format end
respond_to() { |wants| ... }
click to toggle source
# File lib/sinatra/respond_to.rb, line 202 def respond_to(&block) wants = {} def wants.method_missing(type, *args, &handler) ::Sinatra::Base.send(:fail, "Unknown media type for respond_to: #{type}\nTry registering the extension with a mime type") if ::Sinatra::Base.mime_type(type).nil? self[type] = handler end yield wants if wants[format].nil? # Loop through request.accept in prioritized order looking for a Mime Type having a format # that is recognized. alt = nil request.accept.each do |mime_type| break if alt = wants.keys.detect {|k| ::Sinatra::Base.mime_type(k) == mime_type} end format alt if alt end raise UnhandledFormat if wants[format].nil? wants[format].call end
static_file?(path)
click to toggle source
This is mostly just a helper so request.path_info isn't changed when serving files from the public directory
# File lib/sinatra/respond_to.rb, line 183 def static_file?(path) public_dir = File.expand_path(settings.public_folder) path = File.expand_path(File.join(public_dir, unescape(path))) path[0, public_dir.length] == public_dir && File.file?(path) end
Private Instance Methods
render(*args, &block)
click to toggle source
Changes in 1.0 Sinatra reuse render for layout so we store the original value to tell us if this is an automatic attempt to do a layout call. If it is, it might fail with Errno::ENOENT and we want to pass that back to sinatra since it isn't a MissingTemplate error
Calls superclass method
# File lib/sinatra/respond_to.rb, line 151 def render(*args, &block) assumed_layout = args[1] == :layout args[1] = "#{args[1]}.#{format}".to_sym if args[1].is_a?(::Symbol) super *args, &block rescue Errno::ENOENT raise MissingTemplate, "#{args[1]}.#{args[0]}" unless assumed_layout raise # Reraise original error end