Class: ProblemDetails::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/problem_details/document.rb

Overview

The class that implements a Problem Details JSON object described in RFC 7807.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Document

Returns a new instance of Document.



12
13
14
15
16
17
18
19
20
# File 'lib/problem_details/document.rb', line 12

def initialize(params = {})
  params = params.dup
  @type = params.delete(:type)
  @status = Rack::Utils.status_code(params.delete(:status)) if params.key?(:status)
  @title = params.delete(:title) || (@status ? ::Rack::Utils::HTTP_STATUS_CODES[@status] : nil)
  @detail = params.delete(:detail)
  @instance = params.delete(:instance)
  @extentions = params
end

Instance Attribute Details

#detailObject

Returns the value of attribute detail.



10
11
12
# File 'lib/problem_details/document.rb', line 10

def detail
  @detail
end

#instanceObject

Returns the value of attribute instance.



10
11
12
# File 'lib/problem_details/document.rb', line 10

def instance
  @instance
end

#statusObject

Returns the value of attribute status.



10
11
12
# File 'lib/problem_details/document.rb', line 10

def status
  @status
end

#titleObject

Returns the value of attribute title.



10
11
12
# File 'lib/problem_details/document.rb', line 10

def title
  @title
end

#typeObject

Returns the value of attribute type.



10
11
12
# File 'lib/problem_details/document.rb', line 10

def type
  @type
end

Instance Method Details

#to_hashObject Also known as: to_h



22
23
24
25
26
27
28
29
# File 'lib/problem_details/document.rb', line 22

def to_hash
  h = {}
  %i[type title status detail instance].each do |key|
    value = public_send(key)
    h[key] = value if value
  end
  h.merge(@extentions)
end

#to_jsonObject



32
33
34
# File 'lib/problem_details/document.rb', line 32

def to_json
  to_hash.to_json
end