module Roda::RodaPlugins::SinatraHelpers::RequestMethods

  1. lib/roda/plugins/sinatra_helpers.rb

Methods

Public Instance

  1. back
  2. error
  3. not_found
  4. redirect
  5. send_file
  6. uri

Public Instance Aliases

to -> uri
url -> uri

Public Instance methods

back()

Alias for referrer

[show source]
    # File lib/roda/plugins/sinatra_helpers.rb
266 def back
267   referrer
268 end
error(code=500, body = nil)

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.

[show source]
    # File lib/roda/plugins/sinatra_helpers.rb
274 def error(code=500, body = nil)
275   unless code.is_a?(Integer)
276     body = code
277     code = 500
278   end
279 
280   response.status = code
281   response.body = body if body
282   halt
283 end
not_found(body = nil)

Halt processing and return a 404 response with an optional body.

[show source]
    # File lib/roda/plugins/sinatra_helpers.rb
286 def not_found(body = nil)
287   error(404, body)
288 end
redirect(path=(no_add_script_name = true; default_redirect_path), status=default_redirect_status)

If the absolute_redirects or :prefixed_redirects roda class options has been set, respect those and update the path.

[show source]
    # File lib/roda/plugins/sinatra_helpers.rb
292 def redirect(path=(no_add_script_name = true; default_redirect_path), status=default_redirect_status)
293   absolute_redirects = roda_class.absolute_redirects?
294   prefixed_redirects = no_add_script_name ? false : roda_class.prefixed_redirects?
295   path = uri(path, absolute_redirects, prefixed_redirects) if absolute_redirects || prefixed_redirects
296   super(path, status)
297 end
send_file(path, opts = OPTS)

Backwards compatibility for callers of r.send_file.

[show source]
    # File lib/roda/plugins/sinatra_helpers.rb
300 def send_file(path, opts = OPTS)
301   scope.send_file(path, opts)
302 end
uri(addr = nil, absolute = true, add_script_name = true)

Generates the absolute URI for a given path in the app. Takes Rack routers and reverse proxies into account.

[show source]
    # File lib/roda/plugins/sinatra_helpers.rb
306 def uri(addr = nil, absolute = true, add_script_name = true)
307   addr = addr.to_s if addr
308   return addr if addr =~ /\A[A-z][A-z0-9\+\.\-]*:/
309   uri = if absolute
310     h = if @env.has_key?("HTTP_X_FORWARDED_HOST") || port != (ssl? ? 443 : 80)
311       host_with_port
312     else
313       host
314     end
315     ["http#{'s' if ssl?}://#{h}"]
316   else
317     ['']
318   end
319   uri << script_name.to_s if add_script_name
320   uri << (addr || path_info)
321   File.join(uri)
322 end