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.
-
#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
path
as 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.
129 130 131 132 133 134 135 |
# File 'lib/sinatra/base.rb', line 129 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)
208 |
# File 'lib/sinatra/base.rb', line 208 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.
76 77 78 79 80 81 82 83 |
# File 'lib/sinatra/base.rb', line 76 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.
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/sinatra/base.rb', line 116 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.
93 94 95 96 97 |
# File 'lib/sinatra/base.rb', line 93 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.
194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/sinatra/base.rb', line 194 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 |
#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.
177 178 179 180 181 182 183 |
# File 'lib/sinatra/base.rb', line 177 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.
110 111 112 |
# File 'lib/sinatra/base.rb', line 110 def media_type(type) Base.media_type(type) end |
#not_found(body = nil) ⇒ Object
Halt processing and return a 404 Not Found.
100 101 102 |
# File 'lib/sinatra/base.rb', line 100 def not_found(body=nil) error 404, body end |
#redirect(uri, *args) ⇒ Object
Halt processing and redirect to the URI provided.
86 87 88 89 90 |
# File 'lib/sinatra/base.rb', line 86 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.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/sinatra/base.rb', line 138 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.
105 106 107 |
# File 'lib/sinatra/base.rb', line 105 def session env['rack.session'] ||= {} end |
#status(value = nil) ⇒ Object
Set or retrieve the response status code.
69 70 71 72 |
# File 'lib/sinatra/base.rb', line 69 def status(value=nil) response.status = value if value response.status end |