Module: Sinatra::Helpers
- Included in:
- Base
- Defined in:
- lib/sinatra/base.rb
Overview
Methods available to routes, before filters, and views.
Defined Under Namespace
Classes: StaticFile
Instance Method Summary collapse
-
#attachment(filename = nil) ⇒ Object
Set the Content-Disposition to "attachment" with the specified filename, instructing the user agents to prompt to save.
-
#back ⇒ Object
Sugar for redirect (example: redirect back).
-
#body(value = nil, &block) ⇒ Object
Set or retrieve the response body.
-
#content_type(type, params = {}) ⇒ Object
Set the Content-Type of the response body given a media type or file extension.
-
#error(code, body = nil) ⇒ Object
Halt processing and return the error status provided.
-
#etag(value, kind = :strong) ⇒ Object
Set the response entity tag (HTTP 'ETag' header) and halt if conditional GET matches.
-
#headers(hash = nil) ⇒ Object
Set multiple response headers with Hash.
-
#last_modified(time) ⇒ Object
Set the last modified time of the resource (HTTP 'Last-Modified' header) and halt if conditional GET matches.
-
#media_type(type) ⇒ Object
Look up a media type by file extension in Rack's mime registry.
-
#not_found(body = nil) ⇒ Object
Halt processing and return a 404 Not Found.
-
#redirect(uri, *args) ⇒ Object
Halt processing and redirect to the URI provided.
-
#send_file(path, opts = {}) ⇒ Object
Use the contents of the file at
pathas the response body. -
#session ⇒ Object
Access the underlying Rack session.
-
#status(value = nil) ⇒ Object
Set or retrieve the response status code.
Instance Method Details
#attachment(filename = nil) ⇒ Object
Set the Content-Disposition to "attachment" with the specified filename, instructing the user agents to prompt to save.
126 127 128 129 130 131 132 |
# File 'lib/sinatra/base.rb', line 126 def (filename=nil) response['Content-Disposition'] = 'attachment' if filename params = '; filename="%s"' % File.basename(filename) response['Content-Disposition'] << params end end |
#back ⇒ Object
Sugar for redirect (example: redirect back)
207 |
# File 'lib/sinatra/base.rb', line 207 def back ; request.referer ; end |
#body(value = nil, &block) ⇒ Object
Set or retrieve the response body. When a block is given, evaluation is deferred until the body is read with #each.
67 68 69 70 71 72 73 74 |
# File 'lib/sinatra/base.rb', line 67 def body(value=nil, &block) if block_given? def block.each ; yield call ; end response.body = block else response.body = value end end |
#content_type(type, params = {}) ⇒ Object
Set the Content-Type of the response body given a media type or file extension.
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/sinatra/base.rb', line 113 def content_type(type, params={}) media_type = self.media_type(type) fail "Unknown media type: %p" % type if media_type.nil? if params.any? params = params.collect { |kv| "%s=%s" % kv }.join(', ') response['Content-Type'] = [media_type, params].join(";") else response['Content-Type'] = media_type end end |
#error(code, body = nil) ⇒ Object
Halt processing and return the error status provided.
84 85 86 87 88 |
# File 'lib/sinatra/base.rb', line 84 def error(code, body=nil) code, body = 500, code.to_str if code.respond_to? :to_str response.body = body unless body.nil? halt code end |
#etag(value, kind = :strong) ⇒ Object
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.
193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/sinatra/base.rb', line 193 def etag(value, kind=:strong) raise TypeError, ":strong or :weak expected" if ![:strong,:weak].include?(kind) value = '"%s"' % value value = 'W/' + value if kind == :weak response['ETag'] = value # Conditional GET check if = env['HTTP_IF_NONE_MATCH'] = .split(/\s*,\s*/) halt 304 if .include?(value) || .include?('*') end end |
#headers(hash = nil) ⇒ Object
Set multiple response headers with Hash.
96 97 98 99 |
# File 'lib/sinatra/base.rb', line 96 def headers(hash=nil) response.headers.merge! hash if hash response.headers end |
#last_modified(time) ⇒ Object
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.
176 177 178 179 180 181 182 |
# File 'lib/sinatra/base.rb', line 176 def last_modified(time) time = time.to_time if time.respond_to?(:to_time) time = time.httpdate if time.respond_to?(:httpdate) response['Last-Modified'] = time halt 304 if time == request.env['HTTP_IF_MODIFIED_SINCE'] time end |
#media_type(type) ⇒ Object
Look up a media type by file extension in Rack's mime registry.
107 108 109 |
# File 'lib/sinatra/base.rb', line 107 def media_type(type) Base.media_type(type) end |
#not_found(body = nil) ⇒ Object
Halt processing and return a 404 Not Found.
91 92 93 |
# File 'lib/sinatra/base.rb', line 91 def not_found(body=nil) error 404, body end |
#redirect(uri, *args) ⇒ Object
Halt processing and redirect to the URI provided.
77 78 79 80 81 |
# File 'lib/sinatra/base.rb', line 77 def redirect(uri, *args) status 302 response['Location'] = uri halt(*args) end |
#send_file(path, opts = {}) ⇒ Object
Use the contents of the file at path as the response body.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/sinatra/base.rb', line 135 def send_file(path, opts={}) stat = File.stat(path) last_modified stat.mtime content_type media_type(opts[:type]) || media_type(File.extname(path)) || response['Content-Type'] || 'application/octet-stream' response['Content-Length'] ||= (opts[:length] || stat.size).to_s if opts[:disposition] == 'attachment' || opts[:filename] opts[:filename] || path elsif opts[:disposition] == 'inline' response['Content-Disposition'] = 'inline' end halt StaticFile.open(path, 'rb') rescue Errno::ENOENT not_found end |
#session ⇒ Object
Access the underlying Rack session.
102 103 104 |
# File 'lib/sinatra/base.rb', line 102 def session env['rack.session'] ||= {} end |
#status(value = nil) ⇒ Object
Set or retrieve the response status code.
60 61 62 63 |
# File 'lib/sinatra/base.rb', line 60 def status(value=nil) response.status = value if value response.status end |