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
66 def freeze
67   opts[:status_handler].freeze
68   super
69 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
41 def status_handler(code, opts=OPTS, &block)
42   # For backwards compatibility, pass request argument if block accepts argument
43   arity = block.arity == 0 ? 0 : 1
44   handle_headers = case keep_headers = opts[:keep_headers]
45   when nil, false
46     CLEAR_HEADERS
47   when Array
48     if Rack.release >= '3'
49       keep_headers = keep_headers.map(&:downcase)
50     end
51     lambda{|headers| headers.delete_if{|k,_| !keep_headers.include?(k)}}
52   else
53     raise RodaError, "Invalid :keep_headers option"
54   end
55 
56   meth = define_roda_method(:"_roda_status_handler__#{code}", arity, &block)
57   self.opts[:status_handler][code] = define_roda_method(:"_roda_status_handler_#{code}", 1) do |result|
58     res = @_response
59     res.status = result[0]
60     handle_headers.call(res.headers)
61     result.replace(_roda_handle_route{arity == 1 ? send(meth, @_request) : send(meth)})
62   end
63 end