require "pathname" require "multi_json" require "oj" PATH = Pathname("/Users/karmi/Contracts/Elasticsearch/Projects/Clients/Go/go-elasticsearch/tmp/elasticsearch/rest-api-spec/src/main/resources/rest-api-spec/api/") Dir[PATH.join("*.json")].entries.sort.each do |filepath| next if File.basename(filepath) =~ /^_/ puts "* " + File.basename(filepath) data = {} json = MultiJson.load(File.new(filepath)) api_name = json.keys.first api_spec = json.values.first # Documentation # data["documentation"] = { "url" => api_spec["documentation"] } # TODO: "description" # Extract paths # paths = [] api_spec["url"]["paths"].each do |path| parts = api_spec["url"]["parts"].select { |key, value| path.include? "{#{key}}" } if api_spec["url"]["parts"] payload = { "path" => path, "method" => api_spec["methods"].first } payload["parts"] = parts if parts and not parts.empty? paths << payload end # Extract and recombine deprecated paths # api_spec["url"]["deprecated_paths"].each do |path| payload = { "path" => path["path"], "method" => api_spec["methods"].first, "parts" => api_spec["url"]["parts"].select { |key, value| path["path"].include? "{#{key}}" } } payload["deprecated"] = { "version" => path["version"], "description" => path["description"] } payload["parts"] = Hash[payload["parts"].map do |key, value| if api_spec["url"]["paths"].none? { |p| p.include? "{#{key}}" } value["deprecated"] = true end [key, value] end] paths << payload end if api_spec["url"]["deprecated_paths"] data["url"] = { "paths" => paths } # Params # data["params"] = api_spec["url"]["params"] if api_spec["url"]["params"] # Body # data["body"] = api_spec["body"] if api_spec["body"] output = MultiJson.dump({api_name => data}, indent: 2) puts output, "\n" File.open(filepath, 'w') { |f| f.write output } end