Module: Profiler::MCP::PathExtractor

Defined in:
lib/profiler/mcp/path_extractor.rb

Class Method Summary collapse

Class Method Details

.extract_json(content, path) ⇒ Object

Supports dot-notation JSONPath: $.key, $.key.sub, $.array.key



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/profiler/mcp/path_extractor.rb', line 9

def self.extract_json(content, path)
  data = JSON.parse(content)
  segments = path.sub(/\A\$\.?/, "").split(".").flat_map do |seg|
    seg =~ /\A(.+)\[(\d+)\]\z/ ? [$1, $2.to_i] : [seg]
  end
  result = segments.reduce(data) do |obj, seg|
    obj.is_a?(Array) ? obj[seg.to_i] : obj[seg]
  end
  JSON.generate(result)
rescue => e
  "JSONPath error: #{e.message}"
end

.extract_xml(content, xpath) ⇒ Object



22
23
24
25
26
27
# File 'lib/profiler/mcp/path_extractor.rb', line 22

def self.extract_xml(content, xpath)
  require "nokogiri"
  Nokogiri::XML(content).xpath(xpath).to_s
rescue => e
  "XPath error: #{e.message}"
end