module Roda::RodaPlugins::StatusHandler::ClassMethods

  1. lib/roda/plugins/status_handler.rb

Methods

Public Instance

  1. freeze
  2. status_handler

Public Instance methods

freeze()

Freeze the hash of status handlers so that there can be no thread safety issues at runtime.

[show source]
   # File lib/roda/plugins/status_handler.rb
68 def freeze
69   status_handlers.freeze
70   super
71 end
status_handler(code, opts=OPTS, &block)

Install the given block as a status handler for the given HTTP response code.

[show source]
   # File lib/roda/plugins/status_handler.rb
43 def status_handler(code, opts=OPTS, &block)
44   # For backwards compatibility, pass request argument if block accepts argument
45   arity = block.arity == 0 ? 0 : 1
46   handle_headers = case keep_headers = opts[:keep_headers]
47   when nil, false
48     CLEAR_HEADERS
49   when Array
50     if Rack.release >= '3'
51       keep_headers = keep_headers.map(&:downcase)
52     end
53     lambda{|headers| headers.delete_if{|k,_| !keep_headers.include?(k)}}
54   else
55     raise RodaError, "Invalid :keep_headers option"
56   end
57 
58   meth = define_roda_method(:"_roda_status_handler__#{code}", arity, &block)
59   status_handlers[code] = define_roda_method(:"_roda_status_handler_#{code}", 1) do |result|
60     res = @_response
61     res.status = result[0]
62     handle_headers.call(res.headers)
63     result.replace(_roda_handle_route{arity == 1 ? send(meth, @_request) : send(meth)})
64   end
65 end