straides 0.0.1 → 0.0.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.
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
@@ -11,6 +11,7 @@ rescue LoadError
11
11
  require 'rake/rdoctask'
12
12
  RDoc::Task = Rake::RDocTask
13
13
  end
14
+
14
15
  require 'rspec/core/rake_task'
15
16
 
16
17
  RDoc::Task.new(:rdoc) do |rdoc|
@@ -38,4 +39,3 @@ end
38
39
 
39
40
  desc "Run the unit tests"
40
41
  task :spec => ['spec:unit']
41
-
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Straides
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/straides.rb CHANGED
@@ -1,37 +1,37 @@
1
- require 'return_http_code_error'
2
- require 'active_record'
1
+ require 'straides/return_http_code_error'
2
+ require 'straides/version'
3
+ require 'active_support/concern'
3
4
 
4
- class ApplicationController < ActionController::Base
5
-
6
- rescue_from(ReturnHttpCodeError) { |error| show_error(error) }
5
+ module Straides
6
+ extend ActiveSupport::Concern
7
7
 
8
-
9
- protected
8
+ included do
9
+ rescue_from ReturnHttpCodeError, :with => :show_error
10
10
 
11
- # Makes the current action abort and return an HTTP error.
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
- private
19
-
20
- # Outputs the given error to the client.
21
- def show_error error
22
- respond_to do |format|
23
- format.html do
24
- if (error.render_options.keys & [:file, :text, :json, :nothing]).empty?
25
- error.render_options[:file] = "public/#{error.render_options[:status]}.html"
26
- end
27
- render error.render_options
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.1
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-25 00:00:00.000000000Z
12
+ date: 2012-02-27 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: rails
16
- requirement: &70245816341000 !ruby/object:Gem::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: *70245816341000
24
+ version_requirements: *70126776392840
25
25
  - !ruby/object:Gem::Dependency
26
- name: sqlite3
27
- requirement: &70245816340580 !ruby/object:Gem::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: *70245816340580
35
+ version_requirements: *70126776390900
36
36
  - !ruby/object:Gem::Dependency
37
- name: rspec-rails
38
- requirement: &70245816340160 !ruby/object:Gem::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: *70245816340160
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.rdoc
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.12
80
+ rubygems_version: 1.8.10
82
81
  signing_key:
83
82
  specification_version: 3
84
- summary: HTTP Status codes for Rails
83
+ summary: HTTP STatus coDES for RAIls
85
84
  test_files: []
86
- has_rdoc:
data/README.rdoc DELETED
@@ -1,3 +0,0 @@
1
- = Straides
2
-
3
- This project rocks and uses MIT-LICENSE.
@@ -1,8 +0,0 @@
1
- # A custom exception for bubbling up HTTP errors and showing the correspending error pages.
2
- class ReturnHttpCodeError < RuntimeError
3
- attr_accessor :render_options
4
-
5
- def initialize render_options
6
- @render_options = render_options
7
- end
8
- end
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :straides do
3
- # # Task goes here
4
- # end