restful_error 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,15 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 19c7576aafad158add3451d2470a10828e9533a8
4
- data.tar.gz: bade50c831d2d7a894f61c0745b762980ffe9170
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YmQwYzRkYTQwZTcxMDU2MzU1NzFjYTNkMzE5MzA1MWQ3NTk3OTgyYQ==
5
+ data.tar.gz: !binary |-
6
+ OGEzYjE1MGYwNzBhZGE2NGQ2YTM1MWYzMTRjZjIxYjJjMzYxM2M1NA==
5
7
  SHA512:
6
- metadata.gz: 0586cb161caac54f9aacf4233ad6352a36c93b9809475fded81c2a5fb473b0d9fe7cac861a18d438201fdffe73d0e01dea62fca5f677574638342da4277f9a76
7
- data.tar.gz: 5c4f5cba5c88cb538df49695ec93a18ca32aa6a0b3fa6240dec82afadbf676d1b99f631e83b3fd949b062ca271f65016b0985a0979b2e19aac8c0739f76e10cb
8
+ metadata.gz: !binary |-
9
+ Yzk0MWYyMGI4MTA5OWEwNjNiNTU0YTFlMzNmZDFmYzI0MzA0MDJhOWU1M2Iy
10
+ NjViOTk0YjJkMzAyNzgzMzVhMDFmOWE1YWJhOGViN2Y5YWQzOWJlOWE3YmQy
11
+ NWVjNmI4OThiZWIyNzg0YjUzMDRmYWM3MWIwYjY5NWU2Mjk0NGI=
12
+ data.tar.gz: !binary |-
13
+ OWVmMzYyNjVkNTgzOGJmZWI0ZDFiZDk2MDM2YjUwOWRkNDY0YjU3ZjU4YmFm
14
+ NWNjNTRiYzVhZTE3NjUwMmE1ZDJhNDQ1NDY0OWE2YTgzOTk5NzhmNGZhZTEw
15
+ NTIzNzRhZWFjZWMzMmI3ODI5YTI0NWE0N2UxNzA5YzI4ZDg1ZGM=
data/lib/restful_error.rb CHANGED
@@ -3,8 +3,9 @@ require 'webrick/accesslog'
3
3
 
4
4
  RestfulError = WEBrick::HTTPStatus
5
5
 
6
- require "restful_error/version"
7
- require "restful_error/engine"
6
+ load "restful_error/version.rb"
7
+ require 'restful_error/engine'
8
+ require 'restful_error/wrapper'
8
9
 
9
10
  module RestfulError
10
11
  CodeToError.each do |_, klass|
@@ -16,23 +17,29 @@ module RestfulError
16
17
  end
17
18
  end
18
19
 
20
+ module Helper
21
+ def restful
22
+ @restful ||= Wrapper.new(self)
23
+ end
24
+ end
25
+
19
26
  module ActionController
20
- def self.included(base)
21
- base.rescue_from StandardError do |ex|
22
- @exception = ex
23
- @status_code = Rack::Utils.status_code(@exception.try(:status_code)).nonzero? || ActionDispatch::ExceptionWrapper.new(env, @exception).status_code
24
- raise if @status_code == 500 && Rails.configuration.consider_all_requests_local
27
+ def render_exception(ex)
28
+ @exception = ex.extend(Helper)
29
+ ex.restful.set_env(env)
30
+ @status_code = ex.restful.status_code
31
+ @reason_phrase = ex.restful.reason_phrase
32
+ @message = ex.restful.message
25
33
 
26
- @reason_phrase = RestfulError.reason_phrase(@status_code)
27
- @message = @exception.try(:status_message)
28
- unless @message
29
- reason_phrase_key = @reason_phrase.downcase.gsub(/\s|-/, '_').to_sym
30
- @message = I18n.t @exception.class.name.underscore, default: [reason_phrase_key, @exception.class.name], scope: :restful_error
31
- end
34
+ raise if @status_code == 500 && Rails.configuration.consider_all_requests_local
32
35
 
33
- respond_to do |format|
34
- format.any(:json, :xml, :html){ render 'restful_error/show', status: @status_code }
35
- end
36
+ respond_to do |format|
37
+ format.any(:json, :xml, :html){ render 'restful_error/show', status: @status_code }
38
+ end
39
+ end
40
+ def self.included(base)
41
+ base.rescue_from StandardError do |ex|
42
+ render_exception ex
36
43
  end
37
44
  end
38
45
  end
@@ -1,4 +1,9 @@
1
- module RestfulError #:nodoc:
2
- class Engine < ::Rails::Engine #:nodoc:
1
+ begin
2
+ require 'rails'
3
+ module RestfulError #:nodoc:
4
+ class Engine < ::Rails::Engine #:nodoc:
5
+ end
3
6
  end
7
+ rescue LoadError
8
+ #do nothing
4
9
  end
@@ -1,3 +1,3 @@
1
1
  module RestfulError
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1,23 @@
1
+ module RestfulError
2
+ class Wrapper
3
+ def initialize(ex)
4
+ @ex = ex
5
+ end
6
+ def set_env(env)
7
+ @env = env
8
+ end
9
+ def status_code
10
+ Rack::Utils.status_code(@ex.try(:status_code)).nonzero? || ActionDispatch::ExceptionWrapper.new(@env, @ex).status_code
11
+ end
12
+ def reason_phrase
13
+ RestfulError.reason_phrase(status_code)
14
+ end
15
+ def reason_phrase_key
16
+ reason_phrase.downcase.gsub(/\s|-/, '_').to_sym
17
+ end
18
+ def message
19
+ return message if message = @ex.try(:status_message)
20
+ I18n.t @ex.class.name.underscore, default: [reason_phrase_key, @ex.class.name], scope: :restful_error
21
+ end
22
+ end
23
+ end
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["kuboon"]
10
10
  spec.email = ["ohkubo@magician.jp"]
11
11
  spec.summary = %q{Define your error with status code. Raise it and you will get formatted response with i18nized message.}
12
- spec.description = %q{Define your error with status code. Raise it and you will get formatted response with i18nized message.}
13
- spec.homepage = ""
12
+ spec.description = %q{Define status_code method on your error. Views and messages are fully customizable on rails way.}
13
+ spec.homepage = "https://github.com/kuboon/restful_error"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
@@ -22,5 +22,5 @@ Gem::Specification.new do |spec|
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.6"
24
24
  spec.add_development_dependency "rake"
25
- spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "rspec", "~> 3"
26
26
  end
@@ -5,7 +5,20 @@ describe RestfulError do
5
5
  expect(RestfulError::VERSION).not_to be nil
6
6
  end
7
7
 
8
- it 'does something useful' do
9
- expect(false).to eq(true)
8
+ describe 'Wrapper' do
9
+ require 'active_record/errors'
10
+ subject { ex.extend(RestfulError::Helper) }
11
+ context 'with number' do
12
+ let(:ex){ Class.new(RestfulError[404]).new }
13
+ it do
14
+ expect(subject.restful.status_code).to eq 404
15
+ end
16
+ end
17
+ context 'with phrase' do
18
+ let(:ex){ Class.new(RestfulError::NotFound).new }
19
+ it do
20
+ expect(subject.restful.status_code).to eq 404
21
+ end
22
+ end
10
23
  end
11
24
  end
metadata CHANGED
@@ -1,82 +1,82 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restful_error
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - kuboon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-01 00:00:00.000000000 Z
11
+ date: 2015-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">"
17
+ - - ! '>'
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3.1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">"
24
+ - - ! '>'
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
33
  version: '1.6'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.6'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - ! '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - ! '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ">="
59
+ - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: '3'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ">="
66
+ - - ~>
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
69
- description: Define your error with status code. Raise it and you will get formatted
70
- response with i18nized message.
68
+ version: '3'
69
+ description: Define status_code method on your error. Views and messages are fully
70
+ customizable on rails way.
71
71
  email:
72
72
  - ohkubo@magician.jp
73
73
  executables: []
74
74
  extensions: []
75
75
  extra_rdoc_files: []
76
76
  files:
77
- - ".gitignore"
78
- - ".rspec"
79
- - ".travis.yml"
77
+ - .gitignore
78
+ - .rspec
79
+ - .travis.yml
80
80
  - Gemfile
81
81
  - LICENSE.txt
82
82
  - README.md
@@ -89,10 +89,11 @@ files:
89
89
  - lib/restful_error.rb
90
90
  - lib/restful_error/engine.rb
91
91
  - lib/restful_error/version.rb
92
+ - lib/restful_error/wrapper.rb
92
93
  - restful_error.gemspec
93
94
  - spec/restful_error_spec.rb
94
95
  - spec/spec_helper.rb
95
- homepage: ''
96
+ homepage: https://github.com/kuboon/restful_error
96
97
  licenses:
97
98
  - MIT
98
99
  metadata: {}
@@ -102,17 +103,17 @@ require_paths:
102
103
  - lib
103
104
  required_ruby_version: !ruby/object:Gem::Requirement
104
105
  requirements:
105
- - - ">="
106
+ - - ! '>='
106
107
  - !ruby/object:Gem::Version
107
108
  version: '0'
108
109
  required_rubygems_version: !ruby/object:Gem::Requirement
109
110
  requirements:
110
- - - ">="
111
+ - - ! '>='
111
112
  - !ruby/object:Gem::Version
112
113
  version: '0'
113
114
  requirements: []
114
115
  rubyforge_project:
115
- rubygems_version: 2.2.2
116
+ rubygems_version: 2.4.5
116
117
  signing_key:
117
118
  specification_version: 4
118
119
  summary: Define your error with status code. Raise it and you will get formatted response