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)


32
33
34
35
# File 'lib/openapi3_parser/source/pointer.rb', line 32

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

Instance Attribute Details

#absoluteObject (readonly)

Returns the value of attribute absolute.



28
29
30
# File 'lib/openapi3_parser/source/pointer.rb', line 28

def absolute
  @absolute
end

#segmentsObject (readonly)

Returns the value of attribute segments.



28
29
30
# File 'lib/openapi3_parser/source/pointer.rb', line 28

def segments
  @segments
end

Class Method Details

.from_fragment(fragment) ⇒ Object



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

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

    # replace JSON pointer specific encodings
    part = part.gsub("~1", "/").gsub("~0", "~")
    unescaped = CGI.unescape(part.gsub("%20", "+"))
    unescaped.match?(/\A\d+\z/) ? unescaped.to_i : unescaped
  end
  new(segments.compact, absolute:)
end

.merge_pointers(base_pointer, new_pointer) ⇒ Object



24
25
26
# File 'lib/openapi3_parser/source/pointer.rb', line 24

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

Instance Method Details

#==(other) ⇒ Object



37
38
39
# File 'lib/openapi3_parser/source/pointer.rb', line 37

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

#fragmentObject



41
42
43
44
45
46
47
48
# File 'lib/openapi3_parser/source/pointer.rb', line 41

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

#inspectObject



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

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

#root?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/openapi3_parser/source/pointer.rb', line 58

def root?
  segments.empty?
end

#to_sObject



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

def to_s
  fragment
end