Hypermedia API implementation with Grape (or Sinatra)

I'm trying to implement a hypermedia API using Grape installed directly on top of a rack. Grapes support presenters (“entities”), which seem to be a suitable place to provide all related hypermedia.

If I had a Rails router, I could just pick a route around his ears and drop it into the lead logic. For example ( ROAR ):

link :self do article_url(self) end 

But the grapes themselves do not provide easy access to routes, since they do not have names or aliases similar to article_url .

Has anyone encountered a similar problem with Grapes or Sinatra? Is there a simple and easy way to expose resource links?

+4
source share
1 answer

It is possible, but not as simple as the Rails URL Helpers.

From https://github.com/intridea/grape#describing-and-inspecting-an-api :

 TwitterAPI::versions # yields [ 'v1', 'v2' ] TwitterAPI::routes # yields an array of Grape::Route objects TwitterAPI::routes[0].route_version # yields 'v1' TwitterAPI::routes[0].route_description # etc. 
+1
source

Source: https://habr.com/ru/post/1441813/


All Articles