No commit activity in last 3 years
No release in over 3 years
Small library with controller's helper methods for providing json response in single format.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.11
~> 10.0
~> 3.0

Runtime

>= 4.0
 Project Readme

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.