# frozen_string_literal: true module Chusaku module Routes # Extract information about the Rails project's routes. # # Example output: # # { # 'users' => { # 'edit' => [ # { verb: ['GET'], path: '/users/:id', name: ['edit_user'] }], # 'update' => [ # { verb: ['PUT', 'PATCH'], # path: '/users', # name: ['edit_user', 'edit_user2'] }] # }, # 'empanadas' => { # 'create' => [ # { verb: 'POST', path: '/empanadas', name: nil }] # } # } # # @return {Hash} Routes hash def self.call routes = {} Rails.application.routes.routes.each do |route| defaults = route.defaults controller = defaults[:controller] action = defaults[:action] routes[controller] ||= {} routes[controller][action] ||= [] routes[controller][action].push(format_route(route)) end backfill_routes(routes) end private # Extract information of a given route. # # @param {ActionDispatch::Journey::Route} route - Route given by Rails # @return {Hash} Formatted hash for given route def self.format_route(route) { verb: route.verb, path: route.path.spec.to_s.gsub('(.:format)', ''), name: route.name } end # Given a routes hash, backfill entries that aren't already filled by # `Rails.application.routes`. # # @param {Hash} routes - Routes hash generated by this class # @return {Hash} Backfilled routes def self.backfill_routes(routes) paths = {} routes.each do |_controller, actions| actions.each do |action, data| data.each do |datum| paths[datum[:path]] ||= datum[:name] datum[:name] ||= paths[datum[:path]] end end end routes end end end