problem_details 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.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +155 -0
- data/Rakefile +33 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/problem_details.rb +7 -0
- data/lib/problem_details/document.rb +35 -0
- data/lib/problem_details/version.rb +5 -0
- data/problem_details.gemspec +27 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 279ec9da84ba4698847e7ab6cf8ebb09760c8141
|
4
|
+
data.tar.gz: b1f21ed649e8873860fc8b40aa769389789b963e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7c16cc005ec9b2e9f396f6847677fd66fb82d9859c4d17e4a52f74f7ee3c2bcafe6c47d0f220c5ce3565a006bd458f9e28911c6f7a7ac86417ebaff0252ccf9f
|
7
|
+
data.tar.gz: c8956e2c8fffa9ad141fad4b225733c7109372b33c039a7c2f7c2a4d060479367623478554b1962b393141a4ac4af8b28d064c2eb2573fcdae38b655a727a178
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Nobuhiro Nikushi
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
# ProblemDetails [](https://travis-ci.org/nikushi/problem_details)
|
2
|
+
|
3
|
+
ProblemDetails is an implementation of [RFC7807 Problem Details for HTTP APIs](https://tools.ietf.org/html/rfc7807).
|
4
|
+
|
5
|
+
The RFC defines a "problem detail" as a way to inform errors to clients as machine readable form in a HTTP response
|
6
|
+
to avoid the need to define new error response formats for HTTP APIs.
|
7
|
+
|
8
|
+
This library also works with Rails, by the `problem` renderer that helps to respond with the problem detail form.
|
9
|
+
|
10
|
+
Currently only JSON serialization is supported.
|
11
|
+
|
12
|
+
## Features
|
13
|
+
|
14
|
+
* Provides the class that implements a Problem Details JSON Object.
|
15
|
+
* With Rails, automatically adds the renderer to respond with `Content-Type: application/problem+json` which works with `render` in controllers.
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
Add this line to your application's Gemfile:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
gem 'problem_details'
|
23
|
+
```
|
24
|
+
|
25
|
+
Or if you use with Rails, add below line instead.
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
gem 'problem_details-rails'
|
29
|
+
```
|
30
|
+
|
31
|
+
And then execute:
|
32
|
+
|
33
|
+
$ bundle
|
34
|
+
|
35
|
+
## Usage
|
36
|
+
|
37
|
+
### Build a problem
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
require 'problem_details'
|
41
|
+
|
42
|
+
ProblemDetails::Document.new(status: 404).to_json
|
43
|
+
```
|
44
|
+
|
45
|
+
will produce:
|
46
|
+
|
47
|
+
```json
|
48
|
+
{
|
49
|
+
"type": "about:blank",
|
50
|
+
"title": "Not Found",
|
51
|
+
"status": 404
|
52
|
+
}
|
53
|
+
```
|
54
|
+
|
55
|
+
As above, the value of `type` will be presented as `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
|
+
|
57
|
+
> 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
|
+
|
59
|
+
> 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
|
+
|
61
|
+
But you may also have the need to add some little hint, e.g. as a custom detail of the problem:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
ProblemDetails::Document.new(status: 503, detail: 'Database not reachable').to_json
|
65
|
+
```
|
66
|
+
|
67
|
+
will produce:
|
68
|
+
|
69
|
+
```json
|
70
|
+
{
|
71
|
+
"type": "about:blank",
|
72
|
+
"title": "Service Unavailable",
|
73
|
+
"status": 503,
|
74
|
+
"detail": "Database not reachable"
|
75
|
+
}
|
76
|
+
```
|
77
|
+
|
78
|
+
You can build a problem with any additional members which are described as [extension members](https://tools.ietf.org/html/rfc7807#section-3.2).
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
ProblemDetails::Document.new(
|
82
|
+
status: :forbidden,
|
83
|
+
type: 'https://example.com/probs/out-of-credit',
|
84
|
+
balance: 30,
|
85
|
+
accounts: ['/account/12345', '/account/67890'],
|
86
|
+
).to_json
|
87
|
+
```
|
88
|
+
|
89
|
+
will produce(note that `balance` and `accounts` are extention members):
|
90
|
+
|
91
|
+
```json
|
92
|
+
{
|
93
|
+
"type": "https://example.com/probs/out-of-credit",
|
94
|
+
"title": "Forbidden",
|
95
|
+
"status": 403,
|
96
|
+
"balance": 30,
|
97
|
+
"accounts": [
|
98
|
+
"/account/12345",
|
99
|
+
"/account/67890"
|
100
|
+
]
|
101
|
+
}
|
102
|
+
```
|
103
|
+
|
104
|
+
### With Rails
|
105
|
+
|
106
|
+
Once `render_problems-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
|
+
|
108
|
+
For example, respond with validation error messages:
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
# app/controllers/api/posts_controller.rb
|
112
|
+
class Api::PostsController < ApplicationController
|
113
|
+
def create
|
114
|
+
@post = Post.new(params[:post])
|
115
|
+
if @post.save
|
116
|
+
render json: @post
|
117
|
+
else
|
118
|
+
render problem: { errors: @post.errors }, status: :unprocessable_entity
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
```
|
123
|
+
|
124
|
+
With `render problem: { ... }`, generated HTTP response will be like:
|
125
|
+
|
126
|
+
```
|
127
|
+
HTTP/1.1 422 Unprocessable Entity
|
128
|
+
Content-Type: application/problem+json; charset=utf-8
|
129
|
+
|
130
|
+
{
|
131
|
+
"type": "about:blank",
|
132
|
+
"title": "Unprocessable Entity",
|
133
|
+
"status": 422,
|
134
|
+
"errors": {
|
135
|
+
"body": [
|
136
|
+
"can't be blank"
|
137
|
+
]
|
138
|
+
}
|
139
|
+
}
|
140
|
+
```
|
141
|
+
|
142
|
+
|
143
|
+
## Development
|
144
|
+
|
145
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
146
|
+
|
147
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
148
|
+
|
149
|
+
## Contributing
|
150
|
+
|
151
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/nikushi/problem_details.
|
152
|
+
|
153
|
+
## License
|
154
|
+
|
155
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/gem_helper'
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
|
6
|
+
# For multiple gems
|
7
|
+
# ref https://github.com/bkeepers/dotenv/blob/master/Rakefile
|
8
|
+
|
9
|
+
namespace 'problem_details' do
|
10
|
+
Bundler::GemHelper.install_tasks name: 'problem_details'
|
11
|
+
end
|
12
|
+
|
13
|
+
namespace 'problem_details-rails' do
|
14
|
+
class ProblemDetailsRailsGemHelper < Bundler::GemHelper
|
15
|
+
def guard_already_tagged; end # noop
|
16
|
+
|
17
|
+
def tag_version; end # noop
|
18
|
+
end
|
19
|
+
|
20
|
+
ProblemDetailsRailsGemHelper.install_tasks name: 'problem_details-rails'
|
21
|
+
end
|
22
|
+
|
23
|
+
desc 'build gem'
|
24
|
+
task build: ['problem_details:build', 'problem_details-rails:build']
|
25
|
+
|
26
|
+
desc 'build and install'
|
27
|
+
task install: ['problem_details:install', 'problem_details-rails:install']
|
28
|
+
|
29
|
+
desc 'release'
|
30
|
+
task release: ['problem_details:release', 'problem_details-rails:release']
|
31
|
+
|
32
|
+
RSpec::Core::RakeTask.new(:spec)
|
33
|
+
task default: :spec
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
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__)
|
data/bin/setup
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'rack/utils'
|
5
|
+
|
6
|
+
# An object representing RFC 7807 Problem Details
|
7
|
+
module ProblemDetails
|
8
|
+
# The class that implements a Problem Details JSON object described in RFC 7807.
|
9
|
+
class Document
|
10
|
+
attr_accessor :type, :title, :status, :detail, :instance
|
11
|
+
|
12
|
+
def initialize(params = {})
|
13
|
+
params = params.dup
|
14
|
+
@type = params.delete(:type) || 'about:blank'
|
15
|
+
@status = Rack::Utils.status_code(params.delete(:status)) if params.key?(:status)
|
16
|
+
@title = params.delete(:title) || (@status ? ::Rack::Utils::HTTP_STATUS_CODES[@status] : nil)
|
17
|
+
@detail = params.delete(:detail)
|
18
|
+
@instance = params.delete(:instance)
|
19
|
+
@extentions = params
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_h
|
23
|
+
h = {}
|
24
|
+
%i[type title status detail instance].each do |key|
|
25
|
+
value = public_send(key)
|
26
|
+
h[key] = value if value
|
27
|
+
end
|
28
|
+
h.merge(@extentions)
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_json
|
32
|
+
to_h.to_json
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'problem_details/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'problem_details'
|
9
|
+
spec.version = ProblemDetails::VERSION
|
10
|
+
spec.authors = ['Nobuhiro Nikushi']
|
11
|
+
spec.email = ['deneb.ge@gmail.com']
|
12
|
+
|
13
|
+
spec.summary = 'An implementation of RFC 7807 Problem Details for HTTP APIs'
|
14
|
+
spec.description = spec.summary
|
15
|
+
spec.homepage = 'https://github.com/nikushi/problem_details'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
spec.files = `git ls-files|grep -v rails`.split($OUTPUT_RECORD_SEPARATOR).reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
end
|
21
|
+
spec.require_paths = ['lib']
|
22
|
+
|
23
|
+
spec.add_runtime_dependency 'rack'
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
25
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
26
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: problem_details
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nobuhiro Nikushi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-03-26 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: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.16'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.16'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description: An implementation of RFC 7807 Problem Details for HTTP APIs
|
70
|
+
email:
|
71
|
+
- deneb.ge@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- lib/problem_details.rb
|
86
|
+
- lib/problem_details/document.rb
|
87
|
+
- lib/problem_details/version.rb
|
88
|
+
- problem_details.gemspec
|
89
|
+
homepage: https://github.com/nikushi/problem_details
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.5.1
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: An implementation of RFC 7807 Problem Details for HTTP APIs
|
113
|
+
test_files: []
|