batch_request_api
Rails middleware gem to achieve Batch creates, updates and deletes.
- Customizable middleware
- Batch create, update and delete records sequentially or in parallel
Installation
Add this line to your application's Gemfile:
gem 'batch_request_api'
Or install it yourself as:
$ gem install batch_request_api
Overview
After installing the gem, you get the middleware which will intercept requests to the following urls.
- batch_sequential
(/api/v1/batch_sequential)
- batch_parallel
(/api/v1/batch_parallel)
To use custom URLs, add a configuration block to your app initialization. Example:
BatchRequestApi.configure do |config|
config.batch_sequential_paths = ['/api/v1/batch_sequential']
config.batch_parallel_paths = ['/api/v1/batch_parallel']
end
API endpoint can be disabled by setting the path to a falsy value (nil
/false
).
Sequential Usage
This is the simplest way to implement batch. One network request to /api/v1/batch_sequential
containing the batched payload will work with a regular rails controller.
Parallel Usage
This requires your controller to iterate and apply a transaction. One network request to /api/v1/batch_parallel
containing the batched payload will need a code similar to this sample.
The batch request payload is available on the controller using params['json']
Batch Client
- Ember Addon (Ideal)
- Ruby Client
We expect that you will probably use the Ember Add on with this gem to make the batch request and receive a response.
If not, no worries we have built a sample Ruby Client for that reason.
Here are the sample payloads that the middleware expects for create/update/delete. The ruby client constructs the format for create action.
Batch Request Payload:
"requests": [
{
"method": "POST",
"url": "/api/v1/movies",
"body": { }, { }
}
]
}
Batch Request Response:
[
{"status"=>200, "headers"=>{}, "response"=>{}},
{"status"=>200, "headers"=>{}, "response"=>{}}
]
Contributing
If you would like to contribute, you can fork the project, edit, and make a pull request.