Module: Roda::RodaPlugins::StatusHandler::ClassMethods

Defined in:
lib/roda/plugins/status_handler.rb

Instance Method Summary collapse

Instance Method Details

#freezeObject

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



66
67
68
69
# File 'lib/roda/plugins/status_handler.rb', line 66

def freeze
  opts[:status_handler].freeze
  super
end

#status_handler(code, opts = OPTS, &block) ⇒ Object

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/roda/plugins/status_handler.rb', line 41

def status_handler(code, opts=OPTS, &block)
  # For backwards compatibility, pass request argument if block accepts argument
  arity = block.arity == 0 ? 0 : 1
  handle_headers = case keep_headers = opts[:keep_headers]
  when nil, false
    CLEAR_HEADERS
  when Array
    if Rack.release >= '3'
      keep_headers = keep_headers.map(&:downcase)
    end
    lambda{|headers| headers.delete_if{|k,_| !keep_headers.include?(k)}}
  else
    raise RodaError, "Invalid :keep_headers option"
  end

  meth = define_roda_method(:"_roda_status_handler__#{code}", arity, &block)
  self.opts[:status_handler][code] = define_roda_method(:"_roda_status_handler_#{code}", 1) do |result|
    res = @_response
    res.status = result[0]
    handle_headers.call(res.headers)
    result.replace(_roda_handle_route{arity == 1 ? send(meth, @_request) : send(meth)})
  end
end