wvanbergen-http_status_exceptions 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/README.rdoc +2 -0
  2. data/lib/http_status_exceptions.rb +19 -12
  3. metadata +2 -2
data/README.rdoc CHANGED
@@ -6,6 +6,8 @@ which a response will be send back to the client with the desired HTTP status, p
6
6
  You can use this plugin to access control mechanisms. You can simply raise a HTTPStatus::Forbidden if a user is not allowed to
7
7
  perform a certain action. A nice looking error page will be the result. See the example below
8
8
 
9
+ See the project wiki (http://github.com/wvanbergen/http_status_exceptions/wikis) for additional documentation.
10
+
9
11
  == Installation
10
12
 
11
13
  Installation is simple. Simply add the gem in your <tt>environment.rb</tt>:
@@ -1,27 +1,34 @@
1
1
  module HTTPStatus
2
2
 
3
3
  class Base < Exception
4
- attr_accessor :status
5
-
6
- cattr_reader :template_path
4
+
5
+ # The path from which the error documents are loaded.
6
+ cattr_accessor :template_path
7
7
  @@template_path = 'shared/http_status'
8
+
9
+ attr_reader :status, :details
8
10
 
9
- def initialize(message)
11
+ # Creates the exception with a message and some optional other info.
12
+ def initialize(message, details = nil)
10
13
  @status = self.class.to_s.split("::").last.underscore.to_sym
14
+ @details = details
11
15
  super(message)
12
16
  end
13
17
 
18
+ # The numeric status code corresponding to this exception
14
19
  def status_code
15
20
  ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE[@status]
16
21
  end
17
22
 
23
+ # The name of the template that should be used as error page for this exception
18
24
  def template
19
25
  "#{@@template_path}/#{@status}"
20
26
  end
21
27
  end
22
-
28
+
29
+ # Creates all the exception classes based on Rails's list of available status code and
30
+ # registers the exception handler using the rescue_from method.
23
31
  def self.included(base)
24
- # create all the classed
25
32
  ActionController::StatusCodes::STATUS_CODES.each do |code, name|
26
33
  const_set("#{name.to_s.gsub(/[^A-z]/, '').camelize}", Class.new(HTTPStatus::Base))
27
34
  end
@@ -29,14 +36,14 @@ module HTTPStatus
29
36
  base.send(:rescue_from, HTTPStatus::Base, :with => :http_status_exception)
30
37
  end
31
38
 
32
- # The default handler for raised HTTP status exceptions
39
+ # The default handler for raised HTTP status exceptions.
40
+ # It will render a template if available, or respond with an empty response
41
+ # with the HTTP status correspodning to the exception.
33
42
  def http_status_exception(exception)
34
43
  @exception = exception
35
- begin
36
- render(:template => exception.template, :status => exception.status)
37
- rescue ActionView::MissingTemplate
38
- head(exception.status)
39
- end
44
+ render(:template => exception.template, :status => exception.status)
45
+ rescue ActionView::MissingTemplate
46
+ head(exception.status)
40
47
  end
41
48
  end
42
49
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wvanbergen-http_status_exceptions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Willem van Bergen
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-09-20 00:00:00 -07:00
12
+ date: 2008-09-21 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15