Module: Strelka::App::Errors::ClassMethods

Defined in:
lib/strelka/app/errors.rb

Overview

Class-level functionality

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#status_handlersObject (readonly)

The registered status handler callbacks, keyed by Integer Ranges of status codes to which they apply



99
100
101
# File 'lib/strelka/app/errors.rb', line 99

def status_handlers
  @status_handlers
end

Instance Method Details

#inherited(subclass) ⇒ Object

Extension callback -- add instance variables to extending objects.



103
104
105
106
# File 'lib/strelka/app/errors.rb', line 103

def inherited( subclass )
	super
	subclass.instance_variable_set( :@status_handlers, @status_handlers.dup )
end

#on_status(range = DEFAULT_HANDLER_STATUS_RANGE, template = nil, &block) ⇒ Object

Register a callback for responses whose status code is within the specified range. Range can either be a single integer HTTP status code, or a Range of the same (e.g., 400..499) for all statuses with that range.

If no range is specified, any of the HTTP error statuses will invoke the callback.

The block will be called with the response object (a subclass of Mongrel2::Response appropriate for the request type), and a hash of status info that will at least contain the following keys:

[+:status+] the HTTP status code that was passed to Strelka::App#finish_with [+:message+] the message string that was passed to Strelka::App#finish_with



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/strelka/app/errors.rb', line 123

def on_status( range=DEFAULT_HANDLER_STATUS_RANGE, template=nil, &block )
	range = Range.new( range, range ) unless range.is_a?( Range )
	methodname = "for_status_%s" % [ range.begin, range.end ].uniq.join('_to_')

	if template
		raise ArgumentError, "template-style callbacks don't take a block" if block
		raise ScriptError, "template-style callbacks require the :templating plugin" unless
			self.respond_to?( :templates )

		block = Proc.new {|*| template }
	end

	define_method( methodname, &block )

	self.status_handlers[ range ] = instance_method( methodname )
end