JsonAnswerContract
This gem provides some useful controller helper methods that tries to unify JSON responses. For this the following structure is used:
{
success: true/false,
data: {
# The data that you want to return must be there
}
}
Installation
Add this line to your application's Gemfile:
gem 'json_answer_contract'
And then execute:
$ bundle
Or install it yourself as:
$ gem install json_answer_contract
Usage
After installations you may use this gem without some additional onfigurations.
For you gem provides 3 methods:
#success_json_response(data_hash, http_status)
#faillure_json_response(data_hash, http_status)
#build_json_response(success_flag, data_hash, http_status)
First two methods are special cases of the last one.
For example, you want to return json response in UsersController#show
action:
class UsersController < ApplicationController
def show
user = User.find params[:id]
success_json_response user: user.as_json
end
end
Or, maybe you want to return appropriate JSON response for create response in case when validations are failed:
class UsersController < ApplicationController
def create
user = User.new params[:user]
if user.save
# success case
else
failure_json_response({errors: user.errors.full_messages}, 422)
end
end
end
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/alterego-labs/json_answer_contract. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.