Class: ProblemDetail::Document

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(title:, type: 'about:blank', **options) ⇒ Document

A problem details object can have some members.



30
31
32
33
34
# 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

Instance Attribute Details

#titleString (readonly)



39
40
41
# File 'lib/problem_detail.rb', line 39

def title
  @title
end

#typeURI (readonly)



44
45
46
# File 'lib/problem_detail.rb', line 44

def type
  @type
end

Instance Method Details

#detailString?



52
53
54
# File 'lib/problem_detail.rb', line 52

def detail
  @options[:detail]&.to_s
end

#instanceURI?



57
58
59
# File 'lib/problem_detail.rb', line 57

def instance
  URI(@options[:instance].to_s) unless @options[:instance].nil?
end

#optionsHash



62
63
64
# File 'lib/problem_detail.rb', line 62

def options
  @options.reject { |k, _v| i(status detail instance).include?(k) }
end

#statusFixnum?



47
48
49
# File 'lib/problem_detail.rb', line 47

def status
  @options[:status]&.to_i
end

#to_hHash

Properties of the result.



69
70
71
72
73
74
75
76
77
78
# 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