Module: Sinatra::Helpers
- Included in:
- Base
- Defined in:
- lib/sinatra/base.rb
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.
-
#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 as the response body and attempt to.
-
#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.
120 121 122 123 124 125 126 |
# File 'lib/sinatra/base.rb', line 120 def (filename=nil) response['Content-Disposition'] = 'attachment' if filename params = '; filename="%s"' % File.basename(filename) response['Content-Disposition'] << params end 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.
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/sinatra/base.rb', line 107 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.
175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/sinatra/base.rb', line 175 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.
158 159 160 161 162 163 164 |
# File 'lib/sinatra/base.rb', line 158 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.
101 102 103 |
# File 'lib/sinatra/base.rb', line 101 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 as the response body and attempt to
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/sinatra/base.rb', line 129 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 halt StaticFile.open(path, 'rb') rescue Errno::ENOENT not_found end |
#session ⇒ Object
Access the underlying Rack session.
96 97 98 |
# File 'lib/sinatra/base.rb', line 96 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 |