wvanbergen-http_status_exceptions 0.1.0

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.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Willem van Bergen
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,40 @@
1
+ = HTTP status exception
2
+
3
+ This simple plugin will register exception classes for all HTTP status. These exceptions can then be raised from your controllers, after
4
+ which a response will be send back to the client with the desired HTTP status, possible with some other content.
5
+
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
+ perform a certain action. A nice looking error page will be the result. See the example below
8
+
9
+ == Installation
10
+
11
+ Installation is simple. Simply add the gem in your <tt>environment.rb</tt>:
12
+
13
+ Rails::Initializer.run do |config|
14
+ ...
15
+ config.gem 'wvanbergen-http_status_exceptions', :lib => 'http_status_exceptions', :source => 'http://gems.github.com'
16
+ end
17
+
18
+ Run <tt>rake gems:install</tt> to install the gem if needed.
19
+
20
+ == Usage
21
+
22
+ class BlogController < ApplicationController
23
+
24
+ def destroy
25
+ raise HTTPStatus::Forbidden, 'You cannot delete blogs!' unless current_user.can_delete_blogs?
26
+ @blog.destroy
27
+ end
28
+ end
29
+
30
+ By default, this will return an empty response with the "forbidden" status code (403). If you want to add content
31
+ to the response as well, create the following view: <tt>shared/http_status/forbidden.html.erb</tt>. You can use the
32
+ <tt>@exception</tt>-object in your view:
33
+
34
+ <h1>Forbidden</h1>
35
+ <p> <%= h(@exception.message) %> </p>
36
+ <hr />
37
+ <p>HTTP status code <small> <%= @exception.status_code %>: <%= @exception.status.to_s.humanize %></small></p>
38
+
39
+ The response will only be sent if the request format is HTML because of the name of the view file. In theory you
40
+ could make a response for XML requests as well by using <tt>shared/http_status/forbidden.xml.builder</tt> as filename
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'http_status_exceptions.rb'
@@ -0,0 +1,43 @@
1
+ module HTTPStatus
2
+
3
+ class Base < Exception
4
+ attr_accessor :status
5
+
6
+ cattr_reader :template_path
7
+ @@template_path = 'shared/http_status'
8
+
9
+ def initialize(message)
10
+ @status = self.class.to_s.split("::").last.underscore.to_sym
11
+ super(message)
12
+ end
13
+
14
+ def status_code
15
+ ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE[@status]
16
+ end
17
+
18
+ def template
19
+ "#{@@template_path}/#{@status_code}"
20
+ end
21
+ end
22
+
23
+ def self.included(base)
24
+ # create all the classed
25
+ ActionController::StatusCodes::STATUS_CODES.each do |code, name|
26
+ const_set("#{name.to_s.gsub(/[^A-z]/, '').camelize}", Class.new(HTTPStatus::Base))
27
+ end
28
+
29
+ base.send(:rescue_from, HTTPStatus::Base, :with => :http_status_exception)
30
+ end
31
+
32
+ # The default handler for raised HTTP status exceptions
33
+ def http_status_exception(exception)
34
+ @exception = exception
35
+ begin
36
+ render(:template => exception.template, :status => exception.status)
37
+ rescue ActionView::MissingTemplate
38
+ head(exception.status)
39
+ end
40
+ end
41
+ end
42
+
43
+ ActionController::Base.send(:include, HTTPStatus)
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wvanbergen-http_status_exceptions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Willem van Bergen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-09-20 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Clean up your controller code by raising exceptions that generate responses with different HTTP status codes.
17
+ email:
18
+ - willem@vanbergen.org
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - MIT-LICENSE
27
+ - README.rdoc
28
+ - init.rb
29
+ - lib
30
+ - lib/http_status_exceptions.rb
31
+ has_rdoc: false
32
+ homepage: http://github.com/wvanbergen/http_status_exceptions/wikis
33
+ post_install_message:
34
+ rdoc_options: []
35
+
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ version:
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ requirements: []
51
+
52
+ rubyforge_project:
53
+ rubygems_version: 1.2.0
54
+ signing_key:
55
+ specification_version: 2
56
+ summary: A Rails plugin to use exceptions for generating HTTP status responses
57
+ test_files: []
58
+