Module: Sinatra::RespondTo::Helpers
- Defined in:
- lib/sinatra/respond_to.rb
Defined Under Namespace
Classes: Format
Class Method Summary collapse
-
.included(klass) ⇒ Object
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.
- .mime_type(sym) ⇒ Object
Instance Method Summary collapse
- #charset(val = nil) ⇒ Object
- #format(val = nil) ⇒ Object
- #match_accept_type(mime_types, format) ⇒ Object
- #respond_to {|wants| ... } ⇒ Object
-
#static_file?(path) ⇒ Boolean
This is mostly just a helper so request.path_info isn’t changed when serving files from the public directory.
Class Method Details
.included(klass) ⇒ Object
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
178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/sinatra/respond_to.rb', line 178 def self.included(klass) klass.class_eval do def content_type_with_save(*args) content_type_without_save *args @_format = args.first.to_sym response['Content-Type'] end alias_method :content_type_without_save, :content_type alias_method :content_type, :content_type_with_save end if ::Sinatra::VERSION =~ /^1.0/ end |
Instance Method Details
#charset(val = nil) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/sinatra/respond_to.rb', line 216 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 |
#format(val = nil) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/sinatra/respond_to.rb', line 194 def format(val=nil) unless val.nil? mime_type = ::Sinatra::RespondTo::Helpers.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'].sub!(/^[^;]+/, mime_type) charset .default_charset if Sinatra::RespondTo::TEXT_MIME_TYPES.include?(format) and format!=:png end @_format end |
#match_accept_type(mime_types, format) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/sinatra/respond_to.rb', line 237 def match_accept_type(mime_types, format) selected = [] accepted_types = mime_types.map {|type| Regexp.escape(type).gsub(/\\\*/,'.*') } # Fix for Chrome based browsers which returns XML when 'xhtml' is requested. if env['HTTP_USER_AGENT'] =~ /Chrome/ and accepted_types.size>1 accepted_types[0], accepted_types[1] = accepted_types[1], accepted_types[0] if accepted_types[0].eql?('application/xhtml\\+xml') accepted_types[0] = 'text/html' end end accepted_types.each do |at| format.each do |fmt, ht, handler| (selected = [fmt, ht, handler]) and break if ht.match(at) end break unless selected.empty? end selected end |
#respond_to {|wants| ... } ⇒ Object
228 229 230 231 232 233 234 235 |
# File 'lib/sinatra/respond_to.rb', line 228 def respond_to(&block) wants = Format.new yield wants fmt, type, handler = match_accept_type(accept_list, wants) raise UnhandledFormat if fmt.nil? format fmt handler.nil? ? nil : handler.call end |
#static_file?(path) ⇒ Boolean
This is mostly just a helper so request.path_info isn’t changed when serving files from the public directory
209 210 211 212 213 214 |
# File 'lib/sinatra/respond_to.rb', line 209 def static_file?(path) public_dir = File.(.public) path = File.(File.join(public_dir, unescape(path))) path[0, public_dir.length] == public_dir && File.file?(path) end |