Module: RailVerdict::RailsContext::Resolvers::RouteScanner

Defined in:
lib/rail_verdict/rails_context/resolvers/route_scanner.rb

Class Method Summary collapse

Class Method Details

.scan(repository_root:) ⇒ Object



7
8
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rail_verdict/rails_context/resolvers/route_scanner.rb', line 7

def self.scan(repository_root:)
  path = File.join(repository_root, "config/routes.rb")
  return { declarations: [], unresolved: false } unless File.file?(path)
  return { declarations: [], unresolved: false } if File.size(path) > Limits::ROUTES_MAX_BYTES rescue nil

  text = File.binread(path)
  return { declarations: [], unresolved: false } if text.bytesize > Limits::ROUTES_MAX_BYTES

  text.force_encoding(Encoding::UTF_8)
  return { declarations: [], unresolved: false } unless text.valid_encoding?
  return { declarations: [], unresolved: true } if text.include?("\x00")

  declarations = []
  unresolved = false

  text.each_line do |line|
    stripped = line.strip
    next if stripped.empty? || stripped.start_with?("#")

    if stripped.match?(/\b(draw|mount|concern|constraints)\b/)
      unresolved = true
      next
    end

    if (match = stripped.match(/\bresources\s*:\s*(\w+)/))
      declarations << { "controller" => match[1], "declaration" => stripped, "confidence" => "exact" }
      next
    end
    if (match = stripped.match(/\bresource\s*:\s*(\w+)/))
      declarations << { "controller" => match[1], "declaration" => stripped, "confidence" => "exact" }
      next
    end
    if (match = stripped.match(/\b(?:get|post|put|patch|delete)\s+['\"][^'\"]*['\"]\s*,\s*to:\s*['\"](\w+)#/))
      declarations << { "controller" => match[1], "declaration" => stripped, "confidence" => "exact" }
      next
    end
    if stripped.match?(/\bresources\b/) && stripped.include?("do")
      unresolved = true
    end
  end

  { declarations: declarations.first(Limits::MAX_RELATED), unresolved: unresolved }
rescue StandardError
  { declarations: [], unresolved: false }
end