You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: sinja.rb
    path of file: ./repos/sinja/lib
    
    the code of the file until where you have to start completion: # frozen_string_literal: true
require 'set'

require 'active_support/inflector'
require 'sinatra/base'
require 'sinatra/namespace'

require 'sinja/config'
require 'sinja/errors'
require 'sinja/helpers/serializers'
require 'sinja/resource'
require 'sinja/version'

module Sinja
  DEFER_CODE = 357
  MIME_TYPE = 'application/vnd.api+json'
  ERROR_CODES = ObjectSpace.each_object(Class).to_a
    .keep_if { |klass| klass < HttpError }
    .map! { |c| [(c.const_get(:HTTP_STATUS) rescue nil), c] }
    .delete_if { |s, _| s.nil? }
    .to_h.freeze

  def self.registered(app)
    abort "Sinatra::JSONAPI (Sinja) is already registered on #{app}!" \
      if app.respond_to?(:_s
    
  def self.registered(app)
    abort "Sinatra::JSONAPI (Sinja) is already registered on #{app}!" \
      if app.respond_to?(:_sinja)

    app.register Sinatra::Namespace

    app.disable :protection, :show_exceptions, :static
    app.set :_sinja, Sinja::Config.new
    app.set :_resource_config, nil # dummy value overridden in each resource

    app.set :actions do |*actions|
      condition do
        actions.each do |action|
          raise ForbiddenError, 'You are not authorized to perform this action' \
            unless can?(action)
          raise MethodNotAllowedError, 'Action or method not implemented or supported' \
            unless respond_to?(action)
        end

        true
      end
    end

    app.set :qcaptures do |*index|
      condition do
        @qcaptures ||= []

        index.to_h.all? do |key, subkeys|
          key = key.to_s

          params[key].is_a?(Hash) && params[key].any? && Array(subkeys).all? do |subkey|
            subkey = subkey.to_s

            # TODO: What if deleting one is successful, but not another?
            # We'll need to restore the hash to its original state.
            @qcaptures << params[key].delete(subkey) if params[key].key?(subkey)
          end.tap do |ok|
            # If us deleting key(s) causes the hash to become empty, delete it.
            params.delete(key) if ok && params[key].empty?
          end
        end
      end
    end

    app.set :qparams do |*allow_params|
      allow_params = allow_params.to_set

      abort "Unexpected query parameter(s) in route definiton" \
        unless allow_params.subset?(settings._sinja.query_params.keys.to_set)

      condition do
        params.each do |key, value|
          key = key.to_sym

          # Ignore interal Sinatra query parameters (e.g. :captures) and any
          # "known" query parameter set to `nil' in the configurable.
          next if !env['rack.request.query_hash'].key?(key.to_s) ||
            settings._sinja.query_params.fetch(key, BasicObject).nil?

          raise BadRequestError, "`#{key}' query parameter not allowed" \
            unless allow_params.include?(key)

          next if env['sinja.normalized'] == params.object_id

          if value.instance_of?(String) && settings._sinja.query_params[key] != String
            params[key.to_s] = value.split(',')
          elsif !value.is_a?(settings._sinja.query_params[key])
            raise BadRequestError, "`#{key}' query parameter malformed"
          end
        end

        return true if env['sinja.normalized'] == params.object_id

        settings._sinja.query_params.each do |key, klass|
          next if klass.nil?

          if respond_to?("normalize_#{key}_params")
            params[key.to_s] = send("normalize_#{key}_params")
          else
            params[key.to_s] ||= klass.new
          end
        end

        # Sinatra re-initializes `params' at namespace boundaries, so we'll
        # reference its object_id in the flag to make sure we only re-normalize
        # the parameters when necessary.
        env['sinja.normalized'] = params.object_id
      end
    end