Sha256: 9a9250af7a4aad2aa0dfdc3c33c80eef6e0fa1d49a06dfd9b112aee3bd40dee5

Contents?: true

Size: 1.52 KB

Versions: 3

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

require "cgi"

require "openapi3_parser/source"
require "openapi3_parser/validators/reference"

module Openapi3Parser
  class Source
    # An object which represents a reference that can be indicated in a OpenAPI
    # file. Given a string reference it can be used to answer key questions
    # that aid in resolving the reference
    #
    # e.g.
    # r = Openapi3Parser::Source::Reference.new("test.yaml#/path/to/item")
    #
    # r.only_fragment?
    # => false
    #
    # r.rsource_uri
    # => "test.yaml"
    class Reference
      # @param [String] reference   reference from an OpenAPI file
      def initialize(reference)
        @given_reference = reference
      end

      def to_s
        given_reference.to_s
      end

      def only_fragment?
        resource_uri.to_s == ""
      end

      # @return [String, nil]
      def fragment
        uri.fragment
      end

      # @return [URI]
      def resource_uri
        uri_without_fragment
      end

      def absolute?
        uri.absolute?
      end

      # @return [::Array] an array of strings of the components in the fragment
      def json_pointer
        @json_pointer ||= (fragment || "").split("/").drop(1).map do |field|
          CGI.unescape(field.gsub("+", "%20"))
        end
      end

      private

      attr_reader :given_reference

      def uri
        @uri = URI.parse(given_reference)
      end

      def uri_without_fragment
        @uri_without_fragment ||= uri.dup.tap { |u| u.fragment = nil }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
openapi3_parser-0.5.1 lib/openapi3_parser/source/reference.rb
openapi3_parser-0.5.0 lib/openapi3_parser/source/reference.rb
openapi3_parser-0.4.0 lib/openapi3_parser/source/reference.rb