Building your rails API with roda20 May 2016
Sometimes you need to build API serveice in your rails application. Of course you can use popular solutions as grape or rails-api gems. And also you can find real examples in gitlab or rubygems projects.
There’s a list of advantages of this framework:
- Roda provides simple way to working with big count of different plugins;
- You can use any architecture with it;
So I mentioned that roda is fast. If you want to verify this, just check this benchmark repository and you’ll see that roda is really fast. For example, I run benchmarks on locally and get this result:
Roda based on rack. That’s why this integration with rails will be very simple. Firstly you need to add roda gem to your gemfile:
Secondly you need to create simple roda application.
Note that you need to use
The first one needs to JSON responce and the second one provides all REST methods for your application (like
I puted my roda application in to
lib/api/base.rb path but you can use whatever you waht.
After that you need to mount your application in
This is done as well as any other rack application:
And the last part, we need to split our application to different modules with different logic and routes.
For this we have to create required module. In an example I created
Users module with all REST routes:
Finnaly you need to mount our module to roda application. It is very simple too.
That’s all. Now we have a simple roda API application which integrated to our rails app.
Problems / future
In the last part I want to list problems and ideas what I want to solve in future.
- Roda doesn’t have a swagger integrating from the box. Now I’m thinking about using swagger-block gem for it.
- Also roda doesn’t typecast your params. I know that grape uses virtus gem for this. And this feature you should realize by youself too.
On this blog post I wanted to show you that you’re not limited only to popular API server gems. As you can see roda have amazing ideas and properties such as modulatiry, simplicity, speed and stability. Also this framework has a simple way to integration in to your rails application.
Happy hacking! emoji