Module: ActionFigure::Formatters::Rfc9457

Includes:
ActionFigure::Formatter
Defined in:
lib/action_figure/formatters/rfc_9457.rb

Overview

Implements RFC 9457 (Problem Details for HTTP APIs) response helpers. Errors render as application/problem+json documents. Success responses mirror the same type/title vocabulary.

Defaults for type and title are derived mechanically from class names and status symbols — deliberately unattractive. Pass type: and title: kwargs to provide stable, documented values your clients can rely on.

Constant Summary collapse

PRIMITIVE_CLASSES =

rubocop:enable Metrics/ParameterLists, Metrics/AbcSize

[Hash, Array, String, Numeric, Symbol,
TrueClass, FalseClass, NilClass].freeze

Constants included from ActionFigure::Formatter

ActionFigure::Formatter::REQUIRED_METHODS

Instance Method Summary collapse

Methods included from ActionFigure::Formatter

#NoContent

Methods included from ErrorRegistry::Helpers

#Conflict, #Forbidden, #Gone, #Locked, #NotFound, #PaymentRequired, #UnavailableForLegalReasons

Instance Method Details

#Accepted(resource: nil, meta: nil, as: nil, type: nil, title: nil) ⇒ Object



27
28
29
30
# File 'lib/action_figure/formatters/rfc_9457.rb', line 27

def Accepted(resource: nil, meta: nil, as: nil, type: nil, title: nil)
  build_success(resource: resource, meta: meta, as: as,
                type: type, title: title, status: :accepted)
end

#Created(resource:, meta: nil, as: nil, type: nil, title: nil) ⇒ Object



22
23
24
25
# File 'lib/action_figure/formatters/rfc_9457.rb', line 22

def Created(resource:, meta: nil, as: nil, type: nil, title: nil)
  build_success(resource: resource, meta: meta, as: as,
                type: type, title: title, status: :created)
end

#error_response(status:, errors: nil, type: nil, title: nil, detail: nil, instance: nil, **extras) ⇒ Object

rubocop:disable Metrics/ParameterLists, Metrics/AbcSize



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/action_figure/formatters/rfc_9457.rb', line 37

def error_response(status:, errors: nil, type: nil, title: nil, detail: nil, instance: nil, **extras)
  name  = action_name_for_error
  word  = status_word(status)
  body  = {}
  body[:type]     = type  || derive_type(name, "#{word}-error")
  body[:title]    = title || rack_status_title(status)
  body[:status]   = ActionFigure.status_code_for(status)
  body[:detail]   = detail   if detail
  body[:instance] = instance if instance
  body[:errors]   = errors   unless errors.nil?
  extras.each { |k, v| body[k] = v }
  { json: body, status: status, content_type: "application/problem+json" }
end

#Ok(resource:, meta: nil, as: nil, type: nil, title: nil) ⇒ Object



17
18
19
20
# File 'lib/action_figure/formatters/rfc_9457.rb', line 17

def Ok(resource:, meta: nil, as: nil, type: nil, title: nil)
  build_success(resource: resource, meta: meta, as: as,
                type: type, title: title, status: :ok)
end

#UnprocessableContent(errors: nil, type: "unprocessable-content-error", **extras) ⇒ Object



32
33
34
# File 'lib/action_figure/formatters/rfc_9457.rb', line 32

def UnprocessableContent(errors: nil, type: "unprocessable-content-error", **extras)
  error_response(status: :unprocessable_content, errors: errors, type: type, **extras)
end