Sha256: 4406d076f350b51484ad29e62466ea5c52ab456c0037200c90e7ce1749eca374

Contents?: true

Size: 834 Bytes

Versions: 27

Compression:

Stored size: 834 Bytes

Contents

module OasParser
  class Pointer
    def initialize(raw_pointer)
      @raw_pointer = raw_pointer
    end

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

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

    private

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

    def parse_token(token)
      if token =~ /\A\d+\z/
        token.to_i
      else
        token.gsub("~0", "~").gsub("~1", "/")
      end
    end

    def tokens
      tokens = escaped_pointer[1..-1].split("/")

      if @raw_pointer.end_with?("/")
        tokens << ""
      end

      tokens.map do |token|
        parse_token(token)
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
oas_parser-0.18.2 lib/oas_parser/pointer.rb
oas_parser-0.18.1 lib/oas_parser/pointer.rb
oas_parser-0.18.0 lib/oas_parser/pointer.rb
oas_parser-0.17.0 lib/oas_parser/pointer.rb
oas_parser-0.16.0 lib/oas_parser/pointer.rb
oas_parser-0.15.2 lib/oas_parser/pointer.rb
oas_parser-0.15.1 lib/oas_parser/pointer.rb
oas_parser-0.15.0 lib/oas_parser/pointer.rb
oas_parser-0.14.0 lib/oas_parser/pointer.rb
oas_parser-0.13.1 lib/oas_parser/pointer.rb
oas_parser-0.12.0 lib/oas_parser/pointer.rb
oas_parser-0.11.2 lib/oas_parser/pointer.rb
oas_parser-0.11.1 lib/oas_parser/pointer.rb
oas_parser-0.11.0 lib/oas_parser/pointer.rb
oas_parser-0.10.0 lib/oas_parser/pointer.rb
oas_parser-0.9.0 lib/oas_parser/pointer.rb
oas_parser-0.8.1 lib/oas_parser/pointer.rb
oas_parser-0.8.0 lib/oas_parser/pointer.rb
oas_parser-0.7.0 lib/oas_parser/pointer.rb
oas_parser-0.6.0 lib/oas_parser/pointer.rb