rescue-dog 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -18,17 +18,27 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
+ ### Render Static Files
21
22
  $ vim app/controllers/application_controller.rb
22
23
  class ApplicationController
23
24
 
24
- include Rescue::Controller
25
+ include Rescue::Controller::Static
25
26
  define_errors ServerError: 500, NotFound: 404
27
+
28
+ ### Render Template
29
+ $ vim app/controllers/application_controller.rb
30
+ class ApplicationController
26
31
 
27
- # Call the response method when raise an exception
28
- # for ActiveRecord
29
- rescue_from ActiveRecord::RecordNotFound, with: respond_404
30
- # for Mongoid
31
- rescue_from Mongoid::Errors::DocumentNotFound, BSON::InvalidObjectId, with: :respond_404
32
+ include Rescue::Controller::Dynamic
33
+ define_errors ServerError: 500, NotFound: 404
34
+
35
+ ### Associated with the exceptions
36
+ Call the response method when raise an exception
37
+
38
+ #### for ActiveRecord
39
+ rescue_from ActiveRecord::RecordNotFound, with: lambda {|e| respond_status 404 }
40
+ #### for Mongoid
41
+ rescue_from Mongoid::Errors::DocumentNotFound, BSON::InvalidObjectId, with: lambda {|e| respond_status 404 }
32
42
 
33
43
  ## Contributing
34
44
 
@@ -0,0 +1,2 @@
1
+ <h1>Bad Request</h1>
2
+ <p>Sorry, I can not understand it.</p>
@@ -0,0 +1,2 @@
1
+ <h1>Unauthorized</h1>
2
+ <p>Sorry, I can not response to you.</p>
@@ -0,0 +1,2 @@
1
+ <h1>Not Found</h1>
2
+ <p>Sorry, I can not found it.</p>
@@ -0,0 +1,2 @@
1
+ <h1>Server Error</h1>
2
+ <p>Sorry, I can not do it.</p>
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset='utf-8'>
5
+ <title>Error</title>
6
+ <%= stylesheet_link_tag :all %>
7
+ <%= javascript_include_tag :defaults %>
8
+ <%= csrf_meta_tag %>
9
+ </head>
10
+ <body>
11
+
12
+ <%= yield %>
13
+
14
+ </body>
15
+ </html>
data/deploy_gem.sh CHANGED
@@ -25,3 +25,7 @@ echo "[RUN] mv ${PKG_FILE} pkg/"
25
25
  mv ${PKG_FILE} ./pkg
26
26
  echo "[RUN] gem push pkg/${PKG_FILE}"
27
27
  gem push pkg/${PKG_FILE}
28
+
29
+ echo "[RUN] git tag -a version-${$1}"
30
+ git tag -a version-$1
31
+ git push --tags
@@ -0,0 +1,32 @@
1
+ # coding: UTF-8
2
+
3
+ module Rescue
4
+ module Controller
5
+ module Dynamic
6
+ def self.included(base)
7
+ base.extend ClassMethods
8
+ base.extend Rescue::Controller::ClassMethods
9
+ end
10
+
11
+ module ClassMethods
12
+
13
+ def define_respond_method name
14
+ return if method_defined?(name)
15
+ define_method name do |code, exception = nil|
16
+ e = {}
17
+ e[:code] = code
18
+ e[:status] = Rack::Utils::HTTP_STATUS_CODES[code]
19
+ e[:message] = exception.message if exception
20
+
21
+ respond_to do |format|
22
+ format.html { render status: code, template: "/errors/#{code}" }
23
+ format.json { render status: code, json: { errors: [e] } }
24
+ format.xml { render status: code, xml: { errors: [e] } }
25
+ end
26
+ end
27
+ end
28
+
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,23 @@
1
+ # coding: UTF-8
2
+
3
+ module Rescue
4
+ module Controller
5
+ module Static
6
+ def self.included(base)
7
+ base.extend ClassMethods
8
+ base.extend Rescue::Controller::ClassMethods
9
+ end
10
+
11
+ module ClassMethods
12
+
13
+ def define_respond_method name
14
+ return if method_defined?(name)
15
+ define_method name do |code, exception = nil|
16
+ render status: code, file: "#{Rails.root}/public/#{code}", layout: false and return
17
+ end
18
+ end
19
+
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,34 +1,19 @@
1
1
  # coding: UTF-8
2
+ require File.join(File.dirname(__FILE__), "controller/static.rb")
3
+ require File.join(File.dirname(__FILE__), "controller/dynamic.rb")
4
+
2
5
  module Rescue
3
6
  module Controller
4
7
 
5
- def self.included(base)
6
- base.extend ClassMethods
7
- end
8
-
9
8
  module ClassMethods
10
- def define_errors statuses, superclass = StandardError
11
- statuses.each do |class_name, code|
12
- respond = :"respond_#{code}"
13
-
14
- define_error_class class_name, superclass
15
- define_respond_method respond, code
16
9
 
17
- # an error maps to respond method
18
- rescue_from "#{class_name}".constantize, with: respond
19
- end
20
- end
21
-
22
- def define_error_class class_name, superclass = StandardError
23
- return if Object.const_defined?(class_name)
24
- Object.const_set(class_name, Class.new(superclass))
25
- end
10
+ def define_errors statuses, superclass = StandardError
11
+ respond = :respond_status
12
+ define_respond_method respond
26
13
 
27
- # Define "respond_#{code}" method
28
- def define_respond_method name, code
29
- return if method_defined?(name)
30
- define_method name do |e = nil|
31
- render status: code, file: "#{Rails.root}/public/#{code}.#{request.format.to_sym}", layout: false and return
14
+ statuses.each do |class_name, code|
15
+ Rescue::Bind.define_error_class class_name, superclass
16
+ rescue_from "#{class_name}".constantize, with: lambda {|e| send(respond, code, e) }
32
17
  end
33
18
  end
34
19
  end
@@ -1,3 +1,3 @@
1
1
  module Rescue
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/rescue-dog.rb CHANGED
@@ -2,4 +2,13 @@ require "rescue/version"
2
2
 
3
3
  require File.join(File.dirname(__FILE__),'rescue/controller.rb')
4
4
  module Rescue
5
+
6
+ class Bind
7
+ class << self
8
+ def define_error_class class_name, superclass = nil
9
+ return if Object.const_defined?(class_name)
10
+ Object.const_set(class_name, Class.new(superclass||StandardError))
11
+ end
12
+ end
13
+ end
5
14
  end
data/public/400.html ADDED
@@ -0,0 +1,11 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset='utf-8'>
5
+ <title>Error: 400 Bad Request</title>
6
+ </head>
7
+ <body>
8
+ <h1>Bad Request</h1>
9
+ <p>Sorry, I can not understand it.</p>
10
+ </body>
11
+ </html>
data/public/400.json ADDED
@@ -0,0 +1 @@
1
+ {"errors":[{"code":400,"status":"Bad Request"}]}
data/public/401.html ADDED
@@ -0,0 +1,11 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset='utf-8'>
5
+ <title>Error: 401 Unauthorized</title>
6
+ </head>
7
+ <body>
8
+ <h1>Unauthorized</h1>
9
+ <p>Sorry, I can not response to you.</p>
10
+ </body>
11
+ </html>
data/public/401.json ADDED
@@ -0,0 +1 @@
1
+ {"errors":[{"code":401,"status":"Unauthorized"}]}
@@ -1,29 +1,56 @@
1
1
  require 'action_controller/railtie'
2
2
  require 'action_view/railtie'
3
3
 
4
+ # const
5
+ STATUSES = {
6
+ :bad_request => 400,
7
+ :unauthorized => 401,
8
+ :not_found => 404,
9
+ :server_error => 500,
10
+ }
11
+
4
12
  # config
5
13
  app = Class.new Rails::Application
6
14
  app.config.active_support.deprecation = :log
7
15
  app.config.secret_token = 'ccedfce890492dd9fe2908a69a8732104ae133f1e2488bf6a1e96685b05a96d7e11aeaa3da5ade27604a50c3b2c7cc8323dd03ad11bb2e52e95256fb67ef9c8a'
16
+ app.config.generators do |g|
17
+ g.template_engine :haml
18
+ end
8
19
  app.initialize!
9
20
 
10
21
  # routing
11
22
  app.routes.draw do
12
- [:not_found, :server_error].each do |e|
13
- get "/#{e}"=>"errors##{e}" ,as: e
23
+ STATUSES.each do |name, code|
24
+ get "/static/#{name}" =>"static##{name}" ,as: name
25
+ get "/dynamic/#{name}" =>"dynamic##{name}" ,as: name
14
26
  end
15
27
  end
16
28
 
17
29
  # controllers
18
- class ApplicationController < ActionController::Base;
19
- include Rescue::Controller
20
- define_errors ServerError: 500, NotFound: 404
30
+ class ApplicationController < ActionController::Base ; end
31
+
32
+ class StaticController < ApplicationController
33
+ include Rescue::Controller::Static
34
+ define_errors BadRequest: 400, Unauthorized: 401, NotFound: 404, ServerError: 500
35
+
36
+ STATUSES.each do |name, code|
37
+ class_name = "#{name}".classify
38
+ define_method name do
39
+ raise class_name.constantize
40
+ end
41
+ end
21
42
  end
22
- class ErrorsController < ApplicationController
23
43
 
24
- def not_found ; raise NotFound ; end
25
- def server_error ; raise ServerError ; end
44
+ class DynamicController < ApplicationController
45
+ include Rescue::Controller::Dynamic
46
+ define_errors BadRequest: 400, Unauthorized: 401, NotFound: 404, ServerError: 500
26
47
 
48
+ STATUSES.each do |name, code|
49
+ class_name = "#{name}".classify
50
+ define_method name do
51
+ raise class_name.constantize.new "This is an explanation of what caused the error."
52
+ end
53
+ end
27
54
  end
28
55
 
29
56
  Object.const_set(:ApplicationHelper,Module.new)
@@ -0,0 +1,33 @@
1
+ # coding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe Rescue::Controller::Dynamic do
5
+
6
+ let(:object) { DynamicController.new }
7
+
8
+ describe "respond method" do
9
+ subject { object.methods.include?(:respond_status) }
10
+ it "should be called in a subclass of ApplicationController" do
11
+ should be_true
12
+ end
13
+ end
14
+
15
+ TestCase::Controller::ERRORS.each do |name, code|
16
+
17
+ describe "response when raise #{name}" do
18
+ TestCase::Controller::FORMATS.each do |format|
19
+ context "request format => #{format}" do
20
+ before do
21
+ visit "/dynamic/#{name.to_s.underscore}.#{format.to_sym}"
22
+ end
23
+
24
+ subject { page }
25
+ it { should have_content name.to_s.gsub(/([A-Z]+)([A-Z][a-z])/,'\1 \2').gsub(/([a-z\d])([A-Z])/,'\1 \2') }
26
+ it { should have_content "This is an explanation of what caused the error." } unless format.to_sym == :html
27
+ it { response_headers["Content-Type"].should include(format.to_s) }
28
+ end
29
+ end
30
+ end
31
+ end ## end each
32
+
33
+ end
@@ -0,0 +1,32 @@
1
+ # coding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe Rescue::Controller::Static do
5
+
6
+ let(:object) { StaticController.new }
7
+
8
+ describe "respond method" do
9
+ subject { object.methods.include?(:respond_status) }
10
+ it "should be called in a subclass of ApplicationController" do
11
+ should be_true
12
+ end
13
+ end
14
+
15
+ TestCase::Controller::ERRORS.each do |name, code|
16
+
17
+ describe "response when raise #{name}" do
18
+ TestCase::Controller::FORMATS.each do |format|
19
+ context "request format => #{format}" do
20
+ before do
21
+ visit "/static/#{name.to_s.underscore}.#{format.to_sym}"
22
+ end
23
+
24
+ subject { page }
25
+ it { should have_content name.to_s.gsub(/([A-Z]+)([A-Z][a-z])/,'\1 \2').gsub(/([a-z\d])([A-Z])/,'\1 \2') }
26
+ it { response_headers["Content-Type"].should include(format.to_s) }
27
+ end
28
+ end
29
+ end
30
+ end ## end each
31
+
32
+ end
@@ -0,0 +1,14 @@
1
+ # coding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe Rescue::Controller do
5
+
6
+ let(:dynamic) do
7
+ clazz = Class.new ApplicatoinController do
8
+ include Rescue::Controller
9
+ define_errors :dynamic, BadRequest: 400, Unauthorized: 401, NotFound: 404, ServerError: 500
10
+ end
11
+ clazz.new
12
+ end
13
+
14
+ end
@@ -0,0 +1,36 @@
1
+ # coding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe Rescue do
5
+
6
+ describe "define_error_class :RescueStandardError" do
7
+ let(:name) { :RescueStandardError }
8
+ before { Rescue::Bind.define_error_class name }
9
+
10
+ subject { Object.const_defined? name }
11
+ it "should define an exception class" do
12
+ should be_true
13
+ end
14
+
15
+ subject { Object.const_get(name).new }
16
+ it "should define an exception class that is a kind of StandardError" do
17
+ should be_a_kind_of StandardError
18
+ end
19
+ end
20
+
21
+ describe "define_error_class :RescueScriptError, ScriptError" do
22
+ let(:name) { :RescueScriptError }
23
+ before { Rescue::Bind.define_error_class name, ScriptError }
24
+
25
+ subject { Object.const_defined? name }
26
+ it "should define an exception class" do
27
+ should be_true
28
+ end
29
+
30
+ subject { Object.const_get(name).new }
31
+ it "should define an exception class that is a kind of StandardError" do
32
+ should be_a_kind_of ScriptError
33
+ end
34
+ end
35
+
36
+ end
data/spec/test_case.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  class TestCase
2
2
 
3
3
  class Controller
4
- ERRORS = { NotFound: 404, ServerError: 500 }
4
+ ERRORS = { BadRequest: 400, Unauthorized: 401, NotFound: 404, ServerError: 500 }
5
5
  FORMATS = [Mime::Type.new("text/html", :html), Mime::Type.new("application/json", :json)]
6
6
  end
7
7
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rescue-dog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-12 00:00:00.000000000 Z
12
+ date: 2013-03-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -40,17 +40,31 @@ files:
40
40
  - LICENSE.txt
41
41
  - README.md
42
42
  - Rakefile
43
+ - app/views/errors/400.html.erb
44
+ - app/views/errors/401.html.erb
45
+ - app/views/errors/404.html.erb
46
+ - app/views/errors/500.html.erb
47
+ - app/views/layouts/application.html.erb
43
48
  - deploy_gem.sh
44
49
  - lib/rescue-dog.rb
45
50
  - lib/rescue/controller.rb
51
+ - lib/rescue/controller/dynamic.rb
52
+ - lib/rescue/controller/static.rb
46
53
  - lib/rescue/version.rb
54
+ - public/400.html
55
+ - public/400.json
56
+ - public/401.html
57
+ - public/401.json
47
58
  - public/404.html
48
59
  - public/404.json
49
60
  - public/500.html
50
61
  - public/500.json
51
62
  - rescue-dog.gemspec
52
- - spec/controller_spec.rb
53
63
  - spec/rails_spec_app.rb
64
+ - spec/rescue/controller/dynamic_spec.rb
65
+ - spec/rescue/controller/static_spec.rb
66
+ - spec/rescue/controller_spec.rb
67
+ - spec/rescue_spec.rb
54
68
  - spec/spec_helper.rb
55
69
  - spec/test_case.rb
56
70
  homepage: https://github.com/yulii/rescue-dog
@@ -78,7 +92,10 @@ signing_key:
78
92
  specification_version: 3
79
93
  summary: define respond methods
80
94
  test_files:
81
- - spec/controller_spec.rb
82
95
  - spec/rails_spec_app.rb
96
+ - spec/rescue/controller/dynamic_spec.rb
97
+ - spec/rescue/controller/static_spec.rb
98
+ - spec/rescue/controller_spec.rb
99
+ - spec/rescue_spec.rb
83
100
  - spec/spec_helper.rb
84
101
  - spec/test_case.rb
@@ -1,50 +0,0 @@
1
- # coding: UTF-8
2
- require 'spec_helper'
3
-
4
- describe Rescue::Controller do
5
-
6
- before do
7
- @r = ErrorsController.new
8
- end
9
-
10
- TestCase::Controller::ERRORS.each do |name, code|
11
- describe "#{name} exception class" do
12
- subject { Object.const_defined? name }
13
- it "should define '#{name}' class" do
14
- should be_true
15
- end
16
-
17
- subject { Object.const_get(name).new }
18
- it "should be a kind of StandardError" do
19
- should be_a_kind_of StandardError
20
- end
21
- end
22
-
23
- describe "respond_#{code} method" do
24
- subject { ApplicationController.method_defined? :"respond_#{code}" }
25
- it "should be defined in ApplicationController" do
26
- should be_true
27
- end
28
-
29
- subject { @r.methods.include?(:"respond_#{code}") }
30
- it "should be called in a subclass of ApplicationController" do
31
- should be_true
32
- end
33
- end
34
-
35
- describe "raise #{name}" do
36
- TestCase::Controller::FORMATS.each do |format|
37
- context "request format => #{format}" do
38
- before do
39
- visit "/#{name.to_s.underscore}.#{format.to_sym}"
40
- end
41
-
42
- subject { page }
43
- it { should have_content name.to_s.gsub(/([A-Z]+)([A-Z][a-z])/,'\1 \2').gsub(/([a-z\d])([A-Z])/,'\1 \2') }
44
- it { response_headers["Content-Type"].should include(format.to_s) }
45
- end
46
- end
47
- end
48
- end ## end each
49
-
50
- end