Building an API with Ruby on Rails

In the lifecycle of web applications, there are also times when there is a big need for third-party integrations. The simplest way for this is exposing consumption for REST API.
Building an API with Ruby on Rails will correspond to an application of task management. It contains User model that will represent users who have system access, Project model representing projects, and To-do model to represent specific tasks needed to be accomplished in a project. Users may have a lot of Projects and Projects can have more To-dos.
Basic API in Rails
Routes
Rails is providing an excellent tool in defining endpoints through routes.
1. ApiDemoApplication::App.routes.draw do
2. scope ‘/api’ do
3. scope ‘/v1’ do
4. scope ‘/projects’ do
5. get ‘/’ => ‘api_projects#index’
6. post ‘/’ => ‘api_projects#create’
7. scope ‘/:name’ do
8. get ‘/’ => ‘api_projects#show’
9. put ‘/’ => ‘api_projects#update’
10. scope ‘/todos’ do
11. get ‘/’ => ‘api_todos#index’
12. post ‘/’ => ‘api_todos#create’
13. scope ‘/:todo_name’ do
14. get ‘/’ => ‘api_todos#show’
15. put ‘/’ => ‘api_todos#update’
16. end
17. end
18. end
19. end
- Naming
- Versioning
- Route Parameters
Controllers
API controllers are useful in handling authentication and extracting common API functions. Common approach is requiring re-authentication.
- BaseController and Authentication
- Security
- Projects Controller
- Defensive Programming
- HTTP Status Codes
- Code DRY
Read full article on : Building an API with Ruby on Rails