Rails Respond
This package is provided to be used on rails framework and it gives clean methods to handle json response with specific predetermined messages.
Requirement
- Ruby ^1.9
- Rails ^4.2
The package is in process.
Install
Add this line to your application's Gemfile:
gem 'respond'And then execute:
$ bundle
Or install it yourself as:
$ gem install respond
Usage
You can use these methods in deffernt ways:
There are hot ones for quick usage, besides some provided to manage outputs on your own way
require library
require 'respond'create respond instance
respond = Anetwork::Respond.newSome are shown below:
When request succeeds and contains data to return as a result:
respond.succeed(data)When deletion action succeeds:
respond.delete_succeededWhen updating succeeds:
respond.update_succeededWhen insertion succeeds:
respond.insert_succeededWhen deletion action fails:
respond.delete_faildWhen updating fails:
respond.update_faildwhen insertion fails:
respond.insert_faildNot Found Error:
respond.not_foundWhen db connetion is refused:
respond.connection_refusedWhen parameters entered are wrong:
respond.wrong_parametersWhen requested method is not allowed:
respond.method_not_allowedrespond.request_field_notfoundValidation errors:
respond.validation_errors(data)###customization Set custom message for methods is like this:
respond.not_found('Leave it empty or enter your custom message here')And you can do more:
respond.set_status_code(200).set_status_text('succeed').respond_with_message('Your custom message')###render for render the json response , you must use the render method
render :json => resultthe "result" varibule must be instnace of Respond object for example:
render :json => respond.not_foundLicense
The MIT License (MIT). Please see License File for more information.