Instant REST API with Ruby

So...

...you want to build a RESTfull API from scratch? And you want to do it quickly?

Go off Rails

Ruby always seems to be associated with Rails. And the truth is that this good old MVC framework can also be a solution for an API these days. As of version 5 it supports API only applications. And you can find many articles all over the internet on how to use this beast for. But here I am not going to write another one.

Meet the Grape

I still remember that day when I had found Grape. It was damn good documented and very easy to start with ruby gem. It was also fully equipped with everything one needs around API development and... nothing more.
While this equipped with all the bells and whistles monster Rails machine can be truncated to become Rails::API, Grape seems to be bare simple from the very inside and more properly dedicated tool for API building.
I still wonder why it is not used more widely today in this world of so many APIs desirable everywhere.

Power by Thor

Rails generators always made me happy. And I always missed them while working with Grape. This is why I started looking for where those generators power comes from.
And I had found Thor. This powerfull CLI tool saves a lot of time while bootstraping a brand new Rails application. Why not to use it with something else?

Grape + Thor = grapethor

Every time when I was starting a new API project I found myself in the front of two options. Either to start everything from a scratch and create all the application structure from an empty directory. Or to reuse an existing project - clean up some stuff in tha application directory tree and adjust some config files. Both was not the proper way of making the programming job joyfull - as I used to expect it to be.

This is why I decided to make some effort and created a tool for myself. And because I have never cretaed a ruby gem before, I decided it is a good occasion to do it so. Hopefully someone else but me can make use of it as well.

And this is how the grapethor was born.

Make an API

Grapethor example workflow is quite well documented and also CLI commands format should be familiar not only for regular Rails users.

What you get out of the box are few generators for a kind of progressive API building:

  1. Application generator
    This one must be run in the first place. It creates an application directory with clean directories/files structure within.
  2. API generator
    This should be used directly after the application generator but also can be run whenever we need to create a new (version of an) API. It allows us to prefix api endpoint, organize the api scope in the app directory structure and include the API module within the base application. Actually all off these are done in one go.
  3. Resource generator
    This is well known for Railsers some kind of a scaffold generator. Here it creates controller endpoints, models with entities and test files for a given resource.
  4. (Standalone) Endpoint generator
    Very similiar to resource generator but allows to create a standalone resource endpoint or one within an already existing resource.
Some extra features offered by grapethor are:
  • database integration with migrations (sqlite, mysql, postgresql)
  • ORM integration (activerecord)
  • test framework integration (rspec, minitest)
  • automatic swagger documentation
  • docker support
  • copyrights and software license information included
  • full set of rake tasks for development comfort
  • API console (irb) and server (thin, puma)  - ready to go
The gem project is still in early stage of development but already has enough number of options to be a powerfull starter tool for instant API development. 

And today you can give it a try! :)

Comments