I'm using grape to create an api in rails. The project is still pretty fresh but I've hit a blocking point so far with my routes.
I added an test endpoint to see if my versions were working together and they were
my config/routes.rb
file
Rails.application.routes.draw do
mount Root => '/'
end
my app/api/root.rb
file
require "grape"
class Root < Grape::API
format :json
mount Test::API
end
my app/api/test/api.rb
file
require "grape"
module Test
class API < Grape::API
get :world do
{
"response"=>"hello world!",
"name"=>"it is i"
}
end
end
end
and my api/test/api_spec.rb
file
require 'rails_helper'
describe 'Endpoints' do
context 'when the endpoint "world" is hit' do
context 'the response' do
let(:res) { JSON.parse(response.body)['response'] }
it 'returns hello world' do
get '/world'
expect(res).to eq 'hello world!'
end
end
end
end
All of this worked perfectly well, so I tried adding another mount to my app/api/root.rb
file
require "grape"
class Root < Grape::API
format :json
mount Test::API
mount Endpoints::TodoAPI
end
and my app/api/endpoints/todoapi.rb
file
require "grape"
module Endpoints
class TodoAPI < Grape::API
end
end
when I tried to run my api/test/api_spec.rb
file, I suddenly started getting an error
Failure/Error: mount Endpoints::TodoAPI
NameError:
uninitialized constant Endpoints::TodoAPI
mount Endpoints::TodoAPI
^^^^^^^^^
Did you mean? Endpoints::Todoapi
I tried to make the TodoAPI
look exactly like the api
file, and comment out api
's mount but I keep getting the same error.
Your class name is wrong
class TodoAPI
for file name todoapi.rb should be
class Todoapi
hence the
Did you mean? Endpoints::Todoapi statement.
Class names in ruby follow strict naming conventions and follow the snake case file naming with matching camel case class names principle where words are separated by underscores in the file name then the corresponding words in the class name start with a capital letter
e.g. a file containing the class definition of Class ToDoApi
would have live inside a file named to_do_api.rb