class OasRails::Extractors::RouteExtractor
def clean_route(route)
def clean_route(route) route.gsub('(.:format)', '').gsub(/:\w+/) { |match| "{#{match[1..]}}" } end
def clear_cache
This method clear the class instance variable @host_routes
Clear Class Instance Variable @host_routes
def clear_cache @host_routes = nil end
def extract_host_routes
def extract_host_routes valid_routes.map { |r| OasRoute.new_from_rails_route(rails_route: r) } end
def host_paths
def host_paths @host_paths ||= host_routes.map(&:path).uniq.sort end
def host_routes
def host_routes @host_routes ||= extract_host_routes end
def host_routes_by_path(path)
def host_routes_by_path(path) @host_routes ||= extract_host_routes @host_routes.select { |r| r.path == path } end
def ignore_custom_actions(route)
Support ignoring "controller#action"
Support controller name only to ignore all controller actions.
Sanitize api_path by removing the "/" if it starts with that, and adding "/" if it ends without that.
Ignore user-specified paths in initializer configuration.
def ignore_custom_actions(route) api_path = "#{OasRails.config.api_path.sub(%r{\A/}, '')}/".sub(%r{/+$}, '/') ignored_actions = OasRails.config.ignored_actions.flat_map do |custom_route| if custom_route.start_with?(api_path) [custom_route] else ["#{api_path}#{custom_route}", custom_route] end end controller_action = "#{route.defaults[:controller]}##{route.defaults[:action]}" controller_only = route.defaults[:controller] ignored_actions.include?(controller_action) || ignored_actions.include?(controller_only) end
def valid_api_route?(route)
def valid_api_route?(route) return false unless valid_route_implementation?(route) return false if RAILS_DEFAULT_CONTROLLERS.any? { |default| route.defaults[:controller].start_with?(default) } return false if RAILS_DEFAULT_PATHS.any? { |path| route.path.spec.to_s.include?(path) } return false unless route.path.spec.to_s.start_with?(OasRails.config.api_path) return false if ignore_custom_actions(route) true end
def valid_route_implementation?(route)
-
(Boolean)
- true if both the controller and action exist, false otherwise.
Parameters:
-
route
(ActionDispatch::Journey::Route
) -- The route to check.
def valid_route_implementation?(route) controller_name = route.defaults[:controller]&.camelize action_name = route.defaults[:action] return false if controller_name.blank? || action_name.blank? controller_class = "#{controller_name}Controller".safe_constantize if controller_class.nil? false else controller_class.instance_methods.include?(action_name.to_sym) end end
def valid_routes
def valid_routes Rails.application.routes.routes.select do |route| valid_api_route?(route) end end