rescue-dog 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +22 -0
- data/README.md +52 -0
- data/Rakefile +1 -0
- data/deploy_gem.sh +27 -0
- data/lib/rescue/controller.rb +37 -0
- data/lib/rescue/version.rb +3 -0
- data/lib/rescue-dog.rb +5 -0
- data/rescue-dog.gemspec +21 -0
- data/spec/controller_spec.rb +12 -0
- data/spec/rails_spec_app.rb +26 -0
- data/spec/spec_helper.rb +5 -0
- metadata +78 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 yulii
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# Rescue Dog
|
2
|
+
|
3
|
+
respond to an exception raised in Rails
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'rescue-dog'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install rescue-dog
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
$ vim app/controllers/application_controller.rb
|
22
|
+
class ApplicationController
|
23
|
+
|
24
|
+
include Rescue::Controller
|
25
|
+
define_errors ServerError: 500, NotFound: 404
|
26
|
+
|
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
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create new Pull Request
|
40
|
+
|
41
|
+
|
42
|
+
## LICENSE
|
43
|
+
(The MIT License)
|
44
|
+
|
45
|
+
Copyright © 2013 yulii
|
46
|
+
|
47
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
48
|
+
|
49
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
50
|
+
|
51
|
+
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
52
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/deploy_gem.sh
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/bin/sh
|
2
|
+
LIB_NAME="rescue-dog"
|
3
|
+
if [ $# -ne 1 ]
|
4
|
+
then
|
5
|
+
echo "invalid VERSION"
|
6
|
+
exit 1
|
7
|
+
fi
|
8
|
+
GEMSPEC="${LIB_NAME}.gemspec"
|
9
|
+
PKG_FILE="${LIB_NAME}-$1.gem"
|
10
|
+
|
11
|
+
echo "[RUN] rspec"
|
12
|
+
rspec
|
13
|
+
if [ $? -eq 1 ]
|
14
|
+
then
|
15
|
+
exit 1
|
16
|
+
fi
|
17
|
+
echo "[RUN] gem build ${GEMSPEC}"
|
18
|
+
gem build ${GEMSPEC}
|
19
|
+
if [ $? -eq 1 ]
|
20
|
+
then
|
21
|
+
exit 1
|
22
|
+
fi
|
23
|
+
|
24
|
+
echo "[RUN] mv ${PKG_FILE} pkg/"
|
25
|
+
mv ${PKG_FILE} ./pkg
|
26
|
+
echo "[RUN] gem push pkg/${PKG_FILE}"
|
27
|
+
gem push pkg/${PKG_FILE}
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# coding: UTF-8
|
2
|
+
module Rescue
|
3
|
+
module Controller
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
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
|
+
|
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
|
26
|
+
|
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}.html", layout: false and return
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
data/lib/rescue-dog.rb
ADDED
data/rescue-dog.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rescue/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "rescue-dog"
|
8
|
+
gem.version = Rescue::VERSION
|
9
|
+
gem.authors = ["yulii"]
|
10
|
+
gem.email = ["yuliinfo@gmail.com"]
|
11
|
+
gem.description = %q{respond to an exception raised in Rails}
|
12
|
+
gem.summary = %q{define respond methods}
|
13
|
+
gem.homepage = "https://github.com/yulii/rescue-dog"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency "rails", "~>3.2.11"
|
21
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'action_controller/railtie'
|
2
|
+
require 'action_view/railtie'
|
3
|
+
|
4
|
+
# config
|
5
|
+
app = Class.new Rails::Application
|
6
|
+
app.config.active_support.deprecation = :log
|
7
|
+
app.initialize!
|
8
|
+
|
9
|
+
# routing
|
10
|
+
app.routes.draw do
|
11
|
+
resources :users
|
12
|
+
end
|
13
|
+
|
14
|
+
# models
|
15
|
+
class User
|
16
|
+
end
|
17
|
+
|
18
|
+
# controllers
|
19
|
+
class ApplicationController < ActionController::Base;
|
20
|
+
include Rescue::Controller
|
21
|
+
end
|
22
|
+
class UsersController < ApplicationController
|
23
|
+
define_errors ServerError: 500, NotFound: 404
|
24
|
+
end
|
25
|
+
|
26
|
+
Object.const_set(:ApplicationHelper,Module.new)
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rescue-dog
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- yulii
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-09 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.2.11
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.2.11
|
30
|
+
description: respond to an exception raised in Rails
|
31
|
+
email:
|
32
|
+
- yuliinfo@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- .rspec
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE.txt
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- deploy_gem.sh
|
44
|
+
- lib/rescue-dog.rb
|
45
|
+
- lib/rescue/controller.rb
|
46
|
+
- lib/rescue/version.rb
|
47
|
+
- rescue-dog.gemspec
|
48
|
+
- spec/controller_spec.rb
|
49
|
+
- spec/rails_spec_app.rb
|
50
|
+
- spec/spec_helper.rb
|
51
|
+
homepage: https://github.com/yulii/rescue-dog
|
52
|
+
licenses: []
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
requirements: []
|
70
|
+
rubyforge_project:
|
71
|
+
rubygems_version: 1.8.24
|
72
|
+
signing_key:
|
73
|
+
specification_version: 3
|
74
|
+
summary: define respond methods
|
75
|
+
test_files:
|
76
|
+
- spec/controller_spec.rb
|
77
|
+
- spec/rails_spec_app.rb
|
78
|
+
- spec/spec_helper.rb
|