Class: ProblemDetail::Document
- Inherits:
-
Object
- Object
- ProblemDetail::Document
- Defined in:
- lib/problem_detail.rb
Overview
This document defines a “problem detail” as a way to carry machine-readable details of errors in a HTTP response, to avoid the need to define new error response formats for HTTP APIs.
Instance Attribute Summary collapse
-
#title ⇒ String
readonly
A short, human-readable summary of the problem type.
-
#type ⇒ URI
readonly
A URI reference that identifies the problem type.
Instance Method Summary collapse
-
#detail ⇒ String?
An human readable explanation.
-
#initialize(title:, type: 'about:blank', **options) ⇒ Document
constructor
A problem details object can have some members.
-
#instance ⇒ URI?
The specific occurrence of the problem.
-
#options ⇒ Hash
Additional members.
-
#status ⇒ Fixnum?
The HTTP status code generated by the origin server.
-
#to_h ⇒ Hash
Properties of the result.
Constructor Details
#initialize(title:, type: 'about:blank', **options) ⇒ Document
A problem details object can have some members.
28 29 30 31 32 |
# File 'lib/problem_detail.rb', line 28 def initialize(title:, type: 'about:blank', **) @title = title.to_s @type = URI(type.to_s) @options = end |
Instance Attribute Details
#title ⇒ String (readonly)
Returns A short, human-readable summary of the problem type.
37 38 39 |
# File 'lib/problem_detail.rb', line 37 def title @title end |
#type ⇒ URI (readonly)
Returns A URI reference that identifies the problem type.
42 43 44 |
# File 'lib/problem_detail.rb', line 42 def type @type end |
Instance Method Details
#detail ⇒ String?
Returns An human readable explanation.
50 51 52 |
# File 'lib/problem_detail.rb', line 50 def detail @options[:detail].to_s unless @options[:detail].nil? end |
#instance ⇒ URI?
Returns The specific occurrence of the problem.
55 56 57 |
# File 'lib/problem_detail.rb', line 55 def instance URI(@options[:instance].to_s) unless @options[:instance].nil? end |
#options ⇒ Hash
Returns Additional members.
60 61 62 |
# File 'lib/problem_detail.rb', line 60 def @options.reject { |k, _v| %i(status detail instance).include?(k) } end |
#status ⇒ Fixnum?
Returns The HTTP status code generated by the origin server.
45 46 47 |
# File 'lib/problem_detail.rb', line 45 def status @options[:status].to_i unless @options[:status].nil? end |
#to_h ⇒ Hash
Properties of the result.
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/problem_detail.rb', line 67 def to_h .merge({ status: status, detail: detail, instance: instance }.reject { |_k, v| v.nil? }).merge( title: title, type: type ) end |