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)


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

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

Instance Attribute Details

#absoluteObject (readonly)

Returns the value of attribute absolute.



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

def absolute
  @absolute
end

#segmentsObject (readonly)

Returns the value of attribute segments.



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

def segments
  @segments
end

Class Method Details

.from_fragment(fragment) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# 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)
end

.merge_pointers(base_pointer, new_pointer) ⇒ Object



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

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

Instance Method Details

#==(other) ⇒ Object



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

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

#fragmentObject



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

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

#inspectObject



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

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

#to_sObject



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

def to_s
  fragment
end