problem_details 0.1.0 → 0.2.3
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 +5 -5
- data/CHANGELOG.md +22 -0
- data/README.md +70 -7
- data/Rakefile +52 -5
- data/VERSION +1 -0
- data/lib/problem_details/document.rb +4 -3
- data/lib/problem_details/version.rb +1 -1
- data/lib/problem_details.rb +1 -1
- data/problem_details.gemspec +14 -10
- metadata +10 -15
- data/.gitignore +0 -12
- data/.rspec +0 -3
- data/.travis.yml +0 -6
- data/Gemfile +0 -7
- data/bin/console +0 -14
- data/bin/setup +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3dbc10dcfafafc3f0a5ab8e133bed56285533a9adcba459171f4d57c9d9b3136
|
4
|
+
data.tar.gz: a34e365906ec1b5d4d4d8946369a053c40debef9d871e84482369995ef011f9c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bb93e0090a87c06f4021b5a70e3056c7c187629d93d6383aaa61153108dad7ae7dd426f2874af0434c3a3c8b4efc960fad76a32434737673afff9aa21e523d3b
|
7
|
+
data.tar.gz: 673be8931bf5b2ebabd6818b9e40f023bc8304c6d83dafd363721598df28aa83fca8d8b56e1061e3701ccf6fced41ae3de26d2b7af2da99c47851ed6933a889d
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
## 0.2.3 / 2021-09-03
|
2
|
+
|
3
|
+
* Support the `#to_hash` implicit conversion protocol (#8) by @stevenharman
|
4
|
+
|
5
|
+
## 0.2.2 / 2019-10-30
|
6
|
+
|
7
|
+
* Remove default about:blank type (#5) by @akrawchyk
|
8
|
+
|
9
|
+
## 0.2.1 / 2018-03-31
|
10
|
+
|
11
|
+
* Clarify supported ruby/gem versions
|
12
|
+
|
13
|
+
## 0.2.0 / 2018-03-20
|
14
|
+
|
15
|
+
### Enhancements:
|
16
|
+
* Introduce a new gem, `sinatra-problem_details`, the Sinatra problem detail extension.
|
17
|
+
|
18
|
+
## 0.1.0 / 2018-03-26
|
19
|
+
|
20
|
+
* First release
|
21
|
+
* Initial implementation of `problem_details`
|
22
|
+
* Rails renderer feature by `problem_details-rails`
|
data/README.md
CHANGED
@@ -1,11 +1,14 @@
|
|
1
|
-
# ProblemDetails
|
1
|
+
# ProblemDetails
|
2
|
+
|
3
|
+
[](https://github.com/nikushi/problem_details/actions/workflows/ci.yml)
|
4
|
+
[](https://badge.fury.io/rb/problem_details)
|
2
5
|
|
3
6
|
ProblemDetails is an implementation of [RFC7807 Problem Details for HTTP APIs](https://tools.ietf.org/html/rfc7807).
|
4
7
|
|
5
8
|
The RFC defines a "problem detail" as a way to inform errors to clients as machine readable form in a HTTP response
|
6
9
|
to avoid the need to define new error response formats for HTTP APIs.
|
7
10
|
|
8
|
-
This library also works with Rails, by the `problem` renderer that helps to respond with the problem detail form.
|
11
|
+
This library also works with Rails and Sinatra, by the `problem` renderer that helps to respond with the problem detail form.
|
9
12
|
|
10
13
|
Currently only JSON serialization is supported.
|
11
14
|
|
@@ -14,6 +17,12 @@ Currently only JSON serialization is supported.
|
|
14
17
|
* Provides the class that implements a Problem Details JSON Object.
|
15
18
|
* With Rails, automatically adds the renderer to respond with `Content-Type: application/problem+json` which works with `render` in controllers.
|
16
19
|
|
20
|
+
## Supported Versions
|
21
|
+
|
22
|
+
* Ruby 2.4.x, 2.5.x, 2.6.x
|
23
|
+
* Rails 4.x, 5.x
|
24
|
+
* Sinatra >= 1.4
|
25
|
+
|
17
26
|
## Installation
|
18
27
|
|
19
28
|
Add this line to your application's Gemfile:
|
@@ -28,6 +37,12 @@ Or if you use with Rails, add below line instead.
|
|
28
37
|
gem 'problem_details-rails'
|
29
38
|
```
|
30
39
|
|
40
|
+
With Sinatra, add below line instead.
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
gem 'sinatra-problem_details'
|
44
|
+
```
|
45
|
+
|
31
46
|
And then execute:
|
32
47
|
|
33
48
|
$ bundle
|
@@ -40,24 +55,28 @@ And then execute:
|
|
40
55
|
require 'problem_details'
|
41
56
|
|
42
57
|
ProblemDetails::Document.new(status: 404).to_json
|
58
|
+
|
59
|
+
# Or status code symbol can be specified as well.
|
60
|
+
ProblemDetails::Document.new(status: :not_found).to_json
|
43
61
|
```
|
44
62
|
|
45
63
|
will produce:
|
46
64
|
|
47
65
|
```json
|
48
66
|
{
|
49
|
-
"type": "about:blank",
|
50
67
|
"title": "Not Found",
|
51
68
|
"status": 404
|
52
69
|
}
|
53
70
|
```
|
54
71
|
|
55
|
-
As above, the value of `type`
|
72
|
+
As above, the value of `type` is implied to be `about:blank` by default if the value is ommited, also the value of `title` is filled automatically from the status code. These are described in [Predefined Problem Types](https://tools.ietf.org/html/rfc7807#section-4.2):
|
56
73
|
|
57
74
|
> The "about:blank" URI, when used as a problem type, indicates that the problem has no additional semantics beyond that of the HTTP status code.
|
58
75
|
|
59
76
|
> When "about:blank" is used, the title SHOULD be the same as the recommended HTTP status phrase for that code (e.g., "Not Found" for 404, and so on)
|
60
77
|
|
78
|
+
> ..."about:blank" URI is the default value for that ["type"] member. Consequently, any problem details object not carrying an explicit "type" member implicitly uses this URI.
|
79
|
+
|
61
80
|
But you may also have the need to add some little hint, e.g. as a custom detail of the problem:
|
62
81
|
|
63
82
|
```ruby
|
@@ -68,7 +87,6 @@ will produce:
|
|
68
87
|
|
69
88
|
```json
|
70
89
|
{
|
71
|
-
"type": "about:blank",
|
72
90
|
"title": "Service Unavailable",
|
73
91
|
"status": 503,
|
74
92
|
"detail": "Database not reachable"
|
@@ -103,7 +121,7 @@ will produce(note that `balance` and `accounts` are extention members):
|
|
103
121
|
|
104
122
|
### With Rails
|
105
123
|
|
106
|
-
Once `
|
124
|
+
Once `problem_details-rails` gem is installed into a Rails system, a problem can be rendered with the problem detail form with `Content-Type: application/problem+json`.
|
107
125
|
|
108
126
|
For example, respond with validation error messages:
|
109
127
|
|
@@ -128,7 +146,6 @@ HTTP/1.1 422 Unprocessable Entity
|
|
128
146
|
Content-Type: application/problem+json; charset=utf-8
|
129
147
|
|
130
148
|
{
|
131
|
-
"type": "about:blank",
|
132
149
|
"title": "Unprocessable Entity",
|
133
150
|
"status": 422,
|
134
151
|
"errors": {
|
@@ -139,6 +156,52 @@ Content-Type: application/problem+json; charset=utf-8
|
|
139
156
|
}
|
140
157
|
```
|
141
158
|
|
159
|
+
### With Sinatra
|
160
|
+
|
161
|
+
Install `sinatra-problems_details` into a sinatra app, a problem is be rendered as well with `Content-Type: application/problem+json`.
|
162
|
+
|
163
|
+
#### Classic Application
|
164
|
+
|
165
|
+
```ruby
|
166
|
+
require 'sinatra'
|
167
|
+
require 'sinatra-problem_details'
|
168
|
+
|
169
|
+
get '/' do
|
170
|
+
status 400
|
171
|
+
problem foo: 'bar'
|
172
|
+
end
|
173
|
+
```
|
174
|
+
|
175
|
+
#### Modular Application
|
176
|
+
|
177
|
+
```ruby
|
178
|
+
require 'sinatra/base'
|
179
|
+
require 'sinatra-problem_details'
|
180
|
+
|
181
|
+
class MyApp < Sinatra::Base
|
182
|
+
register Sinatra::ProblemDetails
|
183
|
+
|
184
|
+
get '/' do
|
185
|
+
status 400
|
186
|
+
problem foo: 'bar'
|
187
|
+
end
|
188
|
+
end
|
189
|
+
```
|
190
|
+
|
191
|
+
#### Response
|
192
|
+
|
193
|
+
The sinatra apps defined in the previous sections will render:
|
194
|
+
|
195
|
+
```
|
196
|
+
HTTP/1.1 400 Bad Request
|
197
|
+
Content-Type: application/problem+json
|
198
|
+
|
199
|
+
{
|
200
|
+
"title": "Bad Request",
|
201
|
+
"status": 400,
|
202
|
+
"foo": "bar"
|
203
|
+
}
|
204
|
+
```
|
142
205
|
|
143
206
|
## Development
|
144
207
|
|
data/Rakefile
CHANGED
@@ -6,6 +6,23 @@ require 'rspec/core/rake_task'
|
|
6
6
|
# For multiple gems
|
7
7
|
# ref https://github.com/bkeepers/dotenv/blob/master/Rakefile
|
8
8
|
|
9
|
+
# === helpers ===
|
10
|
+
def source_version
|
11
|
+
@source_version ||= File.read(File.expand_path("../VERSION", __FILE__)).strip
|
12
|
+
end
|
13
|
+
|
14
|
+
# == release ====
|
15
|
+
desc "Commits the version"
|
16
|
+
task :commit_version do
|
17
|
+
sh <<-SH
|
18
|
+
gsed -i "s/.*VERSION.*/ VERSION = '#{source_version}'/" lib/problem_details/version.rb
|
19
|
+
gsed -i "s/.*VERSION.*/ VERSION = '#{source_version}'/" problem_details-rails/lib/problem_details/rails/version.rb
|
20
|
+
gsed -i "s/.*VERSION.*/ VERSION = '#{source_version}'/" sinatra-problem_details/lib/sinatra/problem_details/version.rb
|
21
|
+
SH
|
22
|
+
|
23
|
+
sh "git commit --allow-empty -a -m '#{source_version} release'"
|
24
|
+
end
|
25
|
+
|
9
26
|
namespace 'problem_details' do
|
10
27
|
Bundler::GemHelper.install_tasks name: 'problem_details'
|
11
28
|
end
|
@@ -17,17 +34,47 @@ namespace 'problem_details-rails' do
|
|
17
34
|
def tag_version; end # noop
|
18
35
|
end
|
19
36
|
|
20
|
-
ProblemDetailsRailsGemHelper.install_tasks name: 'problem_details-rails'
|
37
|
+
ProblemDetailsRailsGemHelper.install_tasks dir: File.join(__dir__, 'problem_details-rails'), name: 'problem_details-rails'
|
38
|
+
end
|
39
|
+
|
40
|
+
namespace 'sinatra-problem_details' do
|
41
|
+
class SinatraProblemDetailsGemHelper < Bundler::GemHelper
|
42
|
+
def guard_already_tagged; end # noop
|
43
|
+
|
44
|
+
def tag_version; end # noop
|
45
|
+
end
|
46
|
+
|
47
|
+
SinatraProblemDetailsGemHelper.install_tasks dir: File.join(__dir__, 'sinatra-problem_details'), name: 'sinatra-problem_details'
|
21
48
|
end
|
22
49
|
|
23
50
|
desc 'build gem'
|
24
|
-
task build: ['problem_details:build', 'problem_details-rails:build']
|
51
|
+
task build: ['problem_details:build', 'problem_details-rails:build', 'sinatra-problem_details:build']
|
25
52
|
|
26
53
|
desc 'build and install'
|
27
|
-
task install: ['problem_details:install', 'problem_details-rails:install']
|
54
|
+
task install: ['problem_details:install', 'problem_details-rails:install', 'sinatra-problem_details:build']
|
28
55
|
|
29
56
|
desc 'release'
|
30
|
-
task release: ['problem_details:release', 'problem_details-rails:release']
|
57
|
+
task release: ['problem_details:release', 'problem_details-rails:release', 'sinatra-problem_details:release']
|
31
58
|
|
32
59
|
RSpec::Core::RakeTask.new(:spec)
|
33
|
-
task default: :
|
60
|
+
task default: 'spec:all'
|
61
|
+
|
62
|
+
namespace :spec do
|
63
|
+
dirs = %w(
|
64
|
+
.
|
65
|
+
problem_details-rails
|
66
|
+
sinatra-problem_details
|
67
|
+
)
|
68
|
+
|
69
|
+
desc "Run all specs"
|
70
|
+
task :all do
|
71
|
+
dirs.each do |d|
|
72
|
+
Dir.chdir(d) do
|
73
|
+
Bundler.with_clean_env do
|
74
|
+
sh 'bundle --quiet'
|
75
|
+
sh 'bundle exec rake spec'
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.3
|
@@ -11,7 +11,7 @@ module ProblemDetails
|
|
11
11
|
|
12
12
|
def initialize(params = {})
|
13
13
|
params = params.dup
|
14
|
-
@type = params.delete(:type)
|
14
|
+
@type = params.delete(:type)
|
15
15
|
@status = Rack::Utils.status_code(params.delete(:status)) if params.key?(:status)
|
16
16
|
@title = params.delete(:title) || (@status ? ::Rack::Utils::HTTP_STATUS_CODES[@status] : nil)
|
17
17
|
@detail = params.delete(:detail)
|
@@ -19,7 +19,7 @@ module ProblemDetails
|
|
19
19
|
@extentions = params
|
20
20
|
end
|
21
21
|
|
22
|
-
def
|
22
|
+
def to_hash
|
23
23
|
h = {}
|
24
24
|
%i[type title status detail instance].each do |key|
|
25
25
|
value = public_send(key)
|
@@ -27,9 +27,10 @@ module ProblemDetails
|
|
27
27
|
end
|
28
28
|
h.merge(@extentions)
|
29
29
|
end
|
30
|
+
alias_method :to_h, :to_hash
|
30
31
|
|
31
32
|
def to_json
|
32
|
-
|
33
|
+
to_hash.to_json
|
33
34
|
end
|
34
35
|
end
|
35
36
|
end
|
data/lib/problem_details.rb
CHANGED
data/problem_details.gemspec
CHANGED
@@ -1,27 +1,31 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
|
2
|
+
version = File.read(File.expand_path('VERSION', __dir__)).strip
|
3
3
|
lib = File.expand_path('lib', __dir__)
|
4
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
-
require 'problem_details/version'
|
6
5
|
|
7
6
|
Gem::Specification.new do |spec|
|
8
7
|
spec.name = 'problem_details'
|
9
|
-
spec.version =
|
8
|
+
spec.version = version
|
10
9
|
spec.authors = ['Nobuhiro Nikushi']
|
11
10
|
spec.email = ['deneb.ge@gmail.com']
|
12
|
-
|
13
11
|
spec.summary = 'An implementation of RFC 7807 Problem Details for HTTP APIs'
|
14
12
|
spec.description = spec.summary
|
15
13
|
spec.homepage = 'https://github.com/nikushi/problem_details'
|
16
14
|
spec.license = 'MIT'
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
15
|
+
spec.files = Dir['lib/**/*.rb'] + %w[
|
16
|
+
CHANGELOG.md
|
17
|
+
LICENSE.txt
|
18
|
+
README.md
|
19
|
+
Rakefile
|
20
|
+
problem_details.gemspec
|
21
|
+
VERSION
|
22
|
+
]
|
21
23
|
spec.require_paths = ['lib']
|
22
24
|
|
23
|
-
spec.
|
24
|
-
|
25
|
+
spec.required_ruby_version = '>= 2.0.0'
|
26
|
+
|
27
|
+
spec.add_runtime_dependency 'rack', '>= 1.1.0'
|
28
|
+
spec.add_development_dependency 'bundler', '>= 1.16'
|
25
29
|
spec.add_development_dependency 'rake', '~> 10.0'
|
26
30
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
27
31
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: problem_details
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nobuhiro Nikushi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-09-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -16,26 +16,26 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 1.1.0
|
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
|
-
version:
|
26
|
+
version: 1.1.0
|
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.16'
|
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.16'
|
41
41
|
- !ruby/object:Gem::Dependency
|
@@ -73,15 +73,11 @@ executables: []
|
|
73
73
|
extensions: []
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
|
-
-
|
77
|
-
- ".rspec"
|
78
|
-
- ".travis.yml"
|
79
|
-
- Gemfile
|
76
|
+
- CHANGELOG.md
|
80
77
|
- LICENSE.txt
|
81
78
|
- README.md
|
82
79
|
- Rakefile
|
83
|
-
-
|
84
|
-
- bin/setup
|
80
|
+
- VERSION
|
85
81
|
- lib/problem_details.rb
|
86
82
|
- lib/problem_details/document.rb
|
87
83
|
- lib/problem_details/version.rb
|
@@ -98,15 +94,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
98
94
|
requirements:
|
99
95
|
- - ">="
|
100
96
|
- !ruby/object:Gem::Version
|
101
|
-
version:
|
97
|
+
version: 2.0.0
|
102
98
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
99
|
requirements:
|
104
100
|
- - ">="
|
105
101
|
- !ruby/object:Gem::Version
|
106
102
|
version: '0'
|
107
103
|
requirements: []
|
108
|
-
|
109
|
-
rubygems_version: 2.5.1
|
104
|
+
rubygems_version: 3.0.3
|
110
105
|
signing_key:
|
111
106
|
specification_version: 4
|
112
107
|
summary: An implementation of RFC 7807 Problem Details for HTTP APIs
|
data/.gitignore
DELETED
data/.rspec
DELETED
data/.travis.yml
DELETED
data/Gemfile
DELETED
data/bin/console
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require 'bundler/setup'
|
4
|
-
require 'problem_details'
|
5
|
-
|
6
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
-
# with your gem easier. You can also use a different console, if you like.
|
8
|
-
|
9
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
# require "pry"
|
11
|
-
# Pry.start
|
12
|
-
|
13
|
-
require 'irb'
|
14
|
-
IRB.start(__FILE__)
|