Sha256: cb9fa3bb41d25108913fc49bcfcca9dabd460eadbf6df916cd07b23b01447b23

Contents?: true

Size: 1.84 KB

Versions: 13

Compression:

Stored size: 1.84 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, source: nil)
        render_errors([{
          code: code,
          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

13 entries across 13 versions & 1 rubygems

Version Path
eivo-rails-api-0.1.10 app/controllers/eivo/concerns/rendering.rb
eivo-rails-api-0.1.9 app/controllers/eivo/concerns/rendering.rb
eivo-rails-api-0.1.8 app/controllers/eivo/concerns/rendering.rb
eivo-rails-api-0.1.7 app/controllers/eivo/concerns/rendering.rb
eivo-rails-api-0.1.6 app/controllers/eivo/concerns/rendering.rb
eivo-rails-api-0.1.5 app/controllers/eivo/concerns/rendering.rb
eivo-rails-api-0.1.4 app/controllers/eivo/concerns/rendering.rb
eivo-rails-api-0.1.3 app/controllers/eivo/concerns/rendering.rb
eivo-rails-api-0.1.2 app/controllers/eivo/concerns/rendering.rb
eivo-rails-api-0.1.1 app/controllers/eivo/concerns/rendering.rb
eivo-rails-api-0.1.0 app/controllers/eivo/concerns/rendering.rb
eivo-rails-api-0.0.9 app/controllers/eivo/concerns/rendering.rb
eivo-rails-api-0.0.8 app/controllers/eivo/concerns/rendering.rb