class R2OAS::Routing::Parser

Attributes

_engines[RW]
_routes[RW]

Public Class Methods

new(routes) click to toggle source

routes should be Rails.application.routes.routes

# File lib/r2-oas/routing/parser.rb, line 10
def initialize(routes)
  @_routes = routes
  @_engines = {}
end

Public Instance Methods

routes_data() click to toggle source
# File lib/r2-oas/routing/parser.rb, line 15
def routes_data
  data = []
  normalized_routes do |route_els|
    data.push *route_els
  end
  data
end
schemas_data() click to toggle source
# File lib/r2-oas/routing/parser.rb, line 34
def schemas_data
  data = []
  normalized_routes do |route_els|
    route_els.each do |route_el|
      schema_name = route_el[:data][:schema_name]
      data.push schema_name unless data.include?(schema_name)
    end
  end
  data
end
tags_data() click to toggle source
# File lib/r2-oas/routing/parser.rb, line 23
def tags_data
  data = []
  normalized_routes do |route_els|
    route_els.each do |route_el|
      tag_name = route_el[:data][:tag_name]
      data.push tag_name unless data.include?(tag_name)
    end
  end
  data
end

Private Instance Methods

collect_engine_routes(route) click to toggle source

copy from: github.com/rails/rails/blob/v4.2.1/actionpack/lib/action_dispatch/routing/inspector.rb#L114-L140

# File lib/r2-oas/routing/parser.rb, line 83
def collect_engine_routes(route)
  name = route.endpoint
  return unless route.engine?
  return if _engines[name]

  routes = route.rack_app.routes
  _engines[name] = collect_routes(routes.routes) if routes.is_a?(ActionDispatch::Routing::RouteSet)
end
collect_routes(routes) click to toggle source

copy from: github.com/rails/rails/blob/v4.2.1/actionpack/lib/action_dispatch/routing/inspector.rb#L114-L140 github.com/rails/rails/blob/v5.2.3/actionpack/lib/action_dispatch/routing/inspector.rb

# File lib/r2-oas/routing/parser.rb, line 59
def collect_routes(routes)
  result = routes.collect do |route|
    ActionDispatch::Routing::RouteWrapper.new(route)
  end.reject(&:internal?).collect do |route|
    collect_engine_routes(route)

    # delete json_regexp after copy
    { route: route,
      name: route.name,
      verb: route.verb,
      path: route.path,
      reqs: route.reqs }
  end

  # Push Rails Engine Routes Data
  _engines.each do |_engine_name, engine_route_data|
    result.push(*engine_route_data)
  end

  result
end
normalized_routes(&block) click to toggle source
# File lib/r2-oas/routing/parser.rb, line 49
def normalized_routes(&block)
  collect_routes(_routes).each_with_object([]) do |route_data, _arr|
    routes_els = Adjustor.new(route_data).routes_els
    block.call(routes_els) if block_given?
  end
end