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
292 def back
293   referrer
294 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
300 def error(code=500, body = nil)
301   unless code.is_a?(Integer)
302     body = code
303     code = 500
304   end
305 
306   response.status = code
307   response.body = body if body
308   halt
309 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
312 def not_found(body = nil)
313   error(404, body)
314 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
318 def redirect(path=(no_add_script_name = true; default_redirect_path), status=default_redirect_status)
319   opts = roda_class.opts
320   absolute_redirects = opts[:absolute_redirects]
321   prefixed_redirects = no_add_script_name ? false : opts[:prefixed_redirects]
322   path = uri(path, absolute_redirects, prefixed_redirects) if absolute_redirects || prefixed_redirects
323   super(path, status)
324 end
send_file(path, opts = OPTS)

Use the contents of the file at path as the response body. See plugin documentation for options.

[show source]
    # File lib/roda/plugins/sinatra_helpers.rb
327 def send_file(path, opts = OPTS)
328   res = response
329   headers = res.headers
330   if opts[:type] || !headers[RodaResponseHeaders::CONTENT_TYPE]
331     res.content_type(opts[:type] || ::File.extname(path), :default => 'application/octet-stream')
332   end
333 
334   disposition = opts[:disposition]
335   filename    = opts[:filename]
336   if disposition || filename
337     disposition ||= 'attachment'
338     filename = path if filename.nil?
339     res.attachment(filename, disposition)
340   end
341 
342   if lm = opts[:last_modified]
343     last_modified(lm)
344   end
345 
346   file = RACK_FILES.new nil
347   s, h, b = if Rack.release > '2'
348     file.serving(self, path)
349   else
350     file.path = path
351     file.serving(@env)
352   end
353 
354   res.status = opts[:status] || s
355   headers.delete(RodaResponseHeaders::CONTENT_LENGTH)
356   headers.replace(h.merge!(headers))
357   res.body = b
358 
359   halt
360 rescue Errno::ENOENT
361   not_found
362 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
366 def uri(addr = nil, absolute = true, add_script_name = true)
367   addr = addr.to_s if addr
368   return addr if addr =~ /\A[A-z][A-z0-9\+\.\-]*:/
369   uri = if absolute
370     h = if @env.has_key?("HTTP_X_FORWARDED_HOST") || port != (ssl? ? 443 : 80)
371       host_with_port
372     else
373       host
374     end
375     ["http#{'s' if ssl?}://#{h}"]
376   else
377     ['']
378   end
379   uri << script_name.to_s if add_script_name
380   uri << (addr || path_info)
381   File.join(uri)
382 end