#API Server Gem
This is a gem used for converting a any controller in a JSON REST API. If you have a Rails application as a client, it is recommended to use https://github.com/kopz9999/e_api_client to consume resources
You just need to include the module EApiServer::Web::JSON::ServiceControllable in your controller.
class ApiCarsController < ApplicationController
include EApiServer::Web::JSON::ServiceControllable
end
This include will add the following actions to your controller:
- POST create - /{controller_name}
- DELETE destroy - /{controller_name}/1
- GET index - /{controller_name}
- GET show - /{controller_name}/1
- PATCH/PUT update - /{controller_name}/1
Only allow a trusted parameter "white list" through. If a single resource is loaded for #create or #update, then the controller for the resource must implement the method "#{resource_name}_params" to limit permitted parameters for the individual model.
Example:
class ApiCarsController < ApplicationController
include EApiServer::Web::JSON::ServiceControllable
protected
def get_resource_params
params.require(:api_car).permit(:id, :name, :enrollment)
end
end
By default, the module will try to infer the following properties
- resource_class = The resource class based on the controller
- @return [Class]
- Default: Classified name for the controller based on the classified transformation of the string property resource_name. For ApiCarsController, default will be ApiCar
- Override with the following method: get_resource_class
- resource_name = The singular name for the resource class based on the controller
- @return [String]
- Default: Singularized name of the current controller. For ApiCarsController, default will be api_car
- Override with the following method: get_resource_name
- query_params = Allowed parameters for searching on index action. Use your own blacklists if not using strong params
- @return [Hash]
- Default: {}
- Override with the following method: get_query_params
- Request Examples:
- { id: 32 }
- { name: "Nissan Altima" }
- Permit Params Examples:
- params.permit(:name)
- page_params = Allowed parameters for pagination on index action
- @return [Hash]
- Default: params.require(:pagination).permit( :page, :page_size )
- Override with the following method: get_page_params( required_params ). Here required_params arg is params.require(:pagination)
- Request Examples:
- { pagination: { page: "1", page_size: "3" } }
- order_params = Allowed parameters for ordering on index action. Use your own blacklists if not using strong params
- @return [Hash]
- Default: {}
- Override with the following method: get_order_params( required_params ). Here required_params arg is params.require(:ordering)
- Request Examples:
- { ordering: { name: "asc", enrollment: "desc" } }
If you are processing too many queries in the index, it is recommended that you change it to POST index in the routes.
Other methods you may want to override are:
# Override with your own implementation if you want to change it for a JBuilder template
# @return [Action]
def success_show
render json: get_resource_params
end
If you still have doubts about how to query the index, please check the controller test in /test/dummy/test/controllers/api_cars_controller_test.rb
For Rails >= 4.2 please add the responders gem on your gemfile:
gem 'responders', '~> 2.0'
When you test your API, make sure you request it with the correct format. Assumming you are running your API on port 3000, for ApiCarsController#index, you must request: http://localhost:3000/api_cars.json
Remember, the suffix .json is the format of the request
- Support for Batch requests
#Copyright 2015 Kyoto Kopz [email protected]
#License
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.