wvanbergen-http_status_exceptions 0.1.7 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +5 -5
- data/README.rdoc +7 -3
- data/http_status_exceptions.gemspec +9 -4
- data/lib/http_status_exceptions.rb +78 -23
- data/spec/http_status_exception_spec.rb +12 -8
- data/tasks/github-gem.rake +10 -2
- metadata +8 -7
data/MIT-LICENSE
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
Copyright (c) 2008 Willem van Bergen
|
2
|
-
|
1
|
+
Copyright (c) 2008-2009 Willem van Bergen
|
2
|
+
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining
|
4
4
|
a copy of this software and associated documentation files (the
|
5
5
|
"Software"), to deal in the Software without restriction, including
|
@@ -7,14 +7,14 @@ without limitation the rights to use, copy, modify, merge, publish,
|
|
7
7
|
distribute, sublicense, and/or sell copies of the Software, and to
|
8
8
|
permit persons to whom the Software is furnished to do so, subject to
|
9
9
|
the following conditions:
|
10
|
-
|
10
|
+
|
11
11
|
The above copyright notice and this permission notice shall be
|
12
12
|
included in all copies or substantial portions of the Software.
|
13
|
-
|
13
|
+
|
14
14
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
15
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
16
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
17
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
18
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
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.
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
<b>WARNING:</b> the gem version that Github currently serves is faulty. The issue
|
2
|
+
is already fixed in the repository, but Github will not build new gems anymore. Make
|
3
|
+
sure to install the latest version from the gemcutter repository (see below)
|
4
|
+
|
1
5
|
= HTTP status exception
|
2
6
|
|
3
7
|
This simple plugin will register exception classes for all HTTP status. These exceptions can then be raised from your controllers, after
|
@@ -10,11 +14,11 @@ See the project wiki (http://github.com/wvanbergen/http_status_exceptions/wikis)
|
|
10
14
|
|
11
15
|
== Installation
|
12
16
|
|
13
|
-
Installation is simple. Simply add the gem in your <tt>environment.rb</tt>:
|
17
|
+
Installation is simple. Simply add the gem to the configuration in your <tt>environment.rb</tt>:
|
14
18
|
|
15
19
|
Rails::Initializer.run do |config|
|
16
20
|
...
|
17
|
-
config.gem '
|
21
|
+
config.gem 'http_status_exceptions', :source => 'http://gemcutter.org'
|
18
22
|
end
|
19
23
|
|
20
24
|
Run <tt>rake gems:install</tt> to install the gem if needed.
|
@@ -40,7 +44,7 @@ If you don't set a template_layout the current layout for the requested action w
|
|
40
44
|
== Usage
|
41
45
|
|
42
46
|
class BlogController < ApplicationController
|
43
|
-
|
47
|
+
|
44
48
|
def destroy
|
45
49
|
raise HTTPStatus::Forbidden, 'You cannot delete blogs!' unless current_user.can_delete_blogs?
|
46
50
|
@blog.destroy
|
@@ -1,18 +1,23 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
|
-
s.name = 'http_status_exceptions'
|
3
|
-
|
4
|
-
|
2
|
+
s.name = 'wvanbergen-http_status_exceptions'
|
3
|
+
|
4
|
+
# Do not update the version and date values by hand.
|
5
|
+
# This will be done automatically by the gem release script.
|
6
|
+
s.version = "0.2.0"
|
7
|
+
s.date = "2009-10-07"
|
5
8
|
|
6
9
|
s.summary = "A Rails plugin to use exceptions for generating HTTP status responses"
|
7
10
|
s.description = "Clean up your controller code by raising exceptions that generate responses with different HTTP status codes."
|
8
11
|
|
9
|
-
s.add_runtime_dependency('
|
12
|
+
s.add_runtime_dependency('actionpack', '>= 2.1.0')
|
10
13
|
s.add_development_dependency('rspec')
|
11
14
|
|
12
15
|
s.authors = ['Willem van Bergen']
|
13
16
|
s.email = ['willem@vanbergen.org']
|
14
17
|
s.homepage = 'http://github.com/wvanbergen/http_status_exceptions/wikis'
|
15
18
|
|
19
|
+
# Do not update the files and test_files values by hand.
|
20
|
+
# This will be done automatically by the gem release script.
|
16
21
|
s.files = %w(spec/spec_helper.rb http_status_exceptions.gemspec .gitignore init.rb lib/http_status_exceptions.rb Rakefile MIT-LICENSE tasks/github-gem.rake README.rdoc spec/http_status_exception_spec.rb)
|
17
22
|
s.test_files = %w(spec/http_status_exception_spec.rb)
|
18
23
|
end
|
@@ -1,8 +1,34 @@
|
|
1
|
+
# The HTTPStatus module is the core of the http_status_exceptions gem and
|
2
|
+
# contains all functionality.
|
3
|
+
#
|
4
|
+
# The module contains <tt>HTTPStatus::Base</tt> class, which is used as a
|
5
|
+
# superclass for every HTTPStatus exception. Subclasses, like
|
6
|
+
# <tt>HTTPStatus::Forbidden</tt> or <tt>HTTPStatus::NotFound</tt> will be
|
7
|
+
# generated on demand by the <tt>HTTPStatus.const_missing</tt> method.
|
8
|
+
#
|
9
|
+
# Moreover, it contains methods to handle these exceptions and integrate this
|
10
|
+
# functionality into <tt>ActionController::Base</tt>. When this module is in
|
11
|
+
# included in the <tt>ActionController::Base</tt> class, it will call
|
12
|
+
# <tt>rescue_from</tt> on it to handle all <tt>HTTPStatus::Base</tt>
|
13
|
+
# exceptions with the <tt>HTTPStatus#http_status_exceptions</tt> method.
|
14
|
+
#
|
15
|
+
# The exception handler will try to render a response with the correct
|
16
|
+
# HTTPStatus. When no suitable template is found to render the exception with,
|
17
|
+
# it will simply respond with an empty HTTP status code.
|
1
18
|
module HTTPStatus
|
2
19
|
|
20
|
+
# The current gem release version. Do not set this value by hand, it will
|
21
|
+
# be done automatically by them gem release script.
|
22
|
+
VERSION = "0.2.0"
|
23
|
+
|
3
24
|
# The Base HTTP status exception class is used as superclass for every
|
4
|
-
# exception class that is constructed. It implements some shared
|
5
|
-
# for finding the status code and determining the template
|
25
|
+
# exception class that is constructed. It implements some shared
|
26
|
+
# functionality for finding the status code and determining the template
|
27
|
+
# path to render.
|
28
|
+
#
|
29
|
+
# Subclasses of this class will be generated on demand when a non-exisiting
|
30
|
+
# constant of the <tt>HTTPStatus</tt> module is requested. This is
|
31
|
+
# implemented in the <tt>HTTPStatus.const_missing</tt> method.
|
6
32
|
class Base < StandardError
|
7
33
|
|
8
34
|
# The path from which the error documents are loaded.
|
@@ -11,59 +37,82 @@ module HTTPStatus
|
|
11
37
|
|
12
38
|
# The layout in which the error documents are rendered
|
13
39
|
cattr_accessor :template_layout
|
14
|
-
@@
|
40
|
+
@@template_layout = nil # Use the standard layout template setting by default.
|
15
41
|
|
16
42
|
attr_reader :details
|
17
43
|
|
18
|
-
#
|
44
|
+
# Initializes the exception instance.
|
45
|
+
# <tt>message</tt>:: The exception message.
|
46
|
+
# <tt>details</tt>:: An object with details about the exception.
|
19
47
|
def initialize(message = nil, details = nil)
|
20
48
|
@details = details
|
21
49
|
super(message)
|
22
50
|
end
|
23
51
|
|
24
|
-
# Returns the HTTP status symbol
|
25
|
-
#
|
52
|
+
# Returns the HTTP status symbol corresponding to this class. This is one
|
53
|
+
# of the symbols that can be found in the map that can be found in
|
54
|
+
# <tt>ActionController::StatusCodes</tt>.
|
55
|
+
#
|
56
|
+
# This method should be overridden by subclasses, as it returns
|
57
|
+
# <tt>:internal_server_error</tt> by default. This is done automatically
|
58
|
+
# when a new exception class is being generated by
|
59
|
+
# <tt>HTTPStatus.const_missing</tt>.
|
26
60
|
def self.status
|
27
61
|
:internal_server_error
|
28
62
|
end
|
29
63
|
|
30
|
-
# Returns the HTTP status symbol (as defined by Rails) corresponding to
|
31
|
-
# By default, it calls the class method of the same name.
|
64
|
+
# Returns the HTTP status symbol (as defined by Rails) corresponding to
|
65
|
+
# this instance. By default, it calls the class method of the same name.
|
32
66
|
def status
|
33
67
|
self.class.status
|
34
68
|
end
|
35
69
|
|
36
|
-
# The numeric status code corresponding to this exception class.
|
37
|
-
#
|
70
|
+
# The numeric status code corresponding to this exception class. Uses the
|
71
|
+
# status symbol to code map in <tt>ActionController::StatusCodes</tt>.
|
38
72
|
def self.status_code
|
39
73
|
ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE[self.status]
|
40
74
|
end
|
41
75
|
|
42
|
-
# The numeric status code corresponding to this exception.
|
43
|
-
#
|
76
|
+
# The numeric status code corresponding to this exception. By default, it
|
77
|
+
# calls the class method of the same name.
|
44
78
|
def status_code
|
45
79
|
self.class.status_code
|
46
80
|
end
|
47
81
|
|
48
|
-
# The name of the template that should be used as error page for this
|
82
|
+
# The name of the template that should be used as error page for this
|
83
|
+
# exception class.
|
49
84
|
def self.template
|
50
85
|
"#{template_path}/#{status}"
|
51
86
|
end
|
52
87
|
|
53
|
-
# The name of the template that should be used as error page for this
|
54
|
-
# By default, it calls the class method of the same name.
|
88
|
+
# The name of the template that should be used as error page for this
|
89
|
+
# exception. By default, it calls the class method of the same name.
|
55
90
|
def template
|
56
91
|
self.class.template
|
57
92
|
end
|
58
93
|
end
|
59
94
|
|
60
|
-
#
|
61
|
-
#
|
95
|
+
# This function will install a rescue_from handler for HTTPStatus::Base
|
96
|
+
# exceptions in the class in which this module is included.
|
97
|
+
#
|
98
|
+
# <tt>base</tt>:: The class in which the module is included. Should be
|
99
|
+
# <tt>ActionController::Base</tt> during the initialization of the gem.
|
62
100
|
def self.included(base)
|
63
101
|
base.send(:rescue_from, HTTPStatus::Base, :with => :http_status_exception)
|
64
102
|
end
|
65
103
|
|
66
|
-
# Generates a HTTPStatus::Base subclass
|
104
|
+
# Generates a <tt>HTTPStatus::Base</tt> subclass on demand based on the
|
105
|
+
# constant name. The constant name should correspond to one of the status
|
106
|
+
# symbols defined in <tt>ActionController::StatusCodes</tt>. The function
|
107
|
+
# will raise an exception if the constant name cannot be mapped onto one of
|
108
|
+
# the status symbols.
|
109
|
+
#
|
110
|
+
# This method will create a new subclass of <tt>HTTPStatus::Base</tt> and
|
111
|
+
# overrides the status class method of the class to return the correct
|
112
|
+
# status symbol.
|
113
|
+
#
|
114
|
+
# <tt>const</tt>:: The name of the missing constant, for which an exception
|
115
|
+
# class should be generated.
|
67
116
|
def self.const_missing(const)
|
68
117
|
status_symbol = const.to_s.underscore.to_sym
|
69
118
|
raise "Unrecognized HTTP Status name!" unless ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE.has_key?(status_symbol)
|
@@ -74,9 +123,14 @@ module HTTPStatus
|
|
74
123
|
return const_get(const)
|
75
124
|
end
|
76
125
|
|
77
|
-
# The default handler for raised HTTP status exceptions.
|
78
|
-
#
|
79
|
-
#
|
126
|
+
# The default handler for raised HTTP status exceptions. It will render a
|
127
|
+
# template if available, or respond with an empty response with the HTTP
|
128
|
+
# status corresponding to the exception.
|
129
|
+
#
|
130
|
+
# You can override this method in your <tt>ApplicationController</tt> to
|
131
|
+
# handle the exceptions yourself.
|
132
|
+
#
|
133
|
+
# <tt>exception</tt>:: The HTTP status exception to handle.
|
80
134
|
def http_status_exception(exception)
|
81
135
|
@exception = exception
|
82
136
|
render_options = {:template => exception.template, :status => exception.status}
|
@@ -87,5 +141,6 @@ module HTTPStatus
|
|
87
141
|
end
|
88
142
|
end
|
89
143
|
|
90
|
-
# Include the HTTPStatus module into ActionController to enable
|
91
|
-
|
144
|
+
# Include the HTTPStatus module into <tt>ActionController::Base</tt> to enable
|
145
|
+
# the <tt>http_status_exception</tt> exception handler.
|
146
|
+
ActionController::Base.send(:include, HTTPStatus)
|
@@ -1,12 +1,12 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/spec_helper'
|
2
2
|
|
3
|
-
describe HTTPStatus::Base do
|
4
|
-
before(:
|
3
|
+
describe HTTPStatus::Base, 'class inheritance' do
|
4
|
+
before(:all) do
|
5
5
|
ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE.stub!(:has_key?).with(:testing_status).and_return(true)
|
6
6
|
@status_exception_class = HTTPStatus::TestingStatus
|
7
7
|
end
|
8
8
|
|
9
|
-
after(:
|
9
|
+
after(:all) do
|
10
10
|
HTTPStatus::Base.template_path = 'shared/http_status'
|
11
11
|
HTTPStatus.send :remove_const, 'TestingStatus'
|
12
12
|
end
|
@@ -15,6 +15,10 @@ describe HTTPStatus::Base do
|
|
15
15
|
@status_exception_class.status.should == :testing_status
|
16
16
|
end
|
17
17
|
|
18
|
+
it "should use 'shared/http_status' as default view path" do
|
19
|
+
@status_exception_class.template.should == 'shared/http_status/testing_status'
|
20
|
+
end
|
21
|
+
|
18
22
|
it "should check ActionController's status code list for the status code based on the class name" do
|
19
23
|
ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE.should_receive(:[]).with(:testing_status)
|
20
24
|
@status_exception_class.status_code
|
@@ -24,7 +28,7 @@ describe HTTPStatus::Base do
|
|
24
28
|
HTTPStatus::Base.template_path = 'testing'
|
25
29
|
@status_exception_class.template.should == 'testing/testing_status'
|
26
30
|
end
|
27
|
-
|
31
|
+
|
28
32
|
it "should raise an exception when the class name does not correspond to a HTTP status code" do
|
29
33
|
lambda { HTTPStatus::Nonsense }.should raise_error
|
30
34
|
end
|
@@ -45,10 +49,10 @@ end
|
|
45
49
|
it "should return the correct status code (#{status_code}) when using the class" do
|
46
50
|
HTTPStatus.const_get(status_class).status_code.should == status_code
|
47
51
|
end
|
48
|
-
|
52
|
+
|
49
53
|
it "should return the correct status code (#{status_code}) when using the instance" do
|
50
54
|
HTTPStatus.const_get(status_class).new.status_code.should == status_code
|
51
|
-
end
|
55
|
+
end
|
52
56
|
end
|
53
57
|
end
|
54
58
|
|
@@ -84,9 +88,9 @@ describe 'HTTPStatus#http_status_exception' do
|
|
84
88
|
HTTPStatus::Base.template_layout = 'testing'
|
85
89
|
@controller.http_status_exception(HTTPStatus::Base.new('test'))
|
86
90
|
end
|
87
|
-
|
91
|
+
|
88
92
|
it "should call head with the correct status code if render cannot found a template" do
|
89
|
-
@controller.stub!(:render).and_raise(ActionView::MissingTemplate.new([], 'template.
|
93
|
+
@controller.stub!(:render).and_raise(ActionView::MissingTemplate.new([], 'template.html.erb'))
|
90
94
|
@controller.should_receive(:head).with(:internal_server_error)
|
91
95
|
@controller.http_status_exception(HTTPStatus::Base.new('test'))
|
92
96
|
end
|
data/tasks/github-gem.rake
CHANGED
@@ -179,7 +179,7 @@ module GithubGem
|
|
179
179
|
|
180
180
|
# Checks whether the current branch is not diverged from the remote branch
|
181
181
|
def check_not_diverged_task
|
182
|
-
raise "The current branch is diverged from the remote branch!" if git.log.between('HEAD', git.
|
182
|
+
raise "The current branch is diverged from the remote branch!" if git.log.between('HEAD', git.remote(remote).branch(remote_branch).gcommit).any?
|
183
183
|
end
|
184
184
|
|
185
185
|
# Checks whether the repository status ic clean
|
@@ -308,7 +308,15 @@ module GithubGem
|
|
308
308
|
response = http.get(path)
|
309
309
|
open(__FILE__, "w") { |file| file.write(response.body) }
|
310
310
|
end
|
311
|
-
|
311
|
+
|
312
|
+
relative_file = File.expand_path(__FILE__).sub(%r[^#{git.dir.path}/], '')
|
313
|
+
if git.status[relative_file] && git.status[relative_file].type == 'M'
|
314
|
+
git.add(relative_file)
|
315
|
+
git.commit("Updated to latest gem release management tasks.")
|
316
|
+
puts "Updated to latest version of gem release management tasks."
|
317
|
+
else
|
318
|
+
puts "Release managament tasks already are at the latest version."
|
319
|
+
end
|
312
320
|
end
|
313
321
|
|
314
322
|
end
|
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.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Willem van Bergen
|
@@ -9,18 +9,18 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-10-07 00:00:00 +02:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
|
-
name:
|
16
|
+
name: actionpack
|
17
17
|
type: :runtime
|
18
18
|
version_requirement:
|
19
19
|
version_requirements: !ruby/object:Gem::Requirement
|
20
20
|
requirements:
|
21
21
|
- - ">="
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version:
|
23
|
+
version: 2.1.0
|
24
24
|
version:
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
@@ -52,9 +52,10 @@ files:
|
|
52
52
|
- tasks/github-gem.rake
|
53
53
|
- README.rdoc
|
54
54
|
- spec/http_status_exception_spec.rb
|
55
|
-
has_rdoc:
|
55
|
+
has_rdoc: true
|
56
56
|
homepage: http://github.com/wvanbergen/http_status_exceptions/wikis
|
57
|
-
licenses:
|
57
|
+
licenses: []
|
58
|
+
|
58
59
|
post_install_message:
|
59
60
|
rdoc_options: []
|
60
61
|
|
@@ -77,7 +78,7 @@ requirements: []
|
|
77
78
|
rubyforge_project:
|
78
79
|
rubygems_version: 1.3.5
|
79
80
|
signing_key:
|
80
|
-
specification_version:
|
81
|
+
specification_version: 3
|
81
82
|
summary: A Rails plugin to use exceptions for generating HTTP status responses
|
82
83
|
test_files:
|
83
84
|
- spec/http_status_exception_spec.rb
|