Class: JekyllOpenAPI::Document
- Inherits:
-
Object
- Object
- JekyllOpenAPI::Document
- Defined in:
- lib/jekyll_openapi/document.rb
Overview
Navigable object model over a parsed OpenAPI document.
Absorbs the spec-aware traversal that would otherwise live in templates:
enumerating operations (including x- extension methods), filtering by tag,
and merging path-level with operation-level parameters.
Constant Summary collapse
- HTTP_METHODS =
%w[get put post delete options head patch trace].freeze
- PATH_ITEM_FIELDS =
%w[summary description servers parameters $ref].freeze
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
-
.load_file(path, logger: JekyllOpenAPI.logger) ⇒ Object
Convenience loader for YAML or JSON files.
Instance Method Summary collapse
-
#dereferenced ⇒ Hash
The document with all local $refs resolved (memoized).
- #info ⇒ Object
-
#initialize(data, logger: JekyllOpenAPI.logger) ⇒ Document
constructor
A new instance of Document.
-
#operations(tag: nil) ⇒ Object
Flat list of every operation in the document, in path order.
-
#operations_by_tag ⇒ Hash{String => Array}
Tag name => operations, following the tags declaration order.
- #paths ⇒ Object
- #tags ⇒ Object
Constructor Details
#initialize(data, logger: JekyllOpenAPI.logger) ⇒ Document
Returns a new instance of Document.
20 21 22 23 |
# File 'lib/jekyll_openapi/document.rb', line 20 def initialize(data, logger: JekyllOpenAPI.logger) @data = data @logger = logger end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
17 18 19 |
# File 'lib/jekyll_openapi/document.rb', line 17 def data @data end |
Class Method Details
.load_file(path, logger: JekyllOpenAPI.logger) ⇒ Object
Convenience loader for YAML or JSON files
26 27 28 29 30 31 32 33 |
# File 'lib/jekyll_openapi/document.rb', line 26 def self.load_file(path, logger: JekyllOpenAPI.logger) data = if File.extname(path) == ".json" JSON.parse(File.read(path)) else YAML.safe_load_file(path, aliases: true) end new(data, logger: logger) end |
Instance Method Details
#dereferenced ⇒ Hash
Returns the document with all local $refs resolved (memoized).
36 37 38 |
# File 'lib/jekyll_openapi/document.rb', line 36 def dereferenced @dereferenced ||= Dereferencer.new(@data, logger: @logger).dereference end |
#info ⇒ Object
40 41 42 |
# File 'lib/jekyll_openapi/document.rb', line 40 def info dereferenced["info"] || {} end |
#operations(tag: nil) ⇒ Object
Flat list of every operation in the document, in path order.
x--prefixed extension methods (e.g. webDAV verbs) are included,
with the prefix stripped in "method" and kept in "raw_method".
Each entry is a string-keyed Hash (Liquid-friendly):
"path" [String] e.g. "/scenes/{scene}"
"method" [String] e.g. "get" or "mkcol"
"raw_method" [String] e.g. "get" or "x-mkcol"
"operation" [Hash] the Operation object
"parameters" [Array] path-level and operation-level parameters, merged
"grouped_parameters" [Hash] same, grouped by location (see Parameters.group)
66 67 68 69 70 |
# File 'lib/jekyll_openapi/document.rb', line 66 def operations(tag: nil) @operations ||= build_operations return @operations unless tag @operations.select { |op| Array(op["operation"]["tags"]).include?(tag) } end |
#operations_by_tag ⇒ Hash{String => Array}
Returns tag name => operations, following the tags declaration order.
73 74 75 |
# File 'lib/jekyll_openapi/document.rb', line 73 def operations_by_tag .to_h { |t| [t["name"], operations(tag: t["name"])] } end |
#paths ⇒ Object
48 49 50 |
# File 'lib/jekyll_openapi/document.rb', line 48 def paths dereferenced["paths"] || {} end |
#tags ⇒ Object
44 45 46 |
# File 'lib/jekyll_openapi/document.rb', line 44 def dereferenced["tags"] || [] end |