Class: Openapi3Parser::Source::Pointer

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

Overview

A class to decorate the array of fields that make up a pointer and provide common means to convert it into different representations.

Defined Under Namespace

Classes: MergePointers

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(segments, absolute: true) ⇒ Pointer

Returns a new instance of Pointer.

Parameters:

  • segments (::Array)
  • absolute (Boolean) (defaults to: true)

30
31
32
33
# File 'lib/openapi3_parser/source/pointer.rb', line 30

def initialize(segments, absolute: true)
  @segments = segments.freeze
  @absolute = absolute
end

Instance Attribute Details

#absoluteObject (readonly)

Returns the value of attribute absolute


26
27
28
# File 'lib/openapi3_parser/source/pointer.rb', line 26

def absolute
  @absolute
end

#segmentsObject (readonly)

Returns the value of attribute segments


26
27
28
# File 'lib/openapi3_parser/source/pointer.rb', line 26

def segments
  @segments
end

Class Method Details

.from_fragment(fragment) ⇒ Object


10
11
12
13
14
15
16
17
18
19
20
# File 'lib/openapi3_parser/source/pointer.rb', line 10

def self.from_fragment(fragment)
  fragment = fragment[1..-1] if fragment.start_with?("#")
  absolute = fragment[0] == "/"
  segments = fragment.split("/").map do |part|
    next if part == ""

    unescaped = CGI.unescape(part.gsub("%20", "+"))
    unescaped.match?(/\A\d+\z/) ? unescaped.to_i : unescaped
  end
  new(segments.compact, absolute: absolute)
end

.merge_pointers(base_pointer, new_pointer) ⇒ Object


22
23
24
# File 'lib/openapi3_parser/source/pointer.rb', line 22

def self.merge_pointers(base_pointer, new_pointer)
  MergePointers.call(base_pointer, new_pointer)
end

Instance Method Details

#==(other) ⇒ Object


35
36
37
# File 'lib/openapi3_parser/source/pointer.rb', line 35

def ==(other)
  segments == other.segments
end

#fragmentObject


39
40
41
42
43
# File 'lib/openapi3_parser/source/pointer.rb', line 39

def fragment
  fragment = segments.map { |s| CGI.escape(s.to_s).gsub("+", "%20") }
                     .join("/")
  "##{absolute ? fragment.prepend('/') : fragment}"
end

#inspectObject


49
50
51
# File 'lib/openapi3_parser/source/pointer.rb', line 49

def inspect
  %{#{self.class.name}(segments: #{segments}, fragment: "#{fragment}")}
end

#root?Boolean

Returns:

  • (Boolean)

53
54
55
# File 'lib/openapi3_parser/source/pointer.rb', line 53

def root?
  segments.empty?
end

#to_sObject


45
46
47
# File 'lib/openapi3_parser/source/pointer.rb', line 45

def to_s
  fragment
end