Module: Roda::RodaPlugins::SinatraHelpers::RequestMethods
- Defined in:
- lib/roda/plugins/sinatra_helpers.rb
Instance Method Summary collapse
-
#back ⇒ Object
Alias for referrer.
-
#error(code = 500, body = nil) ⇒ Object
Halt processing and return the error status provided with the given code and optional body.
-
#not_found(body = nil) ⇒ Object
Halt processing and return a 404 response with an optional body.
-
#redirect(path = (no_add_script_name = true; default_redirect_path), status = default_redirect_status) ⇒ Object
If the absolute_redirects or :prefixed_redirects roda class options has been set, respect those and update the path.
-
#send_file(path, opts = OPTS) ⇒ Object
Use the contents of the file at
path
as the response body. -
#uri(addr = nil, absolute = true, add_script_name = true) ⇒ Object
(also: #url, #to)
Generates the absolute URI for a given path in the app.
Instance Method Details
#back ⇒ Object
Alias for referrer
288 289 290 |
# File 'lib/roda/plugins/sinatra_helpers.rb', line 288 def back referrer end |
#error(code = 500, body = nil) ⇒ Object
Halt processing and return the error status provided with the given code and optional body. If a single argument is given and it is not an integer, consider it the body and use a 500 status code.
296 297 298 299 300 301 302 303 304 305 |
# File 'lib/roda/plugins/sinatra_helpers.rb', line 296 def error(code=500, body = nil) unless code.is_a?(Integer) body = code code = 500 end response.status = code response.body = body if body halt end |
#not_found(body = nil) ⇒ Object
Halt processing and return a 404 response with an optional body.
308 309 310 |
# File 'lib/roda/plugins/sinatra_helpers.rb', line 308 def not_found(body = nil) error(404, body) end |
#redirect(path = (no_add_script_name = true; default_redirect_path), status = default_redirect_status) ⇒ Object
If the absolute_redirects or :prefixed_redirects roda class options has been set, respect those and update the path.
314 315 316 317 318 319 320 |
# File 'lib/roda/plugins/sinatra_helpers.rb', line 314 def redirect(path=(no_add_script_name = true; default_redirect_path), status=default_redirect_status) opts = roda_class.opts absolute_redirects = opts[:absolute_redirects] prefixed_redirects = no_add_script_name ? false : opts[:prefixed_redirects] path = uri(path, absolute_redirects, prefixed_redirects) if absolute_redirects || prefixed_redirects super(path, status) end |
#send_file(path, opts = OPTS) ⇒ Object
Use the contents of the file at path
as the response body. See plugin documentation for options.
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/roda/plugins/sinatra_helpers.rb', line 323 def send_file(path, opts = OPTS) res = response headers = res.headers if opts[:type] || !headers[CONTENT_TYPE] res.content_type(opts[:type] || ::File.extname(path), :default => OCTET_STREAM) end disposition = opts[:disposition] filename = opts[:filename] if disposition || filename disposition ||= ATTACHMENT filename = path if filename.nil? res.(filename, disposition) end if lm = opts[:last_modified] last_modified(lm) end file = ::Rack::File.new nil file.path = path s, h, b = file.serving(@env) res.status = opts[:status] || s headers.delete(CONTENT_LENGTH) headers.replace(h.merge!(headers)) res.body = b halt rescue Errno::ENOENT not_found end |
#uri(addr = nil, absolute = true, add_script_name = true) ⇒ Object Also known as: url, to
Generates the absolute URI for a given path in the app. Takes Rack routers and reverse proxies into account.
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/roda/plugins/sinatra_helpers.rb', line 358 def uri(addr = nil, absolute = true, add_script_name = true) addr = addr.to_s if addr return addr if addr =~ /\A[A-z][A-z0-9\+\.\-]*:/ uri = if absolute h = if @env.has_key?(HTTP_X_FORWARDED_HOST) || port != (ssl? ? 443 : 80) host_with_port else host end ["http#{'s' if ssl?}://#{h}"] else [EMPTY_STRING] end uri << script_name.to_s if add_script_name uri << (addr || path_info) File.join(uri) end |