Sha256: 30108c720a62dda7a38d50a505102cc3fd0c8aee296bea717e29721d31fcaf84

Contents?: true

Size: 966 Bytes

Versions: 3

Compression:

Stored size: 966 Bytes

Contents

# frozen_string_literal: true

require 'json'
require 'rack/utils'

# An object representing RFC 7807 Problem Details
module ProblemDetails
  # The class that implements a Problem Details JSON object described in RFC 7807.
  class Document
    attr_accessor :type, :title, :status, :detail, :instance

    def initialize(params = {})
      params = params.dup
      @type = params.delete(:type) || 'about:blank'
      @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

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

    def to_json
      to_h.to_json
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
problem_details-0.2.1 lib/problem_details/document.rb
problem_details-0.2.0 lib/problem_details/document.rb
problem_details-0.1.0 lib/problem_details/document.rb