How to build named helper using Grape on rails

691 Views Asked by At

I'm using grape gem; ref https://github.com/intridea/grape.

Could you show me how to build named path like "twitter_api_v1_statuses_path" ?

My code is as follows

module Twitter
  class API < Grape::API
    version 'v1', using: :header, vendor: 'twitter'
    format :json
    prefix :api

    resource :statuses do
      desc "Return a public timeline."
      get :public_timeline do
        Status.limit(20)
      end
    end
end
1

There are 1 best solutions below

1
On

I'm assuming that you want an URL like this http://yourdomain.com/api/v1/statuses/public_timeline. In this scenario, you have only one problem in your API class and it's related to the versioning strategy you've chosen. The :header strategy search for the API version in a specific header and that's not what you're looking for. Change it to :path.

module Twitter
    class API < Grape::API
        version 'v1', using: :path, vendor: 'twitter'
        format :json
        prefix :api

        resource :statuses do
            desc "Return a public timeline."
            get :public_timeline do
                Status.limit(20)
            end
        end
    end
end