Module: Roda::RodaPlugins::SinatraHelpers
- Defined in:
- lib/roda/plugins/sinatra_helpers.rb
Overview
The sinatra_helpers plugin ports most of the helper methods defined in Sinatra::Helpers to Roda, other than those helpers that were already covered by other plugins such as caching and streaming.
Unlike Sinatra, the helper methods are added to either the request or response classes instead of directly to the scope of the route block. However, for consistency with Sinatra, delegate methods are added to the scope of the route block that call the methods on the request or response. If you do not want to pollute the namespace of the route block, you should load the plugin with the delegate: false
option:
plugin :sinatra_helpers, delegate: false
Class Methods Added
The only class method added by this plugin is mime_type
, which is a shortcut for retrieving or setting MIME types in Rack’s MIME database:
Roda.mime_type 'csv' # => 'text/csv'
Roda.mime_type 'foobar', 'application/foobar' # set
Request Methods Added
In addition to adding the following methods, this changes redirect
to use a 303 response status code by default for HTTP 1.1 non-GET requests, and to automatically use absolute URIs if the :absolute_redirects
Roda class option is true, and to automatically prefix redirect paths with the script name if the :prefixed_redirects
Roda class option is true.
When adding delegate methods, a logger method is added to the route block scope that calls the logger method on the request.
back
back
is an alias to referrer, so you can do:
redirect back
error
error
sets the response status code to 500 (or a status code you provide), and halts the request. It takes an optional body:
error # 500 response, empty boby
error 501 # 501 response, empty body
error 'b' # 500 response, 'b' body
error 501, 'b' # 501 response, 'b' body
not_found
not_found
sets the response status code to 404 and halts the request. It takes an optional body:
not_found # 404 response, empty body
not_found 'b' # 404 response, 'b' body
uri
uri
by default returns absolute URIs that are prefixed by the script name:
request.script_name # => '/foo'
uri '/bar' # => 'http://example.org/foo/bar'
You can turn of the absolute or script name prefixing if you want:
uri '/bar', false # => '/foo/bar'
uri '/bar', true, false # => 'http://example.org/bar'
uri '/bar', false, false # => '/bar'
This method is aliased as url
and to
.
send_file
This will serve the file with the given path from the file system:
send_file 'path/to/file.txt'
Options:
- :disposition
-
Set the Content-Disposition to the given disposition.
- :filename
-
Set the Content-Disposition to attachment (unless :disposition is set), and set the filename parameter to the value.
- :last_modified
-
Explicitly set the Last-Modified header to the given value, and return a not modified response if there has not been modified since the previous request. This option requires the caching plugin.
- :status
-
Override the status for the response.
- :type
-
Set the Content-Type to use for this response.
Response Methods Added
body
When called with an argument or block, body
sets the body, otherwise it returns the body:
body # => []
body('b') # set body to 'b'
body{'b'} # set body to 'b', but don't call until body is needed
body=
body
sets the body to the given value:
response.body = 'v'
This method is not delegated to the scope of the route block, call body
with an argument to set the value.
status
When called with an argument, status
sets the status, otherwise it returns the status:
status # => 200
status(301) # sets status to 301
headers
When called with an argument, headers
merges the given headers into the current headers, otherwise it returns the headers:
headers['Foo'] = 'Bar'
headers 'Foo' => 'Bar'
mime_type
mime_type
just calls the Roda class method to get the mime_type.
content_type
When called with an argument, content_type
sets the Content-Type based on the argument, otherwise it returns the Content-Type.
mime_type # => 'text/html'
mime_type 'csv' # set Content-Type to 'text/csv'
mime_type :csv # set Content-Type to 'text/csv'
mime_type '.csv' # set Content-Type to 'text/csv'
mime_type 'text/csv' # set Content-Type to 'text/csv'
Options:
- :charset
-
Set the charset for the mime type to the given charset, if the charset is not already set in the mime type.
- :default
-
Uses the given type if the mime type is not known. If this option is not used and the mime type is not known, an exception will be raised.
attachment
When called with no filename, attachment
just sets the Content-Disposition to attachment. When called with a filename, this sets the Content-Disposition to attachment with the appropriate filename parameter, and if the filename extension is recognized, this also sets the Content-Type to the appropriate MIME type if not already set.
# set Content-Disposition to 'attachment'
'a.csv' # set Content-Disposition to 'attachment;filename="a.csv"',
# also set Content-Type to 'text/csv'
status predicates
This adds the following predicate methods for checking the status:
informational? # 100-199
success? # 200-299
redirect? # 300-399
client_error? # 400-499
not_found? # 404
server_error? # 500-599
If the status has not yet been set for the response, these will return nil
.
License
The implementation was originally taken from Sinatra, which is also released under the MIT License:
Copyright © 2007, 2008, 2009 Blake Mizerany Copyright © 2010, 2011, 2012, 2013, 2014 Konstantin Haase
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Defined Under Namespace
Modules: ClassMethods, DelegateMethods, RequestMethods, ResponseMethods Classes: DelayedBody
Constant Summary collapse
- UTF8_ENCODING =
Encoding.find('UTF-8')
- ISO88591_ENCODING =
Encoding.find('ISO-8859-1')
- BINARY_ENCODING =
Encoding.find('BINARY')
- RACK_FILES =
defined?(Rack::Files) ? Rack::Files : Rack::File
Class Method Summary collapse
-
.configure(app, opts = OPTS) ⇒ Object
Add delegate methods to the route block scope calling request or response methods, unless the :delegate option is false.
-
.load_dependencies(app, _opts = nil) ⇒ Object
Depend on the status_303 plugin.
Class Method Details
.configure(app, opts = OPTS) ⇒ Object
Add delegate methods to the route block scope calling request or response methods, unless the :delegate option is false.
236 237 238 |
# File 'lib/roda/plugins/sinatra_helpers.rb', line 236 def self.configure(app, opts=OPTS) app.send(:include, DelegateMethods) unless opts[:delegate] == false end |
.load_dependencies(app, _opts = nil) ⇒ Object
Depend on the status_303 plugin.
229 230 231 |
# File 'lib/roda/plugins/sinatra_helpers.rb', line 229 def self.load_dependencies(app, _opts = nil) app.plugin :status_303 end |