2/5/2016 - 11:27 PM

Building an API in Ruby on Rails

Building an API in Ruby on Rails

 An API is an “application program interface”, which basically means a computer program that allows other programs to
 communicate with it. In our case, our program will be a web application, and we want to make it easy for other web apps
 to connect to it.
 Example: let’s say I want to build a simple web app that searches Twitter for tweets that contain certain keywords.
 I can use Twitter’s search API to do just that by sending a request for data to their servers and then manipulating or
 displaying that data on my app. There are tons of APIs out there that make their data available to developers for things
 like movie times, restaurant reviews, social media, any pretty much everything else.
 Access to an API is often free of charge, although typically limited to the number of requests you can make in a given time period.
 But what is that process like from the other side? 

 Let’s say you have some data that you want to make available to other developers so they can take advantage of it in their apps.
 Or maybe you have a web app that you want to make available on mobile...Both of these situations are ideal for building
 your very own API.
 Rails::API is a subset of a normal Rails application, created for applications that don't require all functionality that a complete
 Rails application provides. It is a bit more lightweight, and consequently a bit faster than a normal Rails application. 
 The main example for its usage is in API applications only, where you usually don't need the entire Rails middleware stack nor
 template generation.
 What is an API app?

Traditionally, when people said that they used Rails as an "API", they meant providing a programmatically accessible API alongside
their web application. For example, GitHub provides an API that you can use from your own custom clients.

With the advent of client-side frameworks, more developers are using Rails to build a backend that is shared between their web
application and other native applications.

Why use Rails for JSON APIs?

The first question a lot of people have when thinking about building a JSON API using Rails is: "isn't using Rails to spit out
some JSON overkill? Shouldn't I just use something like Sinatra?"

For very simple APIs, this may be true. However, even in very HTML-heavy applications, most of an application's logic is actually
outside of the view layer.

The reason most people use Rails is that it provides a set of defaults that allows us to get up and running quickly without having
to make a lot of trivial decisions.

For example, Twitter uses its public API in its web application, which is built as a static site that consumes JSON resources.

Instead of using Rails to generate dynamic HTML that will communicate with the server through forms and links, many developers
are treating their web application as just another client, consuming a simple JSON API.


 TO WATCH : https://www.youtube.com/watch?v=36M2BSA2LYk
 TO BUILD: https://www.airpair.com/ruby-on-rails/posts/building-a-restful-api-in-a-rails-application
 TO KNOW: https://github.com/rails-api/rails-api