Class: SqlChatbot::Services::RouteIntrospector

Inherits:
Object
  • Object
show all
Defined in:
lib/sql_chatbot/services/route_introspector.rb

Constant Summary collapse

INTERNAL_CONTROLLERS =
%w[
  active_storage/ action_mailbox/ action_cable/ rails/
  sql_chatbot/
].freeze

Instance Method Summary collapse

Instance Method Details

#format_route_listObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sql_chatbot/services/route_introspector.rb', line 33

def format_route_list
  routes = introspect
  return "No application routes detected." if routes.empty?

  lines = routes.select { |r| r[:method] == "GET" }.map do |r|
    parent_note = r[:parentPath] ? " (under #{r[:parentPath]})" : ""
    "- #{r[:path]} \u2014 #{r[:label]}#{parent_note}"
  end

  "## Available Application Pages\n#{lines.join("\n")}"
end

#introspectObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sql_chatbot/services/route_introspector.rb', line 11

def introspect
  return [] unless defined?(Rails) && Rails.application

  Rails.application.routes.routes.filter_map do |route|
    next if route.internal
    next if internal_controller?(route)

    path = normalize_path(route)
    next if path.nil? || path.empty?

    {
      path: path,
      method: extract_method(route),
      label: derive_label(route),
      parentPath: derive_parent(path),
    }
  end
rescue => e
  warn "[SqlChatbot] RouteIntrospector: Could not introspect routes: #{e.message}"
  []
end