Rails で autodoc を使ってみる
Ruby Ruby on Rails
Published: 2019-08-25

やったこと

Rails で API ドキュメントを生成する autodoc を使ってみます。

確認環境

$ ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin17]

$ rails --version
Rails 5.2.3

調査

Gemfile

group :development, :test do
  # ... something
  gem 'autodoc'
end
$ bundle install

app/controllers/tasks_controller.rb

class TasksController < ApplicationController
  def show
    point = Struct.new(:x, :y)
    tmp = point.new(111, 222)

    render :json => tmp.to_json
  end
end

rspec の実行 + APIのドキュメント生成

spec/requests/task_apis_spec.rb

require 'rails_helper'

RSpec.describe "TaskApis", type: :request do
  describe "GET /tasks/show" do
    it "works! (now write some real specs)", :autodoc do
      parameters = {id: 100, id2: 222}
      get '/tasks/show', params: parameters
      expect(response).to have_http_status(200)
    end
  end
end
$ AUTODOC=1 bundle exec rspec spec/requests/task_apis_spec.rb

出力結果

doc/task_apis.md に出力されます。

今回は、内容をキャプチャに記載します。

autodoc結果

参考