In my previous blog post/tutorial, I discussed a coding challenge I was given to build a Ruby RESTful API without using Rails. This last post, I walked through the Ruby gem Sequel. Today, I want to discuss another Ruby gem I had never heard of before this coding challenge: Grape.
Note: While I am discussing this gem, this will not be a tutorial, unlike my previous post on Sequel.
Grape is a “REST-like API framework for Ruby” that is designed to run on Rack or run alongside other Ruby frameworks, such as Rails and Sinatra. When being used alongside other Ruby frameworks, Grape makes it easier to develop RESTful API’s by supplying an easy to use DSL.
If you are like me and still consider yourself a code newbie, you may be thinking what is DSL and what does it have to do with Ruby? When I first read DSL, I immediately thought of internet speed and had no idea what it had to do with anything. Turns out, after lots of googling, that DSL stands for Domain-Specific Language.
Now you must be thinking, what is this gibberish? To keep it simple, a DSL is a programming language that focuses on a specific problem/task. They make it easier for the developer because it removes “extraneous code for a particular task” and allows the developer to “focus specifically on the task at hand.” In layman’s terms, this means that the DSL’s do a lot of the busy/repetitive code in the background so that the developer doesn’t have to.
Well, according to Grape official documentation, it makes it much easier to build a RESTful API because “it has built-in support for common conventions, including multiple formats, subdomain/prefix restriction, content negotiations, versioning and much more.”
Grape also works alongside other Ruby gems that help make a RESTful API even easier. Grape supports CORS via the
rack-cors gem. You could even use Active Model Serializers by installing the
One thing I loved about Grape is that I could still use familiar gems alongside it, which made it even easier for me to follow. There are numerous gems that compliment it, as well as others that are built and maintained specifically to work with Grape, just like the
Within the official documentation of Grape, there is a community projects page that lists all projects currently being built/maintained by Grape that work alongside the gem to help provide an even smoother experience of building a RESTful API.
This page can be found here, but here is a list of a few helpful ones I think are worth noting:
I really enjoyed using Grape. Its simplicity made it really easy to understand and use. I believe Grape is an incredibly useful tool to help build clean and efficient RESTful API’s in Ruby. I really enjoyed how clean my code looked and how self explanatory it all was. I am someone who comments my code quite a bit, so the fact that I didn’t feel the need to do so was a win in and of itself.
I also really like that there are other Grape related gems that add-on functionalities and are built and maintained by the Grape team. They allow people to contribute as well, which is a great idea for anyone who is looking for an open source project.
I hope this quick rundown of Grape intrigues you to use it within a future project. I can't wait to get more experience with it.