Module: HTTPStatus
- Defined in:
- lib/http_status_exceptions.rb
Overview
The HTTPStatus module is the core of the http_status_exceptions gem and contains all functionality.
The module contains HTTPStatus::Base class, which is used as a superclass for every HTTPStatus exception. Subclasses, like HTTPStatus::Forbidden or HTTPStatus::NotFound will be generated on demand by the HTTPStatus.const_missing method.
Moreover, it contains methods to handle these exceptions and integrate this functionality into ActionController::Base. When this module is in included in the ActionController::Base class, it will call rescue_from on it to handle all HTTPStatus::Base exceptions with the HTTPStatus#http_status_exceptions method.
The exception handler will try to render a response with the correct HTTPStatus. When no suitable template is found to render the exception with, it will simply respond with an empty HTTP status code.
Defined Under Namespace
Classes: Base
Constant Summary collapse
- VERSION =
The current gem release version. Do not set this value by hand, it will be done automatically by them gem release script.
"0.2.0"
Class Method Summary collapse
-
.const_missing(const) ⇒ Object
Generates a
HTTPStatus::Basesubclass on demand based on the constant name. -
.included(base) ⇒ Object
This function will install a rescue_from handler for HTTPStatus::Base exceptions in the class in which this module is included.
Instance Method Summary collapse
-
#http_status_exception(exception) ⇒ Object
The default handler for raised HTTP status exceptions.
Class Method Details
.const_missing(const) ⇒ Object
Generates a HTTPStatus::Base subclass on demand based on the constant name. The constant name should correspond to one of the status symbols defined in ActionController::StatusCodes. The function will raise an exception if the constant name cannot be mapped onto one of the status symbols.
This method will create a new subclass of HTTPStatus::Base and overrides the status class method of the class to return the correct status symbol.
const-
The name of the missing constant, for which an exception
class should be generated.
116 117 118 119 120 121 122 123 124 |
# File 'lib/http_status_exceptions.rb', line 116 def self.const_missing(const) status_symbol = const.to_s.underscore.to_sym raise "Unrecognized HTTP Status name!" unless ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE.has_key?(status_symbol) klass = Class.new(HTTPStatus::Base) klass.cattr_accessor(:status) klass.status = status_symbol const_set(const, klass) return const_get(const) end |
.included(base) ⇒ Object
This function will install a rescue_from handler for HTTPStatus::Base exceptions in the class in which this module is included.
base-
The class in which the module is included. Should be
ActionController::Base during the initialization of the gem.
100 101 102 |
# File 'lib/http_status_exceptions.rb', line 100 def self.included(base) base.send(:rescue_from, HTTPStatus::Base, :with => :http_status_exception) end |
Instance Method Details
#http_status_exception(exception) ⇒ Object
The default handler for raised HTTP status exceptions. It will render a template if available, or respond with an empty response with the HTTP status corresponding to the exception.
You can override this method in your ApplicationController to handle the exceptions yourself.
exception-
The HTTP status exception to handle.
134 135 136 137 138 139 140 141 |
# File 'lib/http_status_exceptions.rb', line 134 def http_status_exception(exception) @exception = exception = {:template => exception.template, :status => exception.status} [:layout] = exception.template_layout if exception.template_layout render() rescue ActionView::MissingTemplate head(exception.status) end |