class Rswag::RouteParser

Attributes

controller[R]

Public Class Methods

new(controller) click to toggle source
# File lib/rswag/route_parser.rb, line 7
def initialize(controller)
  @controller = controller
end

Public Instance Methods

routes() click to toggle source
# File lib/rswag/route_parser.rb, line 11
def routes
  ::Rails.application.routes.routes.select do |route|
    route.defaults[:controller] == controller
  end.each_with_object({}) do |route, tree|
    path = path_from(route)
    verb = verb_from(route)
    tree[path] ||= { params: params_from(route), actions: {} }
    tree[path][:actions][verb] = { summary: summary_from(route) }
    tree
  end
end

Private Instance Methods

params_from(route) click to toggle source
# File lib/rswag/route_parser.rb, line 56
def params_from(route)
  route.segments - ['format']
end
path_from(route) click to toggle source
# File lib/rswag/route_parser.rb, line 25
def path_from(route)
  route.path.spec.to_s
    .chomp('(.:format)') # Ignore any format suffix
    .gsub(/:([^\/.?]+)/, '{\1}') # Convert :id to {id}
end
summary_from(route) click to toggle source
# File lib/rswag/route_parser.rb, line 40
def summary_from(route)
  verb = route.requirements[:action]
  noun = route.requirements[:controller].split('/').last.singularize

  # Apply a few customizations to make things more readable
  case verb
  when 'index'
    verb = 'list'
    noun = noun.pluralize
  when 'destroy'
    verb = 'delete'
  end

  "#{verb} #{noun}"
end
verb_from(route) click to toggle source
# File lib/rswag/route_parser.rb, line 31
def verb_from(route)
  verb = route.verb
  if verb.is_a? String
    verb.downcase
  else
    verb.source.gsub(/[$^]/, '').downcase
  end
end