Trouble mounting multiple Grape APIs in Rails
grape-api, module, rack, ruby, ruby-on-rails
Solution
In Grape you can mount multiple APIs in another one. That means you can have one "base" class for your APIs and mount all other into it.
Files Structure:
app/
api/
v1/
v1_api.rb
twilio/
twilio_api.rb
api.rb
app/api/api.rb:
require 'v1/v1_api'
require 'twilio/twilio_api'
module API
class Base < Grape::API
mount API::V1
mount API::Twilio
end
end
app/api/v1/v1_api.rb:
module API
class V1 < Grape::API
prefix "v1"
format :json
get :hello do
{ text: 'Hello from V1' }
end
end
end
app/api/twilio/twilio.rb:
module API
class Twilio < Grape::API
prefix "twilio"
format :xml
get :hello do
{ text: 'Hello from Twilio' }
end
end
end
config/routes.rb:
mount API::Base => '/api'
Restart your rails server and you're good to go. Also you should be easily able to autoload files from app/api/twilio and app/api/v1 directories, so you won't have to require them.
Problem
I have two APIs that I'm trying to mount in my Rails app-- one called 'v1' and another called 'twilio'. Each API will be composed of multiple files, so I want each to have its own folder. Inside my app/api directory, I have 2 folders--'v1' and 'twilio'--and a file called 'api.rb' that I am trying to use to mount the two api's. It's contents are: ``` module API class V1 < Grape::API prefix "api" format :json mount API::Root => '/v1' end class Twilio < Grape::API prefix "twilio" format :xml mount API::Twilio_API => '/twilio' end end ``` In the 'v1' directory, I have a file called 'root.rb' that begins as follows: ``` module API class Root < Grape::API version 'v1', :using => :header ... ``` And in the 'twilio' directory, I have a file called 'twilio_api.rb' that begins as: ``` module API class Twilio_API < Grape::API version 'v1', :using => :header ... ``` My routes file has: ``` mount API::V1 => "/" mount API::Twilio => "/" ``` When I start my rails server, I'm getting the error: ``` `load_missing_constant': Expected [My rails app]/app/api/v1/root.rb to define Root (LoadError) ``` I don't understand this, since root.rb certainly does define the Root class. Any help would be much appreciated.