Skip to content

Latest commit

 

History

History
157 lines (117 loc) · 3.09 KB

README.md

File metadata and controls

157 lines (117 loc) · 3.09 KB

Daredevil

This gem provides insight to api errors by providing messages for all general server error response codes. It is inspired by the json_api_responders gem.

Table of contents

Status

CircleCI

Installation

Add daredevil to your application's Gemfile:

gem 'daredevil'

And then execute:

$ bundle

Inside your base controller, include the module:

module Api
  module V1
    class BaseController < ApplicationController
      include Daredevil
    end
  end
end

Configuration

If you would like to use serializers instead of jbuilder, you can add a config initializer.

Daredevil.configure do |config|
  config.responder_type = :serializers
end

Usage

If a status is not set, the default status will be returned.

class ResourceController
  before_action :set_resource

  def index
    respond_with @resources
  end

  def show
    respond_with @resource
  end

  def create
    if resource.save
      respond_with @resource, status: 201
    else
      respond_with @resource, status: 442
    end
  end

  def update
    if resource.valid?
      respond_with @resource, status: 200
    else
      respond_with @resource, status: 442
    end
  end

  def destroy
    head status: 204
  end

  private

  def set_resource; end
end

If serializers are set as the render method, Daredevil will try to infer which serializer first by checking for a namespaced serializer, then fallback to a non-namespaced serializer.

The following example will look for Api::V1::UserSerializer first, then if not found will look for UserSerializer.

module Api
  module V1
    class UserController
      def index
        respond_with @users
      end
    end
  end
end

Or, specify a serializer specifically:

module Api
  module V1
    class UserController
      def index
        respond_with @users, serializer: KustomUserSerializer
      end
    end
  end
end

Developing

  1. Thank you! We love our contributors!
  2. Clone the repository.
  3. Make your changes in a thoughtfully-named branch.
  4. Ensure 1:1 test coverage.
  5. Submit a Pull Request!
  6. Celebrate! 🎉

License

This project rocks and uses MIT-LICENSE.

Credits

Daredevil is maintained and funded by ProctorU, a simple online proctoring service that allows you to take exams or certification tests at home.