Class: RailsFlowMap::OpenapiFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_flow_map/formatters/openapi_formatter.rb

Overview

Generates OpenAPI 3.0 specification from Rails application routes

This formatter analyzes Rails routes and generates a complete OpenAPI/Swagger specification that can be used with tools like Swagger UI, Postman, or Insomnia.

Examples:

Basic usage

formatter = OpenapiFormatter.new(graph)
spec = formatter.format
File.write('openapi.yaml', spec)

With custom configuration

formatter = OpenapiFormatter.new(graph, {
  api_version: '2.0.0',
  title: 'My API',
  description: 'Custom API documentation',
  servers: [{ url: 'https://api.myapp.com', description: 'Production' }]
})

Instance Method Summary collapse

Constructor Details

#initialize(graph, options = {}) ⇒ OpenapiFormatter

Creates a new OpenAPI formatter instance

Parameters:

  • graph (FlowGraph)

    The graph containing route information

  • options (Hash) (defaults to: {})

    Configuration options

Options Hash (options):

  • :api_version (String)

    The API version (default: ‘1.0.0’)

  • :title (String)

    The API title

  • :description (String)

    The API description

  • :servers (Array<Hash>)

    Custom server definitions



29
30
31
32
33
34
35
# File 'lib/rails_flow_map/formatters/openapi_formatter.rb', line 29

def initialize(graph, options = {})
  @graph = graph
  @options = options
  @api_version = options[:api_version] || '1.0.0'
  @title = options[:title] || 'Rails API Documentation'
  @description = options[:description] || 'Auto-generated API documentation by RailsFlowMap'
end

Instance Method Details

#format(graph = @graph) ⇒ String

Generates the OpenAPI specification

Parameters:

  • graph (FlowGraph) (defaults to: @graph)

    Optional graph to format (uses instance graph by default)

Returns:

  • (String)

    OpenAPI 3.0 specification in YAML format



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rails_flow_map/formatters/openapi_formatter.rb', line 41

def format(graph = @graph)
  {
    openapi: '3.0.0',
    info: generate_info,
    servers: generate_servers,
    paths: generate_paths,
    components: {
      schemas: generate_schemas,
      securitySchemes: generate_security_schemes
    }
  }.to_yaml
end