straides 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +37 -0
- data/Rakefile +1 -1
- data/lib/straides/return_http_code_error.rb +21 -0
- data/lib/straides/version.rb +1 -1
- data/lib/straides.rb +27 -27
- metadata +15 -17
- data/README.rdoc +0 -3
- data/lib/return_http_code_error.rb +0 -8
- data/lib/tasks/straides_tasks.rake +0 -4
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# Straides
|
2
|
+
|
3
|
+
Better HTTP STatus coDES for RAIls!
|
4
|
+
|
5
|
+
Straides provides a convenient and more consistent way for handling error conditions
|
6
|
+
in controllers.
|
7
|
+
|
8
|
+
Straides makes it possible, for example, to abort execution of a request in a before_filter.
|
9
|
+
|
10
|
+
|
11
|
+
# Usage
|
12
|
+
|
13
|
+
# Stop controller execution and return the request with the given HTTP error.
|
14
|
+
error 404 unless [condition]
|
15
|
+
|
16
|
+
# Shorter version, integrated into loading for example a user object.
|
17
|
+
user = User.find_by_username(params[:id]) or error 401
|
18
|
+
|
19
|
+
|
20
|
+
## Customizing the response
|
21
|
+
|
22
|
+
Straides returns reasonable default responses dependent on the request format.
|
23
|
+
|
24
|
+
* If the request returns HTML, it renders `public/[error code].html`, similar to what Rails does.
|
25
|
+
* If the request returns JSON, it leaves the response body empty by default.
|
26
|
+
|
27
|
+
These behaviors can be overridden by providing the call to `error` with the same parameters
|
28
|
+
as you would give to [render](http://apidock.com/rails/ActionController/Base/render) in Rails.
|
29
|
+
|
30
|
+
# Provide a custom error message in the response body.
|
31
|
+
error 401, :text => 'Please log in first.'
|
32
|
+
|
33
|
+
# Render a different file in the response body.
|
34
|
+
error 404, :file => 'public/custom_404.html'
|
35
|
+
|
36
|
+
# Render a custom JSON response.
|
37
|
+
error 401, :json => { :code => 401, :message => 'Please log in first.' }
|
data/Rakefile
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
module Straides
|
2
|
+
|
3
|
+
# A custom exception for bubbling up HTTP errors and showing the corresponding error pages.
|
4
|
+
class ReturnHttpCodeError < RuntimeError
|
5
|
+
attr_accessor :render_options
|
6
|
+
|
7
|
+
# @constructor
|
8
|
+
#
|
9
|
+
# @param [ Hash ] render_options
|
10
|
+
def initialize(render_options = {})
|
11
|
+
@render_options = render_options
|
12
|
+
end
|
13
|
+
|
14
|
+
# Checks if error options contain any templates assigned to it.
|
15
|
+
#
|
16
|
+
# @return [ true, false ]
|
17
|
+
def has_template?
|
18
|
+
(render_options.keys & [:file, :text, :json, :nothing]).any?
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/straides/version.rb
CHANGED
data/lib/straides.rb
CHANGED
@@ -1,37 +1,37 @@
|
|
1
|
-
require 'return_http_code_error'
|
2
|
-
require '
|
1
|
+
require 'straides/return_http_code_error'
|
2
|
+
require 'straides/version'
|
3
|
+
require 'active_support/concern'
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
rescue_from(ReturnHttpCodeError) { |error| show_error(error) }
|
5
|
+
module Straides
|
6
|
+
extend ActiveSupport::Concern
|
7
7
|
|
8
|
-
|
9
|
-
|
8
|
+
included do
|
9
|
+
rescue_from ReturnHttpCodeError, :with => :show_error
|
10
10
|
|
11
|
-
|
12
|
-
def error status, render_options = {}
|
13
|
-
render_options[:status] = status
|
14
|
-
raise ReturnHttpCodeError, render_options
|
15
|
-
end
|
11
|
+
protected
|
16
12
|
|
13
|
+
# Makes the current action abort and return an HTTP error.
|
14
|
+
#
|
15
|
+
# @param [ Integer ] status
|
16
|
+
# @param [ Hash ] render_options
|
17
|
+
def error(status, render_options = {})
|
18
|
+
render_options[:status] = status
|
19
|
+
raise ReturnHttpCodeError, render_options
|
20
|
+
end
|
17
21
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
error.render_options[:
|
26
|
-
|
27
|
-
|
28
|
-
end
|
29
|
-
format.json do
|
30
|
-
if (error.render_options.keys & [:file, :text, :json, :nothing]).empty?
|
31
|
-
error.render_options[:text] = ''
|
22
|
+
# Outputs the given error to the client.
|
23
|
+
#
|
24
|
+
# @param [ Straides::ReturnHttpCodeError ] error
|
25
|
+
def show_error(error)
|
26
|
+
unless error.has_template?
|
27
|
+
if request.send(:format).html?
|
28
|
+
error.render_options[:file] = "public/#{error.render_options[:status] || 'error'}"
|
29
|
+
error.render_options[:formats] = [:html]
|
30
|
+
else
|
31
|
+
error.render_options[:nothing] = true
|
32
32
|
end
|
33
|
-
render error.render_options
|
34
33
|
end
|
34
|
+
render error.render_options
|
35
35
|
end
|
36
36
|
end
|
37
37
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: straides
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-02-
|
12
|
+
date: 2012-02-27 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
16
|
-
requirement: &
|
15
|
+
name: activesupport
|
16
|
+
requirement: &70126776392840 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70126776392840
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
|
-
name:
|
27
|
-
requirement: &
|
26
|
+
name: rake
|
27
|
+
requirement: &70126776390900 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70126776390900
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
|
-
name: rspec
|
38
|
-
requirement: &
|
37
|
+
name: rspec
|
38
|
+
requirement: &70126776389120 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70126776389120
|
47
47
|
description: A more convenient way to return different HTTP status codes from Rails.
|
48
48
|
email:
|
49
49
|
- kevin.goslar@gmail.com
|
@@ -51,13 +51,12 @@ executables: []
|
|
51
51
|
extensions: []
|
52
52
|
extra_rdoc_files: []
|
53
53
|
files:
|
54
|
-
- lib/return_http_code_error.rb
|
54
|
+
- lib/straides/return_http_code_error.rb
|
55
55
|
- lib/straides/version.rb
|
56
56
|
- lib/straides.rb
|
57
|
-
- lib/tasks/straides_tasks.rake
|
58
57
|
- MIT-LICENSE
|
59
58
|
- Rakefile
|
60
|
-
- README.
|
59
|
+
- README.md
|
61
60
|
homepage: http://github.com/kevgo/straides
|
62
61
|
licenses: []
|
63
62
|
post_install_message:
|
@@ -78,9 +77,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
78
77
|
version: '0'
|
79
78
|
requirements: []
|
80
79
|
rubyforge_project:
|
81
|
-
rubygems_version: 1.8.
|
80
|
+
rubygems_version: 1.8.10
|
82
81
|
signing_key:
|
83
82
|
specification_version: 3
|
84
|
-
summary: HTTP
|
83
|
+
summary: HTTP STatus coDES for RAIls
|
85
84
|
test_files: []
|
86
|
-
has_rdoc:
|
data/README.rdoc
DELETED