Class: RouteGuard::Rules::DuplicateRoutes

Inherits:
Base
  • Object
show all
Defined in:
lib/route_guard/rules/duplicate_routes.rb

Instance Attribute Summary

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from Base

#expand_path, #initialize, #path_shadows?, #split_segments, #verb_matches?

Constructor Details

This class inherits a constructor from RouteGuard::Rules::Base

Instance Method Details

#analyze(routes, report) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/route_guard/rules/duplicate_routes.rb', line 9

def analyze(routes, report)
  issues = []

  groups = Hash.new { |h, k| h[k] = [] }
  routes.each do |route|
    route.verb.split("|").each do |v|
      groups[[v, route.path]] << route
    end
  end

  groups.each do |(verb, path), grp_routes|
    next if grp_routes.length <= 1

    primary = grp_routes.first
    duplicates = grp_routes[1..-1]

    duplicates.each do |dup|
      issues << Models::Issue.new(
        rule_name: :duplicate_routes,
        severity: :error,
        message: "Duplicate Route: #{verb} #{path} matches a route defined earlier.",
        route: dup,
        related_routes: [primary]
      )
    end
  end

  issues
end