Module: LlamaBotRails::RouteHelper

Defined in:
lib/llama_bot_rails/route_helper.rb

Class Method Summary collapse

Class Method Details

.extract_yard_description(comment_text) ⇒ Object

Extracts the description from YARD comments



4
5
6
7
8
# File 'lib/llama_bot_rails/route_helper.rb', line 4

def self.extract_yard_description(comment_text)
  comment_text.lines.map { |l| l.sub(/^# ?/, '') }
              .take_while { |l| !l.strip.start_with?('@') }
              .join(' ').strip
end

.extract_yard_tag(comment_text, tag) ⇒ Object

Extracts a specific YARD tag from comments



11
12
13
14
15
# File 'lib/llama_bot_rails/route_helper.rb', line 11

def self.extract_yard_tag(comment_text, tag)
  if match = comment_text.match(/@#{tag} (.+)/)
    match[1].strip
  end
end

.formatted_routes_xml(allowed_routes) ⇒ Object

Main method: returns XML string of formatted routes for allowed_routes



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/llama_bot_rails/route_helper.rb', line 18

def self.formatted_routes_xml(allowed_routes)
  xml_routes = ""
  allowed_routes.each do |route_str|
    controller, action = route_str.split('#')
    matching_routes = Rails.application.routes.routes.select do |r|
      r.defaults[:controller] == controller && r.defaults[:action] == action
    end

    matching_routes.each do |r|
      verb = r.verb.to_s.gsub(/[$^]/, '') # Handles both Regexp and String
      path = r.path.spec.to_s
      path_params = path.scan(/:\w+/).map { |p| p[1..-1] } # e.g. ["id"]

      # Extract controller class and strong parameters
      controller_class = "#{controller.camelize}Controller".safe_constantize
      strong_params = []
       = {}

      if controller_class
        # Extract YARD documentation for the action
        begin
          method_obj = controller_class.instance_method(action.to_sym)
          source_location = method_obj.source_location
          if source_location
            file_path, line_number = source_location
            file_lines = File.readlines(file_path)
            # Look for YARD comments above the method
            comment_lines = []
            current_line = line_number - 2 # Start above the method definition
            while current_line >= 0 && file_lines[current_line].strip.start_with?('#')
              comment_lines.unshift(file_lines[current_line].strip)
              current_line -= 1
            end
            # Parse YARD tags
            comment_text = comment_lines.join("\n")
            [:description] = extract_yard_description(comment_text)
            [:tool_description] = extract_yard_tag(comment_text, 'tool_description')
            [:example] = extract_yard_tag(comment_text, 'example')
            [:params] = extract_yard_tag(comment_text, 'params')
          end
        rescue => e
          # Silently continue if YARD parsing fails
        end
        # Look for the strong parameter method (e.g., page_params, user_params, etc.)
        param_method = "#{controller.singularize}_params"
        if controller_class.private_method_defined?(param_method.to_sym)
          source_location = controller_class.instance_method(param_method.to_sym).source_location
          if source_location
            file_path, line_number = source_location
            file_lines = File.readlines(file_path)
            method_lines = []
            current_line = line_number - 1
            while current_line < file_lines.length
              line = file_lines[current_line].strip
              method_lines << line
              break if line.include?('end') && !line.include?('permit')
              current_line += 1
            end
            method_source = method_lines.join(' ')
            if match = method_source.match(/\.permit\((.*?)\)/)
              permit_content = match[1]
              strong_params = permit_content.scan(/:(\w+)/).flatten
            end
          end
        end
        # Also check for any additional params the action might accept
        additional_params = []
        case action
        when 'update', 'create'
          if controller == 'pages' && action == 'update'
            additional_params << 'message'
          end
        end
        all_params = (path_params + strong_params + additional_params).uniq
      else
        all_params = path_params
      end

      xml = <<~XML
        <route>
          <name>#{route_str}</name>
          <verb>#{verb}</verb>
          <path>#{path}</path>
          <path_params>#{path_params.join(', ')}</path_params>
          <accepted_params>#{all_params.join(', ')}</accepted_params>
          <strong_params>#{strong_params.join(', ')}</strong_params>
          <description>#{[:description]}</description>
          <tool_description>#{[:tool_description]}</tool_description>
          <example>#{[:example]}</example>
          <params>#{[:params]}</params>
        </route>
      XML

      xml_routes += xml
    end
  end
  xml_routes
end