rescue-dog 0.0.3 → 0.1.0

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # Rescue Dog
2
2
 
3
- respond to an exception raised in Rails
3
+ The Rescue-Dog responds HTTP status (the code and message) when raise the exception.
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,27 +18,42 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
+ 1. Include `Rescue::Controller::Static` or `Rescue::Controller::Dynamic`.
22
+ 2. Call `rescue_associate` method. And then, the exception class is defined and added to `rescue_handlers`.
23
+ 3. Raise the exception or Call `response_status` method.
24
+
21
25
  ### Render Static Files
26
+ Render /public/400(.:format) if you raise BadRequest exception.
27
+
22
28
  $ vim app/controllers/application_controller.rb
23
29
  class ApplicationController
24
30
 
25
31
  include Rescue::Controller::Static
26
- define_errors ServerError: 500, NotFound: 404
32
+ rescue_associate :BadRequest ,with: 400
33
+ rescue_associate :Unauthorized ,with: 401
34
+ rescue_associate :NotFound ,with: 404
35
+ rescue_associate :ServerError ,with: 500
27
36
 
28
37
  ### Render Template
38
+ Render app/views/errors/404(.:format) if you raise NotFound exception.
39
+
29
40
  $ vim app/controllers/application_controller.rb
30
41
  class ApplicationController
31
42
 
32
43
  include Rescue::Controller::Dynamic
33
- define_errors ServerError: 500, NotFound: 404
44
+ rescue_associate :BadRequest ,with: 400
45
+ rescue_associate :Unauthorized ,with: 401
46
+ rescue_associate :NotFound ,with: 404
47
+ rescue_associate :ServerError ,with: 500
34
48
 
35
49
  ### Associated with the exceptions
36
- Call the response method when raise an exception
50
+ Call the response method when raise an exception.
37
51
 
38
52
  #### for ActiveRecord
39
- rescue_from ActiveRecord::RecordNotFound, with: lambda {|e| respond_status 404 }
53
+ rescue_associate ActiveRecord::RecordNotFound ,with: 404
40
54
  #### for Mongoid
41
- rescue_from Mongoid::Errors::DocumentNotFound, BSON::InvalidObjectId, with: lambda {|e| respond_status 404 }
55
+ rescue_associate Mongoid::Errors::DocumentNotFound, BSON::InvalidObjectId, with: 404
56
+
42
57
 
43
58
  ## Contributing
44
59
 
data/deploy_gem.sh CHANGED
@@ -26,6 +26,6 @@ mv ${PKG_FILE} ./pkg
26
26
  echo "[RUN] gem push pkg/${PKG_FILE}"
27
27
  gem push pkg/${PKG_FILE}
28
28
 
29
- echo "[RUN] git tag -a version-${$1}"
30
- git tag -a version-$1
29
+ echo "[RUN] git tag -a version-$1"
30
+ git tag -a version-$1 -m ""
31
31
  git push --tags
@@ -3,21 +3,15 @@
3
3
  module Rescue
4
4
  module Controller
5
5
  module Dynamic
6
- def self.included(base)
7
- base.extend ClassMethods
8
- base.extend Rescue::Controller::ClassMethods
9
- end
10
6
 
11
- module ClassMethods
12
-
13
- def define_respond_method name
14
- return if method_defined?(name)
15
- define_method name do |code, exception = nil|
7
+ def self.included(base)
8
+ base.class_eval do
9
+ define_method Rescue::Bind.respond_name do |code, exception = nil|
16
10
  e = {}
17
11
  e[:code] = code
18
12
  e[:status] = Rack::Utils::HTTP_STATUS_CODES[code]
19
13
  e[:message] = exception.message if exception
20
-
14
+
21
15
  respond_to do |format|
22
16
  format.html { render status: code, template: "/errors/#{code}" }
23
17
  format.json { render status: code, json: { errors: [e] } }
@@ -25,8 +19,9 @@ module Rescue
25
19
  end
26
20
  end
27
21
  end
28
-
22
+ base.extend Rescue::Controller::ClassMethods
29
23
  end
24
+
30
25
  end
31
26
  end
32
27
  end
@@ -3,21 +3,16 @@
3
3
  module Rescue
4
4
  module Controller
5
5
  module Static
6
+
6
7
  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|
8
+ base.class_eval do
9
+ define_method Rescue::Bind.respond_name do |code, exception = nil|
16
10
  render status: code, file: "#{Rails.root}/public/#{code}", layout: false and return
17
11
  end
18
12
  end
19
-
13
+ base.extend Rescue::Controller::ClassMethods
20
14
  end
15
+
21
16
  end
22
17
  end
23
18
  end
@@ -7,16 +7,34 @@ module Rescue
7
7
 
8
8
  module ClassMethods
9
9
 
10
- def define_errors statuses, superclass = StandardError
11
- respond = :respond_status
12
- define_respond_method respond
10
+ def rescue_associate *klasses, &block
11
+ options = klasses.extract_options!
13
12
 
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) }
13
+ unless block_given?
14
+ if options.has_key?(:with)
15
+ if options[:with].is_a?(Integer)
16
+ block = lambda {|e| send(Rescue::Bind.respond_name, options[:with], e) }
17
+ elsif options[:with].is_a?(Proc)
18
+ block = options[:with]
19
+ end
20
+ else
21
+ raise ArgumentError, "Need a handler. Supply an options hash that has a :with key as the last argument."
22
+ end
23
+ end
24
+
25
+ klasses.each do |klass|
26
+ key = if klass.is_a?(Class) && klass <= Exception
27
+ klass.name
28
+ elsif klass.is_a?(String) || klass.is_a?(Symbol)
29
+ Rescue::Bind.define_error_class klass, StandardError
30
+ klass
31
+ else
32
+ raise ArgumentError, "#{klass} is neither an Exception nor a String"
33
+ end
34
+ self.rescue_handlers += [[key, block]]
17
35
  end
18
36
  end
19
- end
20
37
 
38
+ end
21
39
  end
22
40
  end
@@ -1,3 +1,3 @@
1
1
  module Rescue
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/rescue-dog.rb CHANGED
@@ -5,10 +5,16 @@ module Rescue
5
5
 
6
6
  class Bind
7
7
  class << self
8
+
9
+ def respond_name
10
+ :respond_status
11
+ end
12
+
8
13
  def define_error_class class_name, superclass = nil
9
14
  return if Object.const_defined?(class_name)
10
15
  Object.const_set(class_name, Class.new(superclass||StandardError))
11
16
  end
17
+
12
18
  end
13
19
  end
14
20
  end
@@ -31,7 +31,10 @@ class ApplicationController < ActionController::Base ; end
31
31
 
32
32
  class StaticController < ApplicationController
33
33
  include Rescue::Controller::Static
34
- define_errors BadRequest: 400, Unauthorized: 401, NotFound: 404, ServerError: 500
34
+ rescue_associate :BadRequest ,with: 400
35
+ rescue_associate :Unauthorized ,with: 401
36
+ rescue_associate :NotFound ,with: 404
37
+ rescue_associate :ServerError ,with: 500
35
38
 
36
39
  STATUSES.each do |name, code|
37
40
  class_name = "#{name}".classify
@@ -43,7 +46,10 @@ end
43
46
 
44
47
  class DynamicController < ApplicationController
45
48
  include Rescue::Controller::Dynamic
46
- define_errors BadRequest: 400, Unauthorized: 401, NotFound: 404, ServerError: 500
49
+ rescue_associate :BadRequest ,with: 400
50
+ rescue_associate :Unauthorized ,with: 401
51
+ rescue_associate :NotFound ,with: 404
52
+ rescue_associate :ServerError ,with: 500
47
53
 
48
54
  STATUSES.each do |name, code|
49
55
  class_name = "#{name}".classify
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.3
4
+ version: 0.1.0
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-20 00:00:00.000000000 Z
12
+ date: 2013-03-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails