Class: RFC::API::Problem::Payload
- Inherits:
-
Data
- Object
- Data
- RFC::API::Problem::Payload
- Defined in:
- lib/rfc/api/problem/payload.rb
Overview
Models the problem details response payload.
Instance Attribute Summary collapse
-
#extensions ⇒ Object
readonly
Returns the value of attribute extensions.
Class Method Summary collapse
- .for(**attributes) ⇒ Object
- .from_json(body) ⇒ Object
-
.from_xml(body, deserializer: XML::Deserializer) ⇒ Object
:reek:TooManyStatements.
Instance Method Summary collapse
- #add_extension(name, value) ⇒ Object
- #extension?(name) ⇒ Boolean
-
#initialize(title: nil, type: nil, status: nil, detail: nil, instance: nil, extensions: nil) ⇒ Payload
constructor
A new instance of Payload.
- #to_h ⇒ Object
- #to_json ⇒ Object
-
#to_xml(serializer: XML::Serializer, **options) ⇒ Object
:reek:TooManyStatements :reek:FeatureEnvy.
Constructor Details
#initialize(title: nil, type: nil, status: nil, detail: nil, instance: nil, extensions: nil) ⇒ Payload
Returns a new instance of Payload.
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rfc/api/problem/payload.rb', line 45 def initialize title: nil, type: nil, status: nil, detail: nil, instance: nil, extensions: nil super title: title || Rack::Utils::HTTP_STATUS_CODES[status], type: type || "about:blank", status:, detail:, instance:, extensions: extensions || {} end |
Instance Attribute Details
#extensions ⇒ Object (readonly)
Returns the value of attribute extensions
13 14 15 |
# File 'lib/rfc/api/problem/payload.rb', line 13 def extensions @extensions end |
Class Method Details
.for(**attributes) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/rfc/api/problem/payload.rb', line 14 def self.for **attributes status = attributes.delete(:status).then { Rack::Utils.status_code it if it } title = attributes.delete(:title).then { it || Rack::Utils::HTTP_STATUS_CODES[status] } new title:, status:, **attributes end |
.from_json(body) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/rfc/api/problem/payload.rb', line 21 def self.from_json body attributes = JSON body, symbolize_names: true extensions = attributes.reject { |key| PRIMARY_KEYS.include? key } self.for(**attributes.slice(*PRIMARY_KEYS), extensions:) end |
.from_xml(body, deserializer: XML::Deserializer) ⇒ Object
:reek:TooManyStatements
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rfc/api/problem/payload.rb', line 29 def self.from_xml body, deserializer: XML::Deserializer elements = REXML::Document.new(body).root.elements attributes = elements.each_with_object({extensions: {}}) do |element, collection| name = element.name.to_sym text = element.text case name when *PRIMARY_KEYS then collection[name] = text else collection[:extensions].merge! deserializer.call(element) end end self.for(**attributes) end |
Instance Method Details
#add_extension(name, value) ⇒ Object
59 60 61 62 |
# File 'lib/rfc/api/problem/payload.rb', line 59 def add_extension name, value extensions[name] = value self end |
#extension?(name) ⇒ Boolean
64 |
# File 'lib/rfc/api/problem/payload.rb', line 64 def extension?(name) = extensions.key? name |
#to_h ⇒ Object
66 |
# File 'lib/rfc/api/problem/payload.rb', line 66 def to_h = {type:, title:, status:, detail:, instance:, **extensions}.compact |
#to_json ⇒ Object
68 |
# File 'lib/rfc/api/problem/payload.rb', line 68 def to_json(*) = to_h.to_json(*) |
#to_xml(serializer: XML::Serializer, **options) ⇒ Object
:reek:TooManyStatements :reek:FeatureEnvy
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/rfc/api/problem/payload.rb', line 72 def to_xml serializer: XML::Serializer, ** document = REXML::Document.new document.add REXML::XMLDecl.new("1.0", "UTF-8") problem = REXML::Element.new("problem").add_namespace("urn:ietf:rfc:7807") document.add problem to_h.each { |name, value| serializer.call name, value, problem } "".dup.tap { document.write(**, output: it) } end |