Class: OasParser::Pointer

Inherits:
Object
  • Object
show all
Defined in:
lib/oas_parser/pointer.rb

Instance Method Summary collapse

Constructor Details

#initialize(raw_pointer) ⇒ Pointer

Returns a new instance of Pointer.



3
4
5
# File 'lib/oas_parser/pointer.rb', line 3

def initialize(raw_pointer)
  @raw_pointer = raw_pointer
end

Instance Method Details

#circular_reference?(path) ⇒ Boolean

Detect circular reference by checking whether the ref exists in current path.

Example: components:

schemas:
  Pet:
    type: object
    properties:
      name:
        type: string
      children:
        type: array
        items: # <--- parsing here
          - $ref: '#/components/schemas/Pet'

path: “/components/schemas/Pet/properties/children/items” raw_pointer: “#/components/schemas/Pet”

It’d return true when we’re parsing the pet children items where the ref points back to itself.

Returns:

  • (Boolean)


34
35
36
# File 'lib/oas_parser/pointer.rb', line 34

def circular_reference?(path)
  path.include?("#{escaped_pointer}/")
end

#escaped_pointerObject



38
39
40
41
42
43
44
# File 'lib/oas_parser/pointer.rb', line 38

def escaped_pointer
  if @raw_pointer.start_with?("#")
    Addressable::URI.unencode(@raw_pointer[1..-1])
  else
    @raw_pointer
  end
end

#resolve(document) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/oas_parser/pointer.rb', line 7

def resolve(document)
  return document if escaped_pointer == ""

  tokens.reduce(document) do |nested_doc, token|
    nested_doc.fetch(token)
  end
end