Ruby on Rails
January 27, 2021
Rails is a go-to web framework when you want to create a complex web application. But when it comes to APIs there are many players like Roda, Sinatra, Padrino, Grape and, Rails API. Every framework has its pros and cons and it is up to us to choose the best based on the application's requirement. In this article, we will be looking into Grape "An opinionated framework for creating REST-like APIs in Ruby" and the Rails API, and try to understand how we can choose best for our API.
Let's first discuss what are the perks of using each of them one by one.
Typically when someone talks about 'Rails API' they meant accessible API along with their web application. But with the increasing popularity of client-side frameworks, many developers are using Rails to build a back-end that instead of using forms and HTML, just throws a JSON response.
You may think "isn't using Rails to spit out some JSON overkill?". Shouldn't you just use something like Sinatra or Grape? For a very simple API, it may be true but in most cases, it's not.
Rails API was released in 2012, let's first see the benefits that come with Rails API only application. I will just name them here as they are out of the scope for this topic.
And many more. You can check the complete list of features on this link.
You may not have thought about which parts of Rails are still applicable for your API only application, but the answer turns out to be most of it.
Grape is a REST-like API micro-framework for Ruby. It's designed to run on Rack or complement existing web application frameworks such as Rails and Sinatra by providing a simple DSL to easily develop RESTful APIs. It has built-in support for common conventions, including multiple formats, subdomain/prefix restriction, content negotiation, versioning, and much more.
It was released in 2010 and best suited for API-only apps. If you think at some point in the future the scope of your app will grow to a point where you need to have something more than just JSON API (some forms or HTML pages), it's better to not use Grape.
Grape: around 2000 hits/sec
Rails API: around 1000 hits/sec
There are areas where the decision between frameworks isn't clear cut for example:
Performance - while it appears Grape outperforms Rails API, it's possible removing unused middlewares from a Rails API app may deliver a similar performance as the Grape app.
Shared logic - if a new service shares some logic with an existing Rails app (database access, ActiveRecord models, etc), you are frequently left with making the best of not-perfect decisions on how to handle that.
There are some sweet spots for each of these frameworks:
Rails API - you have an existing Rails app and are looking for a simpler, moderately faster approach to delivering a JSON API than go with Rails API.
Grape - you've outgrown a homegrown API app and need a more advanced framework dedicated to delivering JSON APIs.
To conclude this topic I'd just say that depending on the scope of your application and the facts discussed above chose the best framework that suits your needs.
A universally unique identifier (UUID) is a 128-bit number used to identify information in computer systems. Sometimes it is referred to as a 'globally unique identifier'.
Stripe is a tool that you can use to handle online payments, but there’s a lot more in it. It is one of the topmost payment methods of the market today.