API, an abbreviation of application program interface, is a set of practices, protocols, and tools for building software applications. Sometimes we me may need third party integration for our web application. To achieve it REST API is an easy thing. Advantage of Rest API will be language independent.
REST (REpresentational State Transfer) is a simple architecture that runs over HTTPS or TLS. The REST style emphasizes that interactions between clients and services are enhanced by having a limited number of operations.
Flexibility is provided by allocating resources their own sole universal resource indicators (URIs). Because each operation (GET, POST, PUT, and DELETE) has a specific meaning, REST avoids uncertainty.
Building a simple REST API:
API do not need any view as user do not interact with the application directly. We just specify the data that third party will send.
There will be no view or edit action as we don’t have any view here. For Every request will return some data with status code and success failure message. The data may in the form of json or xml.
Requirement : gem ‘rails-api’
While creating application create as rails-api new [application_name], so that it avoids view, assets/stylesheets and assets/javascript.
We have to namespace our API which keeps our code clean and keeps your APIs independent from the rest of your controllers. We start this by adding in our routes.
routes.rb
namespace :api do
resources :users
end
It will look for a UsersController in app/controllers/api/users_controller.rb
class api::UsersController < ApplicationController
end
I hope you all are familiar with simple CRUD operations, do the same here but it wont have any view so instead redirection we need to render some data with status code.
For Ex.
class Api::UsersController < ApplicationController
def index
@users = User.all
respond_to do |format|
format.json { render json: @users }
format.xml { render xml: @users }
end
enddef destroy
respond_to do |format|
if @user.destroy
format.json { head :no_content, status: :Deleted Successfully }
format.xml { head :no_content, status: :Deleted Successfully }
else
format.json { render json: @user.errors, status: :Unable }
format.xml { render xml: @user.errors, status: :Unable }
end
end
endend
In this way we can build a REST API.
If you need to add any new feature, any changes in your API you can make a versions of your API.
Versioning of API
In routes.rb
namespace :api do
namespace :v1 do
resources :users
end
end
It will look for a UsersController in
app/controllers/api/v1/users_controller.rb
class Api::V1::UsersController < ApplicationController
end
Finally provide the API Documentation for the users
API Requests:
Listing users
url: http://localhost:3000/api/users
method: GET
Deleting User
url: http://localhost:3000/api/users/:id
method: DELETE
RailsCarma, a branded and reputed Global Rails Company in providing high end RoR development services over a decade. It provides updated and high-level solutions for all RoR services.
Read similar articles :