Module: OpenApi::Router
- Defined in:
- lib/open_api/router.rb
Class Method Summary collapse
- .find_path_httpverb_by(route_base, action) ⇒ Object
- .get_actions_by_route_base(route_base) ⇒ Object
- .routes ⇒ Object
- .routes_list ⇒ Object
Class Method Details
.find_path_httpverb_by(route_base, action) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/open_api/router.rb', line 46 def find_path_httpverb_by(route_base, action) routes_list[route_base]&.map do |action_info| if action_info[:action_path].split('#').last == action.to_s return [ action_info[:path], action_info[:http_verb].split('|').first ] end end ; nil end |
.get_actions_by_route_base(route_base) ⇒ Object
42 43 44 |
# File 'lib/open_api/router.rb', line 42 def get_actions_by_route_base(route_base) routes_list[route_base]&.map { |action_info| action_info[:action_path].split('#').last } end |
.routes ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/open_api/router.rb', line 7 def routes @routes ||= if (file = Config.rails_routes_file) File.read(file) else # :nocov: # ref https://github.com/rails/rails/blob/master/railties/lib/rails/tasks/routes.rake require './config/routes' all_routes = Rails.application.routes.routes require 'action_dispatch/routing/inspector' inspector = ActionDispatch::Routing::RoutesInspector.new(all_routes) if Rails::VERSION::MAJOR < 6 inspector.format(ActionDispatch::Routing::ConsoleFormatter.new, nil) else inspector.format(ActionDispatch::Routing::ConsoleFormatter::Sheet.new) end # :nocov: end end |
.routes_list ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/open_api/router.rb', line 27 def routes_list @routes_list ||= routes.split("\n").drop(1).map do |line| next unless line['#'] infos = line.match(/[A-Z|].*/).to_s.split(' ') # => [GET, /api/v1/examples/:id, api/v1/examples#index] { http_verb: infos[0].downcase, # => "get" / "get|post" path: infos[1][0..-11].split('/').map do |item| item[':'] ? "{#{item[1..-1]}}" : item end.join('/'), # => "/api/v1/examples/{id}" action_path: infos[2] # => "api/v1/examples#index" } rescue next end.compact.group_by { |api| api[:action_path].split('#').first } # => { "api/v1/examples" => [..] }, group by paths end |