Class: OpenapiSlicer
- Inherits:
-
Object
- Object
- OpenapiSlicer
- Defined in:
- lib/openapi_slicer.rb
Overview
OpenapiSlicer is a tool that slices an OpenAPI spec based on a regular expression, and filters out necessary paths and components, ensuring that all dependent components (schemas, parameters, etc.) are included.
Constant Summary collapse
- VERSION =
The current version of the OpenapiSlicer
"0.2.0"
Instance Attribute Summary collapse
-
#spec ⇒ Hash
The OpenAPI specification loaded from the file.
Instance Method Summary collapse
-
#export(regex, target_file) ⇒ Object
Exports the filtered OpenAPI spec to a file.
-
#filter(regex) ⇒ Hash
Filters the OpenAPI spec paths based on a regular expression and extracts the necessary components and tags.
-
#initialize(file_path:) ⇒ OpenapiSlicer
constructor
Initializes the OpenapiSlicer.
Constructor Details
#initialize(file_path:) ⇒ OpenapiSlicer
Initializes the OpenapiSlicer.
21 22 23 24 25 26 27 28 |
# File 'lib/openapi_slicer.rb', line 21 def initialize(file_path:) raise "Invalid file type. Only JSON and YAML are supported." unless file_path.match?(/\.(json|ya?ml)$/) @file_path = file_path @spec = load_spec(file_path) @components = {} @tags = Set.new end |
Instance Attribute Details
#spec ⇒ Hash
15 16 17 |
# File 'lib/openapi_slicer.rb', line 15 def spec @spec end |
Instance Method Details
#export(regex, target_file) ⇒ Object
Exports the filtered OpenAPI spec to a file.
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/openapi_slicer.rb', line 45 def export(regex, target_file) result = filter(regex) File.open(target_file, "w") do |f| if target_file.match?(/\.json$/) f.write(result.to_json) else f.write(result.to_yaml) end end end |
#filter(regex) ⇒ Hash
Filters the OpenAPI spec paths based on a regular expression and extracts the necessary components and tags.
35 36 37 38 39 |
# File 'lib/openapi_slicer.rb', line 35 def filter(regex) paths = @spec["paths"].select { |path, _| path.match?(regex) } dependencies = extract_dependencies(paths) slice_spec(paths, dependencies) end |