Sha256: 20aa67972b82de08d1a3925adef9fbec76ff1f8fc6c8ceca6c1fa782a02a1bcf

Contents?: true

Size: 1.91 KB

Versions: 4

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

module EIVO
  module Concerns
    module Rendering
      extend ::ActiveSupport::Concern

      def render_success(serializer = nil)
        if serializer
          render json: serializer.serialized_json
        else
          data = {
            data: nil
          }
          render json: ::MultiJson.dump(data)
        end
      end

      def render_unauthorized(_exception = nil)
        render_error 'unauthorized', status: :unauthorized
      end

      def render_forbidden(_exception = nil)
        render_error 'forbidden', status: :forbidden
      end

      def render_not_found(_exception = nil)
        render_error 'not_found', status: :not_found
      end

      def render_internal_server_error(_exception)
        render_error 'internal_server_error', status: :internal_server_error
      end

      def render_parameter_missing(exception)
        render_error 'parameter_missing', source: { parameter: exception.param }
      end

      def render_model_errors(errors)
        json_errors = errors.details.map do |attribute, attribute_errors|
          attribute_errors.map do |error|
            {
              code: error[:error],
              source: {
                parameter: attribute
              },
              status: ::Rack::Utils::SYMBOL_TO_STATUS_CODE[:bad_request].to_s
            }
          end
        end

        render_errors json_errors.flatten
      end

      def render_error(code, status: :bad_request, title: nil, detail: nil, source: nil)
        render_errors([{
          code: code,
          title: title,
          detail: detail,
          source: source,
          status: ::Rack::Utils::SYMBOL_TO_STATUS_CODE[status].to_s
        }.compact], status: status)
      end

      def render_errors(errors, status: :bad_request)
        data = {
          errors: errors
        }
        render json: ::MultiJson.dump(data), status: status
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
eivo-rails-api-0.1.14 app/controllers/eivo/concerns/rendering.rb
eivo-rails-api-0.1.13 app/controllers/eivo/concerns/rendering.rb
eivo-rails-api-0.1.12 app/controllers/eivo/concerns/rendering.rb
eivo-rails-api-0.1.11 app/controllers/eivo/concerns/rendering.rb