Module Sinatra::ResponseHelpers

  1. lib/sinatra.rb

Helper methods for building various aspects of the HTTP response.

Public instance methods

content_type (type, params={})

Set the content type of the response body (HTTP ‘Content-Type’ header).

The type argument may be an internet media type (e.g., ‘text/html’, ‘application/xml+atom’, ‘image/png’) or a Symbol key into the Rack::File::MIME_TYPES table.

Media type parameters, such as “charset”, may also be specified using the optional hash argument:

get '/foo.html' do
  content_type 'text/html', :charset => 'utf-8'
  "<h1>Hello World</h1>"
end
[show source]
     # File lib/sinatra.rb, line 451
451:     def content_type(type, params={})
452:       type = Rack::File::MIME_TYPES[type.to_s] if type.kind_of?(Symbol)
453:       fail "Invalid or undefined media_type: #{type}" if type.nil?
454:       if params.any?
455:         params = params.collect { |kv| "%s=%s" % kv }.join(', ')
456:         type = [ type, params ].join(";")
457:       end
458:       response.header['Content-Type'] = type
459:     end
entity_tag (value, strength=:strong)

Set the response entity tag (HTTP ‘ETag’ header) and halt if conditional GET matches. The value argument is an identifier that uniquely identifies the current version of the resource. The strength argument indicates whether the etag should be used as a :strong (default) or :weak cache validator.

When the current request includes an ‘If-None-Match’ header with a matching etag, execution is immediately halted. If the request method is GET or HEAD, a ‘304 Not Modified’ response is sent. For all other request methods, a ‘412 Precondition Failed’ response is sent.

Calling this method before perfoming heavy processing (e.g., lengthy database queries, template rendering, complex logic) can dramatically increase overall throughput with caching clients.

See Also

RFC2616: ETag, ResponseHelpers#last_modified

[show source]
     # File lib/sinatra.rb, line 498
498:     def entity_tag(value, strength=:strong)
499:       value =
500:         case strength
501:         when :strong then '"%s"' % value
502:         when :weak   then 'W/"%s"' % value
503:         else         raise TypeError, "strength must be one of :strong or :weak"
504:         end
505:       response.header['ETag'] = value
506: 
507:       # Check for If-None-Match request header and halt if match is found.
508:       etags = (request.env['HTTP_IF_NONE_MATCH'] || '').split(/\s*,\s*/)
509:       if etags.include?(value) || etags.include?('*')
510:         # GET/HEAD requests: send Not Modified response
511:         throw :halt, 304 if request.get? || request.head?
512:         # Other requests: send Precondition Failed response
513:         throw :halt, 412
514:       end
515:     end
etag (value, strength=:strong)

Alias for entity_tag

header (header = nil)

Alias for headers

headers (header = nil)

Access or modify response headers. With no argument, return the underlying headers Hash. With a Hash argument, add or overwrite existing response headers with the values provided:

headers 'Content-Type' => "text/html;charset=utf-8",
  'Last-Modified' => Time.now.httpdate,
  'X-UA-Compatible' => 'IE=edge'

This method also available in singular form (header).

[show source]
     # File lib/sinatra.rb, line 431
431:     def headers(header = nil)
432:       @response.headers.merge!(header) if header
433:       @response.headers
434:     end
last_modified (time)

Set the last modified time of the resource (HTTP ‘Last-Modified’ header) and halt if conditional GET matches. The time argument is a Time, DateTime, or other object that responds to to_time.

When the current request includes an ‘If-Modified-Since’ header that matches the time specified, execution is immediately halted with a ‘304 Not Modified’ response.

Calling this method before perfoming heavy processing (e.g., lengthy database queries, template rendering, complex logic) can dramatically increase overall throughput with caching clients.

[show source]
     # File lib/sinatra.rb, line 472
472:     def last_modified(time)
473:       time = time.to_time if time.respond_to?(:to_time)
474:       time = time.httpdate if time.respond_to?(:httpdate)
475:       response.header['Last-Modified'] = time
476:       throw :halt, 304 if time == request.env['HTTP_IF_MODIFIED_SINCE']
477:       time
478:     end
redirect (path, *args)

Immediately halt response execution by redirecting to the resource specified. The path argument may be an absolute URL or a path relative to the site root. Additional arguments are passed to the halt.

With no integer status code, a ‘302 Temporary Redirect’ response is sent. To send a permanent redirect, pass an explicit status code of 301:

redirect '/somewhere/else', 301

NOTE: No attempt is made to rewrite the path based on application context. The ‘Location’ response header is set verbatim to the value provided.

[show source]
     # File lib/sinatra.rb, line 416
416:     def redirect(path, *args)
417:       status(302)
418:       header 'Location' => path
419:       throw :halt, *args
420:     end