1

I'm working on a Rails app that uses grape for its API. Currently, there's a single version of the API that is mounted in the root folder (not v1, as I would like), and I need to start creating v2. The current version is mounted as follows:

/app/api/api.rb:

module API
  class Base < Grape::API
    logger Rails.logger

    mount API::Things         => '/things'
end

and then I have /app/api/api/things.rb with something like:

module API
  class Things < Grape::API
    get '/' do
    end
  end
end

Basically, the API is mounted in /api/things with a hardcoded path. I need to:

  • Make this API the v1 without changing its path.
  • Create a v2 path.

So, I changed api.rb to be:

module API
  class Base < Grape::API
    mount API::V1::Base
    mount API::V2::Base
  end
end

and created api/api/v1/base.rb as follows:

module API
  module V1
    class Base < Grape::API
      version 'v1', using: :accept_version_header

      mount API::Things         => '/things'
    end
  end
end

and then the same for v2:

module API
  module V2
    class Base < Grape::API
      version 'v2'

      mount API::V2::Things         => '/things'
    end
  end
end

My problem is that /things does not inherit the /v2 path component as I would expect. Therefore, since v1 is mounted in /things, v2 fails to mount there.

If I write v2 to use resources instead of paths as v1 is, it works, but I'd like to keep the mounts as is, to make v2 consistent with v1.

pgb
  • 24,813
  • 12
  • 83
  • 113

1 Answers1

0

I think you've misspelled v2, not writing the right quote -> you have 'v2`, not 'v2'...

It might also be cased by 'mount API::Things' as you might want to omit API:: and write 'mount Things'

Maxim Fedotov
  • 1,349
  • 1
  • 18
  • 38
  • It's only misspelled here (just fixed it). The actual code is fine. – pgb Jul 18 '16 at 17:07
  • Yes. Same issue. It seems that for `v2` it's creating the route `/things/v2` instead of `/v2/things`. – pgb Jul 18 '16 at 17:15