Building APIs

There’s a lot of data out there on the Internet. We can access the data that other providers make available by consuming their APIs. An API - or application protocol interface - is a way to serve and access data. Accessing data from other sources allows you to use their data to build on their services, provide additional functionality, and use their data in new and inventive ways. This is an external API.

However, internal APIs are just as common. This makes data more accessible to our own application and associated services, something that’s especially useful when using AJAX and JavaScript frameworks. Building an API in Rails is often a straightforward process, especially when the model is simple, since Rails has tools to make the data available in different formats.

JSON

Data available through APIs, internal or external, often comes in the form of JSON, or JavaScript Object Notation. It’s a standard format used for efficient readability - both for developers and parsers. To create an API in Rails, all we have to do is take the data from a model and serialize it - or turn it into JSON data. We can do that using the .to_json method.

# posts_controller.rb
# ...
 def post_data
   post = Post.find(params[:id])
   render json: post.to_json
 end

Here, Rails takes the post and converts it to JSON to be served. We can serve different formats using respond_to. We can also limit the data we’re serving and include associations as well.

# posts_controller
# ...
 def show
   @post = Post.find(params[:id])
   respond_to do |format|
     format.html { render :show }
     format.json { render json: @post.to_json(only: [:title, :description, :id],
                             include: [author: { only: [:name]}]) }
   end
 end

However, as things get more complicated, we might want to find a cleaner way. We can instead use ActiveModel Serializers. You include and install the gem, run rails g serializer <model name>, and modify the attributes in the new serializer class file. Then, when we respond with JSON data, it serializes the model implicitly.

So now you can just do:

# posts_controller.rb
# ...
def show
   @post = Post.find(params[:id])
   respond_to do |format|
     format.html { render :show }
     format.json { render json: @post}
   end
 end
 

Now when you build a JavaScript front end that wants to make dynamic AJAX requests without reloading, you can do that by accessing these serializer-enabled controller actions.

You can read more about Rails ActiveModel Serializers in the Rails Guides.

Mitul Mistry

Mitul Mistry
I’m Mitul Mistry, a full-stack developer and designer. Contact me here: MitulMistryDev@gmail.com

Angular Rails App

Rails templates are powerful and give us an easy way to build a front end that integrates with our back end. However, Rails templates are...… Continue reading

Intro to Angular

Published on June 02, 2016

Rails Sessions

Published on June 02, 2016