class Openapi::Rswag::RouteParser
Attributes
controller[R]
Public Class Methods
new(controller)
click to toggle source
# File lib/openapi/rswag/route_parser.rb, line 6 def initialize(controller) @controller = controller end
Public Instance Methods
routes()
click to toggle source
# File lib/openapi/rswag/route_parser.rb, line 10 def routes ::Rails.application.routes.routes.select do |route| route.defaults[:controller] == controller end.reduce({}) do |tree, route| 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/openapi/rswag/route_parser.rb, line 55 def params_from(route) route.segments - ['format'] end
path_from(route)
click to toggle source
# File lib/openapi/rswag/route_parser.rb, line 24 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/openapi/rswag/route_parser.rb, line 39 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/openapi/rswag/route_parser.rb, line 30 def verb_from(route) verb = route.verb if verb.kind_of? String verb.downcase else verb.source.gsub(/[$^]/, '').downcase end end