Alias paths/endpoints

474 views
Skip to first unread message

Paul Jolly

unread,
Oct 17, 2013, 11:13:48 AM10/17/13
to ruby-...@googlegroups.com
Is it possible to alias* paths?

e.g. I want a call to /profile to be equivalent to /users/CURRENT_USER_ID. 

module My
  class API < Grape::API

    format :json

    get :profile do
      # ************************************************
      # want this to be handled by /users/CURRENT_USER_ID - is this possible?
      # CURRENT_USER_ID I can resolve from request parameters (e.g. Bearer token)
    end

    resource :users do
      desc "Return a user."
      params do
        requires :id, type: Integer, desc: "User id."
      end
      route_param :id do
        get do
          {id: params[:id]}
        end
      end
    end
  end
end

Indeed I want to go further in that a call to /profile/friends would map to /users/CURRENT_USER_ID/friends

In my poor terminology this would be equivalent to aliasing /profile to /users/CURRENT_USER_ID - which is dynamic.

Note I don't want to redirect here.

Thanks


*this is definitely an abuse of the term, but I can't think of anything more appropriate term 

Daniel Doubrovkine

unread,
Oct 18, 2013, 12:26:28 PM10/18/13
to ruby-...@googlegroups.com
Aliases aren't possible, but you can implement two endpoints like this:

[ "/profile", "/users/:id" ].each do |route|
  get route do
   # code
  end
end

I do suggest though that you do a 301 redirect. We've made this mistake before in our API and this creates nightmare cache scenarios. I vote for a hypermedia API where there's only one authoritative way and URL to represent a single resource.


--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--

dB. | Moscow - Geneva - Seattle - New York
code.dblock.org - @dblockdotorg - artsy.net - github/dblock

Paul Jolly

unread,
Oct 18, 2013, 2:01:04 PM10/18/13
to ruby-...@googlegroups.com

Thanks for the response. Your point on caching is well received: we will reexamine our use case in that light.

You received this message because you are subscribed to a topic in the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ruby-grape/IbWd6B2l_Lk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ruby-grape+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages