
Security News
npm Introduces minimumReleaseAge and Bulk OIDC Configuration
npm rolls out a package release cooldown and scalable trusted publishing updates as ecosystem adoption of install safeguards grows.
mini_api
Advanced tools
A gem to standardize json responses in Rails applications, highly inspired on Responders
Add this line to your application's Gemfile:
gem "mini_api"
And then execute:
$ bundle
You must install Kaminari to handle pagination and Active Model Serializers to handle data serialization
After install the gem, include the MiniApi module into your ApplicationController or other parent controller
class ApplicationController < ActionController::Base
include MiniApi
end
This include three methods in your controllers: render_json, page and per_page
The methods page and per_page will handle the params: page and per_page respectively
In your controller you only need to call the render_json method informing what you want to send. Example:
class UsersController < ApplicationController
def show
user = User.find(params[:id])
render_json user
end
end
The generated json will be like:
{
"success": true,
"data": { }, // user data here
"message": ""
}
If your data is a ActiveRecord::Relation, the behavior is the same, with pagination data added to meta key
class UsersController < ApplicationController
def index
users = User.all
render_json users
end
end
The response will be like:
{
"success": true,
"data": { }, // users here
"meta": {
"current_page": 1,
"next_page": 2,
"prev_page": null,
"total_pages": 10,
"total_records": 100
}
}
Many times, our controller actions need to persist or validate some data coming from request, the default approach to do that is like:
class UsersController < ApplicationController
def new
user = User.new(user_params)
if user.save
render json: user, status: :created
else
render json: user.errors.messages, status: :unprocessable_entity
end
end
end
But, with mini_api, you could simplify the action doing like:
class UsersController < ApplicationController
def new
user = User.new(user_params)
user.save
render_json user
end
end
If the user was created successfully, then the response will be like:
{
"success": true,
"data": { }, // user data here
"message": "User was successfully created."
}
with status_code 201
But, if user is not valid, then the response will be like:
{
"success": false,
"errors": { }, // user errors here
"message": "User could not be created."
}
witht status_code 422
The message key is different based on actions on informed model: create, update, and destroy
You can respond any type of data, but ActiveRecord/ActiveModel::Model and ActiveRecord::Relation has a special treatment as shown above
It is possible to transform the keys of request and response. By default, will transform to snake_case, but the possible values are snake_case, camel_lower and camel_case
To change the transform operation, simply adds a initializer on initilizations folder with content:
MiniApi::Config.configure do |config|
config.transform_params_keys_to = :snake_case
config.transform_response_keys_to = :camel_lower
end
The option transform_params_keys_to will transform request params.
The option transform_response_keys_to will transform responses.
You can override the status, message and sucess keys simply informing values to render_json. Example:
class UsersController < ApplicationController
def new
user = User.new(user_params)
if user.save
render_json user, message: 'custom message'
else
render_json user.errors.messages, status: :bad_request, success: true
end
end
end
This way, the response will contain the informed values
This plugin handle pagination using kaminari gem. The params to evaluate pagination are page and per_page
if no page is informed, by default will use page 1
Default value of per_page is 25. Only specific values are permitted to per_page, they are: 10, 25, 50, 100
If the value it is none of those, so the default value is used
The gem is available as open source under the terms of the MIT License.
FAQs
Unknown package
We found that mini_api demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
npm rolls out a package release cooldown and scalable trusted publishing updates as ecosystem adoption of install safeguards grows.

Security News
AI agents are writing more code than ever, and that's creating new supply chain risks. Feross joins the Risky Business Podcast to break down what that means for open source security.

Research
/Security News
Socket uncovered four malicious NuGet packages targeting ASP.NET apps, using a typosquatted dropper and localhost proxy to steal Identity data and backdoor apps.