RailsWarp
A Rails plugin for elegant, hash-based response wrapper for clean Rails API responses.
Usage
RailsWarp is automatically included into ActionController and Jbuilder. You don\u2019t need to include any module manually.
- Controllers: call ok and fail directly.
Basic controller examples:
class UsersController < ApplicationController
def show
user = User.find(params[:id])
ok data: { user: user }
end
def create
user = User.new(user_params)
if user.save
ok data: { user: user }, message: "created", code: 201
else
fail message: "validation error", code: 422, data: { errors: user.errors.full_messages }
end
end
endAdd extra fields (merged into the response):
ok data: { user: user }, meta: { request_id: request.request_id }HTTP status mapping:
- 200, 201, 204: returned as-is
- 400: bad_request
- 401: unauthorized
- 403: forbidden
- 404: not_found
- 422: unprocessable_entity
- 500: internal_server_error
Global error handling example:
class ApplicationController < ActionController::Base
rescue_from ActiveRecord::RecordNotFound do |e|
fail message: e.message, code: 404
end
rescue_from StandardError do |e|
fail message: "internal error", code: 500, data: { error: e.class.name }
end
endJbuilder usage (already mixed in, call ok/fail inside templates):
# app/views/users/show.json.jbuilder
json.ok data: { user: { id: @user.id, name: @user.name } }, message: "ok"
# app/views/shared/error.json.jbuilder
json.fail message: "not found", code: 404, data: { resource: "User" }
# extra fields
json.ok data: { list: @items.map { |i| { id: i.id, name: i.name } } }, meta: { total: @items.count }Aliases in Jbuilder: you can also use json.warp_ok and json.warp_fail.
Installation
Add this line to your application's Gemfile:
gem "rails_warp"And then execute:
$ bundleOr install it yourself as:
$ gem install rails_warpResources
Contributing
Contribution directions go here.
License
The gem is available as open source under the terms of the MIT License.