rack-banken 0.0.1
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.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +33 -0
- data/Rakefile +2 -0
- data/lib/rack/banken.rb +14 -0
- data/lib/rack/banken/exception_handler.rb +25 -0
- data/lib/rack/banken/exceptions/base.rb +33 -0
- data/lib/rack/banken/exceptions/internal_server_error.rb +8 -0
- data/lib/rack/banken/exceptions/validation_failed.rb +11 -0
- data/lib/rack/banken/version.rb +5 -0
- data/rack-banken.gemspec +27 -0
- data/spec/rack_banken_spec.rb +51 -0
- data/spec/spec_helper.rb +32 -0
- metadata +144 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2d8b79c7983910bb312279416974088cb82f2e08
|
4
|
+
data.tar.gz: 3c09e94c43c1de5ece72b6fdd7ca57612730e018
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d50378b24bccc37ea15863a58af05eabab19ecc696c957d545765144d2d2e18874a174e833623dd5b092cdf2375eb75213cc45c640d291166b3c71e840f09e37
|
7
|
+
data.tar.gz: 3db03e3d5a73d85319b63ea80ce9da9f30cc1f4bcd5e3b76851f086d023b925c3d2b0c40a5db6b0f6922349803f326121bceb779d640cbd0b0af51a53a2cc2db
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Arakaki Yuji
|
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,33 @@
|
|
1
|
+
# Rack::Banken
|
2
|
+
|
3
|
+
Banken is Rack application for return error response by unified way.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'rack-banken', github: "Arakaki-Yuji/rack-banken"
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
## How to Setup
|
18
|
+
|
19
|
+
If you use it no rails
|
20
|
+
|
21
|
+
````ruby:config/application.rb
|
22
|
+
|
23
|
+
.
|
24
|
+
.
|
25
|
+
.
|
26
|
+
module MyApp
|
27
|
+
class Application < Rails::Application
|
28
|
+
|
29
|
+
config.middleware.insert(0, ::Rack::Banken::ExceptionHandler)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
````
|
data/Rakefile
ADDED
data/lib/rack/banken.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "rack/banken/version"
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
module Banken
|
5
|
+
require "rack"
|
6
|
+
require "json"
|
7
|
+
|
8
|
+
require "rack/banken/exception_handler"
|
9
|
+
|
10
|
+
require "rack/banken/exceptions/base"
|
11
|
+
require "rack/banken/exceptions/internal_server_error"
|
12
|
+
require "rack/banken/exceptions/validation_failed"
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Rack
|
2
|
+
module Banken
|
3
|
+
class ExceptionHandler
|
4
|
+
|
5
|
+
def initialize(app)
|
6
|
+
@app = app
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(env)
|
10
|
+
begin
|
11
|
+
@app.call(env)
|
12
|
+
|
13
|
+
rescue Exceptions::ValidationFailed => e
|
14
|
+
raise Exceptions::ValidationFailed, e.message
|
15
|
+
|
16
|
+
rescue => e
|
17
|
+
raise Exceptions::InternalServerError, e.message
|
18
|
+
end
|
19
|
+
|
20
|
+
rescue Exceptions::Base => exception
|
21
|
+
exception.to_rack_response
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Rack
|
2
|
+
module Banken
|
3
|
+
module Exceptions
|
4
|
+
class Base < StandardError
|
5
|
+
def to_rack_response
|
6
|
+
[status_code, headers, [body]]
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def status_code
|
12
|
+
500
|
13
|
+
end
|
14
|
+
|
15
|
+
def headers
|
16
|
+
{ "Content-Type" => "application/json" }
|
17
|
+
end
|
18
|
+
|
19
|
+
def body
|
20
|
+
{ result: false, error: error_message }.to_json
|
21
|
+
end
|
22
|
+
|
23
|
+
def error_message
|
24
|
+
self.message
|
25
|
+
end
|
26
|
+
|
27
|
+
def type
|
28
|
+
self.class.to_s.split("::").last.underscore
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/rack-banken.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rack/banken/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rack-banken"
|
8
|
+
spec.version = Rack::Banken::VERSION
|
9
|
+
spec.authors = ["Arakaki Yuji"]
|
10
|
+
spec.email = ["arakaki@ryukyu-i.co.jp"]
|
11
|
+
spec.summary = %q{rack-banken is rack application for return error response.}
|
12
|
+
spec.homepage = ""
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_runtime_dependency "rack", "~> 1.5.2"
|
21
|
+
spec.add_runtime_dependency "json", "~> 1.7"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rack-test"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0.0"
|
27
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
RSpec.describe "Rack::Banken" do
|
3
|
+
include SpecHelper
|
4
|
+
include Rack::Test::Methods
|
5
|
+
|
6
|
+
let(:test_app) { SpecHelper::TestApplication.new }
|
7
|
+
let(:app) { Rack::Banken::ExceptionHandler.new(test_app) }
|
8
|
+
|
9
|
+
it "GET Access" do
|
10
|
+
get '/'
|
11
|
+
|
12
|
+
expect(last_response.status).to eq(200)
|
13
|
+
expect(last_response.body).to eq("test body")
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "if Exception occured" do
|
17
|
+
let(:path){ '/runtime_error' }
|
18
|
+
|
19
|
+
it "response staus is 500" do
|
20
|
+
get path
|
21
|
+
expect(last_response.status).to eq(500)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "response body is formated by Rack::Banken::Exceptions::Base.body" do
|
25
|
+
get path
|
26
|
+
json = JSON.parse(last_response.body)
|
27
|
+
|
28
|
+
expect(json['result']).to eq(false)
|
29
|
+
expect(json['error']).to be_kind_of(String)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "if Exception Rack::Banken::Exceptions::ValidationFailed occured" do
|
34
|
+
let(:path) { '/validation_failed' }
|
35
|
+
|
36
|
+
it "response status is 400" do
|
37
|
+
get path
|
38
|
+
expect(last_response.status).to eq(400)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "response body is formated by Rack::Banken::Exceptions::ValidationFailed" do
|
42
|
+
get path
|
43
|
+
json = JSON.parse(last_response.body)
|
44
|
+
|
45
|
+
expect(json['result']).to eq(false)
|
46
|
+
expect(json['error']).to be_kind_of(String)
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$:.unshift(File.dirname(__FILE__))
|
3
|
+
|
4
|
+
%w(rubygems rack rack/test rack/banken rspec).each do |f|
|
5
|
+
require f
|
6
|
+
end
|
7
|
+
|
8
|
+
module SpecHelper
|
9
|
+
extend self
|
10
|
+
|
11
|
+
class TestApplication
|
12
|
+
def call(env)
|
13
|
+
request = Rack::Request.new(env)
|
14
|
+
|
15
|
+
if request.path_info == '/runtime_error'
|
16
|
+
raise StandardError, 'runtime error is occured'
|
17
|
+
end
|
18
|
+
|
19
|
+
if request.path_info == '/validation_failed'
|
20
|
+
raise ::Rack::Banken::Exceptions::ValidationFailed, 'params is not valid.'
|
21
|
+
end
|
22
|
+
|
23
|
+
code = 200
|
24
|
+
body = ["test body"]
|
25
|
+
header = {
|
26
|
+
"Content-type" => "text/html;charset=utf-8"
|
27
|
+
}
|
28
|
+
[code, header, body]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-banken
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Arakaki Yuji
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.5.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.5.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: json
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.7'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.7'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.7'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rack-test
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 3.0.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 3.0.0
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- arakaki@ryukyu-i.co.jp
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- Gemfile
|
106
|
+
- LICENSE.txt
|
107
|
+
- README.md
|
108
|
+
- Rakefile
|
109
|
+
- lib/rack/banken.rb
|
110
|
+
- lib/rack/banken/exception_handler.rb
|
111
|
+
- lib/rack/banken/exceptions/base.rb
|
112
|
+
- lib/rack/banken/exceptions/internal_server_error.rb
|
113
|
+
- lib/rack/banken/exceptions/validation_failed.rb
|
114
|
+
- lib/rack/banken/version.rb
|
115
|
+
- rack-banken.gemspec
|
116
|
+
- spec/rack_banken_spec.rb
|
117
|
+
- spec/spec_helper.rb
|
118
|
+
homepage: ''
|
119
|
+
licenses:
|
120
|
+
- MIT
|
121
|
+
metadata: {}
|
122
|
+
post_install_message:
|
123
|
+
rdoc_options: []
|
124
|
+
require_paths:
|
125
|
+
- lib
|
126
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
136
|
+
requirements: []
|
137
|
+
rubyforge_project:
|
138
|
+
rubygems_version: 2.2.0
|
139
|
+
signing_key:
|
140
|
+
specification_version: 4
|
141
|
+
summary: rack-banken is rack application for return error response.
|
142
|
+
test_files:
|
143
|
+
- spec/rack_banken_spec.rb
|
144
|
+
- spec/spec_helper.rb
|