Class: JekyllOpenAPI::Document

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, logger: JekyllOpenAPI.logger) ⇒ Document

Returns a new instance of Document.

Parameters:

  • data (Hash)

    a parsed OpenAPI document (dereferenced or not)



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

#dataObject (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

#dereferencedHash

Returns the document with all local $refs resolved (memoized).

Returns:

  • (Hash)

    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

#infoObject



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)

Parameters:

  • tag (String, nil) (defaults to: nil)

    only operations tagged with this tag



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_tagHash{String => Array}

Returns tag name => operations, following the tags declaration order.

Returns:

  • (Hash{String => Array})

    tag name => operations, following the tags declaration order



73
74
75
# File 'lib/jekyll_openapi/document.rb', line 73

def operations_by_tag
  tags.to_h { |t| [t["name"], operations(tag: t["name"])] }
end

#pathsObject



48
49
50
# File 'lib/jekyll_openapi/document.rb', line 48

def paths
  dereferenced["paths"] || {}
end

#tagsObject



44
45
46
# File 'lib/jekyll_openapi/document.rb', line 44

def tags
  dereferenced["tags"] || []
end