Help us understand the problem. What is going on with this article?

grape-swaggerでgrapeでつくったAPIのドキュメントを自動生成する

More than 5 years have passed since last update.

概要

grapeのドキュメントをSwagger UIで使える形で自動生成します。

Swagger UIとは

こんなんです。

swagger.gif

APIとその説明を列挙し実際にパラメータを入力して叩くことができます。
超便利!

install

rails前提にしてますが、そうじゃなくてもいいかも。

  # Documentation
  gem 'grape-swagger'
  gem 'grape-swagger-ui'

add_swagger_documentationをソースに追加

api.rb
module API
  class Root < Grape::API
    mount API::Cats
    mount API::Dogs
    mount API::Pirates
    add_swagger_documentation
  end
end

確認する

bundle install
rails s 

アクセス
http://localhost:3000/api/swagger

もしv1とかでURLを切っていたら以下をフォームに入力してください。

http://localhost:3000/api/v1/swagger_doc.json

ドキュメントの書き方

こんな感じで書けるようです。詳しくはREADME.mdで。

api.rb
desc "Reserve a virgin in heaven", {
  :notes => <<-NOTE
    Virgins in heaven
    -----------------

    > A virgin doesn't come for free

    If you want to reserve a virgin in heaven, you have to do
    some crazy stuff on earth.

        def do_good
          puts 'help people'
        end

    * _Will go to Heaven:_ Probably
    * _Will go to Hell:_ Probably not
  NOTE
}

まとめ

ドキュメントだけでなく実行環境も手に入り一石二鳥ですね!

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした