class PathTree
Loads the Discord HTTP API OpenAPI spec and builds a PathTree trie of all valid endpoints. Reuses the core Node/insert/match logic from Async::Matrix::Api::PathTree.
The Discord spec is a single JSON file (OpenAPI 3.1.0) with the server URL https://discord.com/api/v10, yielding prefix segments ["api", "v10"].
tree = PathTree.load tree.match(%w[api v10 channels 123 messages], "POST")
=> valid: true, operation_id: "create_message", methods: ["post"]
Definitions
def load_json_schema(path)
Parse the Discord OpenAPI JSON file and insert all paths into the trie.
Implementation
def load_json_schema(path)
doc = JSON.parse(File.read(path))
base_path = extract_json_base_path(doc)
paths = doc["paths"]
return unless paths.is_a?(Hash)
paths.each do |path_template, methods_hash|
next unless methods_hash.is_a?(Hash)
full_path = "#{base_path}#{path_template.strip}"
segments = full_path.split("/").reject(&:empty?)
methods_hash.each do |method, operation|
# Skip path-level parameters (Discord spec puts these alongside methods)
next if method == "parameters"
next unless %w[get post put delete patch head].include?(method)
operation_id = operation.is_a?(Hash) ? operation["operationId"] : nil
insert(segments, method, operation_id)
end
end
end