Create API using Grape is new for me, usually i create API with jbuilder or Rails API Skeleton. Before we start, i’m so sorry if my grammar and my explanation is bad. Because it’s my first explanation, and usually i create API with jbuilder, but i will try to the best to explain how to compare routing rails with Grape API.
What is Grape
Grape is a REST-like API 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.
Routing Grape API
Routing Grape API have some different with Rails application, this is the difference:
- Grape API is different engine and run on Rack
Grape API need include
routes.rb if you want run at ruby on rails application.
- Routing is written in the module file
if you take a look code above, you can see a some different how implement rails routes and route grape work, grape need defined a route on same module file, on grape api i’m define a routes at this file
app/api/v1/user.rb if run a server and go to this URL
http://localhost:3000/api/v1/users this will have a result like this
well, grape routing working like rails app, same concept use MVC. I mean when request come, it will go to routes rails first, and Rack will check if the url have prefix “api” and will process to grape API engine, Grape API will find a base URL and will mount the process.
Implementation routing at grape API same like implement route at sinatra freamwork, all route is define at same module and if
path (“/”) is true, the routes will give output that has been specified.
Classy web-development dressed in a DSL (official / canonical repo) - sinatra/sinatra
- has the same properties as routes on rails
In Grape API we also can use
How compare rails and grape route?
You can compare rails and grape route with this way:
- rails framework routing use routes.rb, and will process to controllers, and will processing the request
- grape API routing use mount at routes.rb, when request come, the request will go to routes rails app, and forwarded to grape api module, and after processing at module finish the request will have a some output.
Grape endpoints (classes inheriting from Grape::API) are basically equivalent to Rails controllers. As such, they can contain many unrelated methods (index/show/delete/create). And in my opinion Grape is very suitable for microservice method.