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
261 def back
262   referrer
263 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
269 def error(code=500, body = nil)
270   unless code.is_a?(Integer)
271     body = code
272     code = 500
273   end
274 
275   response.status = code
276   response.body = body if body
277   halt
278 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
281 def not_found(body = nil)
282   error(404, body)
283 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
287 def redirect(path=(no_add_script_name = true; default_redirect_path), status=default_redirect_status)
288   opts = roda_class.opts
289   absolute_redirects = opts[:absolute_redirects]
290   prefixed_redirects = no_add_script_name ? false : opts[:prefixed_redirects]
291   path = uri(path, absolute_redirects, prefixed_redirects) if absolute_redirects || prefixed_redirects
292   super(path, status)
293 end
send_file(path, opts = OPTS)

Backwards compatibility for callers of r.send_file.

[show source]
    # File lib/roda/plugins/sinatra_helpers.rb
296 def send_file(path, opts = OPTS)
297   scope.send_file(path, opts)
298 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
302 def uri(addr = nil, absolute = true, add_script_name = true)
303   addr = addr.to_s if addr
304   return addr if addr =~ /\A[A-z][A-z0-9\+\.\-]*:/
305   uri = if absolute
306     h = if @env.has_key?("HTTP_X_FORWARDED_HOST") || port != (ssl? ? 443 : 80)
307       host_with_port
308     else
309       host
310     end
311     ["http#{'s' if ssl?}://#{h}"]
312   else
313     ['']
314   end
315   uri << script_name.to_s if add_script_name
316   uri << (addr || path_info)
317   File.join(uri)
318 end