Module: JekyllOpenAPI::JSONPointer

Defined in:
lib/jekyll_openapi/json_pointer.rb

Overview

RFC 6901 JSON Pointer resolution.

Class Method Summary collapse

Class Method Details

.resolve(doc, pointer) ⇒ Object?

Resolves a JSON Pointer against a document.

Parameters:

  • doc (Hash, Array)

    root document

  • pointer (String)

    e.g. "/components/schemas/Scene"

Returns:

  • (Object, nil)

    the referenced value, or nil if not found



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/jekyll_openapi/json_pointer.rb', line 10

def self.resolve(doc, pointer)
  return doc if pointer.nil? || pointer.empty?
  return nil unless pointer.start_with?("/")

  pointer.split("/", -1).drop(1).reduce(doc) do |node, token|
    token = unescape(token)
    case node
    when Hash
      node.fetch(token) { return nil }
    when Array
      return nil unless /\A\d+\z/.match?(token)
      node.fetch(Integer(token)) { return nil }
    else
      return nil
    end
  end
end

.unescape(token) ⇒ Object

"~1" then "~0", in that order (RFC 6901 §4)



29
30
31
# File 'lib/jekyll_openapi/json_pointer.rb', line 29

def self.unescape(token)
  token.gsub("~1", "/").gsub("~0", "~")
end