Rails Grape show 404 page - ruby-on-rails

I am a beginner in Grape, I want to show my api list by using swagger-ui.
I put swagger html in public/swagger, and I access localhost:3000/swagger
However, it keep showing 404 not found. I thought it's causing by Grape configuration.
Here is api.rb
#app/api/twitter/api/api.rb
require 'grape'
module Twitter
class API < Grape::API
version 'v1', using: :header, vendor: 'twitter'
format :json
prefix :api
add_swagger_documentation
resource :statuses do
desc 'Return a public timeline.'
get :public_timeline do
Status.limit(20)
end
end
end
end

You don't have to do it manually by putting swagger html somewhere.
You could use gem grape-swagger provided by Grape team, add two lines in you config.ru.
require 'grape-swagger'
module API
class Root < Grape::API
format :json
...
add_swagger_documentation
end
end
then you could access swagger doc at http://localhost:3000/swagger_doc

Related

Unable to generate documentation with grape-swagger

I am trying to generate the documentation with grape-swagger.
On my gem file
gem 'grape-entity'
gem 'grape-swagger'
gem 'grape-swagger-entity'
gem 'grape-swagger-rails'
On my endpoint
require 'grape-swagger'
module MyModule
class Api < Grape::API
content_type :json, 'application/json'
default_format :json
format :json
mount V1::Root
add_swagger_documentation
end
end
When I am visiting http://localhost:3000/swagger_doc I am getting an error,
No route matches [GET] "/swagger_doc".
Also I don't see any doc generated.
add the prefix if you have mounted a place other than root.
for example /api/swagger_doc

Grape add new endpoint

I'm using Rails 6 with Grape as API. I'm pretty new in Grape and I'm trying to learn how to add new endpoint using Grape. The idea is to get Index endpoint which is nested in v1/users/index
Here is my structure:
app/
controllers/
api/
root.rb - API::Root
v1/
base.rb - API::V1::Base
users/
base.rb - API::V1::Users::Base
index.rb - API::V1::Users::Index
api/root.rb
module API
class Root < Grape::API
default_format :json
prefix :api
# exception handling
include Rescuers
# helpers
helpers ::API::Helpers::ParamsHelper
# core API modules
mount V1::Base
end
end
api/v1/base.rb:
module API
module V1
class Base < Root
version 'v1', using: :path
content_type :json, 'application/vnd.api+json'
# mount resource modules
mount V1::Users::Base
end
end
end
api/v1/users/base.rb:
module API
module V1
module Users
class Base < Grape::API
version 'v1', using: :path
content_type :json, 'application/vnd.api+json'
# mount resource modules
mount Users::Index
end
end
end
end
api/v1/users/index.rb:
module API
module V1
module Users
class Index < Grape::API
desc 'Test'
get do
head 200
end
end
end
end
end
Here are my routes:
Rails.application.routes.draw do
# API
scope :api do
mount API::Root, at: '/'
end
end
I want to have this index.rb in my routes as GET v1/users/index but when I type rake routes I don't see it. This should not do anything, I want to understand what is the core requirements when it comes to creation endpoint with Grape.

Inheritance is not working in grape

I am using grape redtful-api. I am Unable to inherit common_params in Grape. I defined common
_params in class API1 and called it in API2 throws the error. How can I change the code to make this work?
module Example
class API1 < Grape::API
version 'v1'
format :json
prefix :api
resource :exc1 do
common_params = proc do
requires :param1
requires :param2
end
params(&common_params)
get :params_by_pair do
p1 = params[:param1]
p2 = params[:param2]
response = "https://www.example1.com/#{p1}_#{p2}"
end
end
end
end
module Example
class API2 < API1
version 'v1', using: :header, vendor: 'twitter'
format :json
prefix :api
resource :exc2 do
params(&common_params)
get :params_by_pair do
p1 = params[:param1]
p2 = params[:param2]
response = "https://www.example2.com/#{p1}_#{p2}"
end
end
end
end
The issue doesn't have much to do with Grape but rather the way variables' scope works in Ruby. common_params is just a local, it won't survive the end of the scope. You could make it work by using a class instance variable or similar but let's not go there. The way you're supposed to share helpers across different grapes is through a dedicated module.
module Example
module SharedHelpers
extend Grape::API::Helpers
params :common_params do
requires :param1
requires :param2
end
end
end
And now in the different grapes you need to "include" the module and use the helper.
module Example
class API1 < Grape::API
helpers SharedHelpers # !!!
version 'v1'
format :json
prefix :api
resource :exc1 do
params do
use :common_params # !!!
end
get :params_by_pair do
...
end
end
end
end
To use the helpers in the API2 grape, use the same technique.

How to build named helper using Grape on rails

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
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

Rails Grape api versioning module structure

I'm trying to implement api versioning, almost the same as I've done here . but i don't seem to get module/folder structure right in rails app, because I get error messages like V1 is not a module /app/api/v1/xml_responses/device.rb:3:in '<module:API>'
Directory structure
/app
/api
- api.rb
/v1
-base.rb
/xml_responces
- device.rb
api.rb
require 'v1/base.rb'
module API
class Base < Grape::API
mount API::V1 => '/v1/'
end
end
v1/base.rb
module API
module V1
class ApiV1 < Grape::API
require 'builder'
helpers DeviceMethods
prefix 'api'
version 'v1', using: :header
end
end
end
V1/xml_responses/device.rb
module API
module V1
module XMLResponses::Device
def self.do_something
#do_something
end
end
end
end
Routes.rb
mount API::Base => '/'
I can't figure out what i'm doing wrong! could you please help me?
I was having similar problems, but then stumbled on this great post that helped me get things to work, and had more complete information than I found elsewhere. See http://funonrails.com/2014/03/building-restful-api-using-grape-in-rails/
Looking at your code, this looks funny:
module XMLResponses::Device
def self.do_something
Do you mean to do something like this?
module API
module V1
module XMLResponses
class Device < Grape::API
resource :device do
get do { Device.all } # Or whatever
end
end
end
end
end
Make sure that you have this line in application.rb
config.paths.add "app/api", glob: "**/*.rb"
config.autoload_paths += Dir["#{Rails.root}/app/api/*"]
As suggested at grape wiki here.

Resources