> For the complete documentation index, see [llms.txt](https://tonystrawberry.gitbook.io/rails-openapi-generator/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tonystrawberry.gitbook.io/rails-openapi-generator/ja/gaido/programmatic-use.md).

# プログラム的利用

ジェネレーターはrakeやCLI以外での利用のためにRuby APIを公開しています。テスト、ツール統合、動的なspec配信などに便利です。

## 生成してディスクに書き出す

```ruby
require "rails_openapi_generator"

config = RailsOpenapiGenerator::Configuration.new
config.output_path = "doc/openapi.json"
config.title       = "My API"
config.api_version = "1.0.0"

report = RailsOpenapiGenerator::Generator.new(config).generate
```

`generate`はファイルを`config.output_path`に書き出し、`GenerationReport`を返します。

## ドキュメントをメモリ上で取得

```ruby
document = RailsOpenapiGenerator::Generator.new(config).document
# => Hash — シリアライズされていない完全なOpenAPI 3.1ドキュメント
```

書き出す前にドキュメントを後処理する場合に使います。

```ruby
doc = RailsOpenapiGenerator::Generator.new(config).document

# セキュリティスキームを追加
doc["components"] ||= {}
doc["components"]["securitySchemes"] = {
  "bearerAuth" => {
    "type" => "http",
    "scheme" => "bearer",
    "bearerFormat" => "JWT"
  }
}

# グローバルに適用
doc["security"] = [{ "bearerAuth" => [] }]

File.write("doc/openapi.json", JSON.pretty_generate(doc))
```

## 生成レポートの読み取り

```ruby
report = RailsOpenapiGenerator::Generator.new(config).generate

puts "処理済み: #{report.processed_count}"
puts "スキップ: #{report.skipped_count}"

report.skipped.each do |item|
  puts "  - #{item.verb} #{item.path}: #{item.reason}"
end

report.warnings.each do |warning|
  puts "警告: #{warning}"
end
```

## デフォルト設定を使う

`config/initializers/rails_openapi_generator.rb`がすでにある場合は、設定済みのインスタンスを直接使えます。

```ruby
generator = RailsOpenapiGenerator::Generator.new(RailsOpenapiGenerator.configuration)
generator.generate
```

## コントローラからspecを配信する

```ruby
class Api::DocsController < ApplicationController
  def show
    config = RailsOpenapiGenerator.configuration
    document = RailsOpenapiGenerator::Generator.new(config).document
    render json: document
  end
end
```

注意: 大規模アプリでリクエストごとにspecを生成すると遅くなります。結果をキャッシュしましょう。

```ruby
def show
  spec = Rails.cache.fetch("openapi_spec", expires_in: 1.hour) do
    RailsOpenapiGenerator::Generator.new(RailsOpenapiGenerator.configuration).document
  end
  render json: spec
end
```

## RSpecとの統合

変更後もspecが有効なままかテストできます。

```ruby
# spec/openapi_spec.rb
require "rails_helper"
require "json_schemer"

RSpec.describe "OpenAPI spec" do
  let(:document) do
    config = RailsOpenapiGenerator::Configuration.new
    config.output_path = Rails.root.join("doc/openapi.json").to_s
    RailsOpenapiGenerator::Generator.new(config).document
  end

  it "有効なOpenAPI 3.1であること" do
    meta_schema = JSONSchemer.openapi31
    errors = meta_schema.validate(document).to_a
    expect(errors).to be_empty, errors.map { |e| e["error"] }.join("\n")
  end

  it "生成に警告がないこと" do
    config = RailsOpenapiGenerator::Configuration.new
    report = RailsOpenapiGenerator::Generator.new(config).generate
    expect(report.warnings).to be_empty
  end
end
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://tonystrawberry.gitbook.io/rails-openapi-generator/ja/gaido/programmatic-use.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
