https://github.com/proctoru/daredevil
Add JSON errors to your Rails app.
https://github.com/proctoru/daredevil
proctoru
Last synced: about 1 year ago
JSON representation
Add JSON errors to your Rails app.
- Host: GitHub
- URL: https://github.com/proctoru/daredevil
- Owner: ProctorU
- License: mit
- Created: 2017-07-14T13:42:25.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2018-06-27T18:00:04.000Z (almost 8 years ago)
- Last Synced: 2025-05-15T07:09:02.209Z (about 1 year ago)
- Topics: proctoru
- Language: Ruby
- Size: 48.8 KB
- Stars: 1
- Watchers: 31
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: MIT-LICENSE
Awesome Lists containing this project
README
# 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](https://github.com/stankec/json_api_responders) gem.
## Table of contents
- [Status](#status)
- [Installation](#installation)
- [Configuration](#configuration)
- [Usage](#usage)
- [Developing](#developing)
- [License](#license)
- [Credits](#credits)
## Status
[](https://circleci.com/gh/ProctorU/daredevil)
## Installation
Add `daredevil` to your application's `Gemfile`:
```ruby
gem 'daredevil'
```
And then execute:
```bash
$ bundle
```
Inside your base controller, include the module:
```ruby
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.
```ruby
Daredevil.configure do |config|
config.responder_type = :serializers
end
```
## Usage
If a status is not set, the default status will be returned.
```ruby
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`.
```ruby
module Api
module V1
class UserController
def index
respond_with @users
end
end
end
end
```
Or, specify a serializer specifically:
```ruby
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](https://github.com/ProctorU/daredevil/graphs/contributors)!
1. Clone the repository.
1. Make your changes in a thoughtfully-named branch.
1. Ensure 1:1 test coverage.
1. Submit a [Pull Request](https://github.com/ProctorU/daredevil/pulls)!
1. Celebrate! :tada:
## License
This project rocks and uses MIT-LICENSE.
## Credits
Daredevil is maintained and funded by [ProctorU](https://twitter.com/ProctorU),
a simple online proctoring service that allows you to take exams or
certification tests at home.
ProctorU Engineering & Design