You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have implemented Grape swagger to load all the Grape APIs to save them as documentation and manipulate data using these API. Unfortunately, I am unable to load any API or redirect it. Even when I tried to check routes, I have seen it is unable to mount with the api's under the resources folder. I need some help to solve the issue.
The is the code structure:
app/api/api.rb
require 'grape-swagger'
module API
class Base < Grape::API
include API::ExceptionHandling
version 'v1', using: :path
format :json
prefix :api
add_swagger_documentation(
api_version: 'v1',
hide_documentation_path: true,
mount_path: '/api/v1/swagger_doc',
hide_format: true,
markdown: false,
)
include API::Resources
include CanCan::Ability
include Grape::Kaminari
mount API::V1::Resources::Base
route :any, '*path' do
error!({ error: 'Not Found', details: "No such route '#{request.path}'", status: 404 }, 404)
end
end
end
app/api/api/v1/resources/base.rb
require 'grape-swagger'
module API
module V1
module Resources
class Base < Grape::API
include Grape::Kaminari
version 'v1', using: :accept_version_header
format :json
params do
use :pagination, per_page: 2, max_per_page: 3
end
mount Resources::Users
mount Resources::Orders
mount Resources::Authors
mount Resources::Books
mount Resources::Genres
add_swagger_documentation format: :json,
hide_documentation_path: true,
info: {
title: 'GRAPE API',
description: 'API to manage ' + 'GRAPE API',
},
api_version: 'v1',
version: true,
doc_version: '1.0.0',
mount_path: 'docs'
end
end
end
end
app/api/api/v1/resources/books.rb
module API
module V1
module Resources
class Books < Resources::Base
helpers do
def find_book
Book.find(params[:id])
end
end
resource :books do
desc 'Get all books'
get do
books = Book.all
present paginate(books), with: Entities::Book
end
desc 'Get a specific book'
params do
requires :id, type: Integer, desc: 'ID of the book'
end
get ':id', root: 'book' do
present find_book, with: Entities::Book
end
route_param :id, type: Integer do
desc 'Get authors of a specific book'
get :book_authors do
authors = find_book.authors
present paginate(authors), with: Entities::Author
end
end
desc 'Create a book'
params do
requires :book, type: Hash do
requires :name, type: String, desc: 'Name of the book'
requires :price, type: Float, desc: 'Price of the book'
requires :total_copies, type: Integer, desc: 'Total Copies of the book'
end
end
post do
Book.create!(params[:book])
end
desc 'Update a book'
params do
requires :book, type: Hash do
optional :name, type: String, allow_blank: false
optional :price, type: Float, allow_blank: false
optional :total_copies, type: Integer
end
end
patch ':id' do
book = find_book
book.update!(params[:book])
present book, with: Entities::Book, message: 'Book Successfully Updated'
end
desc 'Delete a book'
params do
requires :id, type: Integer
end
delete ':id' do
book = find_book
if book.destroy
present book, with: Entities::Book, message: 'Book Successfully Deleted'
else
error = { error: book.errors.messages, message: 'Book Failed to Delete' }
present error
end
end
end
end
end
end
end
config/routes.rb
Rails.application.routes.draw do
constraints Clearance::Constraints::SignedIn.new { |user| user.admin? } do
require 'sidekiq/web'
mount Sidekiq::Web => '/sidekiq'
end
# scope '(:locale)', locale: /en|bn/ do
# mount API::Base, at: '/'
# mount GrapeSwaggerRails::Engine => '/swagger'
# end
mount API::Base => '/api'
mount GrapeSwaggerRails::Engine => '/swagger'
end
I have implemented Grape swagger to load all the Grape APIs to save them as documentation and manipulate data using these API. Unfortunately, I am unable to load any API or redirect it. Even when I tried to check routes, I have seen it is unable to mount with the api's under the resources folder. I need some help to solve the issue.
The is the code structure:
app/api/api.rb
app/api/api/v1/resources/base.rb
app/api/api/v1/resources/books.rb
config/routes.rb
config/initializers/swagger.rb
The text was updated successfully, but these errors were encountered: