class ProblemDetail::Document

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.

Attributes

title[R]

@!attribute [r] title

@return [String] A short, human-readable summary of the problem type.

type[R]

@!attribute [r] type

@return [URI] A URI reference that identifies the problem type.

Public Class Methods

new(title:, type: 'about:blank', **options) click to toggle source

A problem details object can have some members.

@param title [#to_s] A short, human-readable summary of the problem type.

It SHOULD NOT change from occurrence to occurrence of the problem,
except for purposes of localisation (e.g., using proactive content
negotiation).

@param type [#to_s, nil] A URI reference that identifies the problem type.

When dereferenced, it is encouraged to provide human-readable
documentation for the problem type (e.g., using HTML.  When this member
is not present, its value is assumed to be "about:blank".

@param options [Hash] Extend the problem details with additional members

such as:
* status: the HTTP status code generated by the origin server for this
  occurrence of the problem;
* detail: an human readable explanation specific to this occurrence of
  the problem;
* instance: a URI reference that identifies the specific occurrence of
  the problem.
# File lib/problem_detail.rb, line 30
def initialize(title:, type: 'about:blank', **options)
  @title    = title.to_s
  @type     = URI(type.to_s)
  @options  = options
end

Public Instance Methods

detail() click to toggle source

@return [String, nil] An human readable explanation.

# File lib/problem_detail.rb, line 52
def detail
  @options[:detail]&.to_s
end
instance() click to toggle source

@return [URI, nil] The specific occurrence of the problem.

# File lib/problem_detail.rb, line 57
def instance
  URI(@options[:instance].to_s) unless @options[:instance].nil?
end
options() click to toggle source

@return [Hash] Additional members.

# File lib/problem_detail.rb, line 62
def options
  @options.reject { |k, _v| %i(status detail instance).include?(k) }
end
status() click to toggle source

@return [Fixnum, nil] The HTTP status code generated by the origin server.

# File lib/problem_detail.rb, line 47
def status
  @options[:status]&.to_i
end
to_h() click to toggle source

Properties of the result.

@return [Hash] The properties of the result.

# File lib/problem_detail.rb, line 69
def to_h
  options.merge({
    status:   status,
    detail:   detail,
    instance: instance
  }.reject { |_k, v| v.nil? }).merge(
    title: title,
    type:  type
  )
end