Sha256: 6ac73c51c98b95c8d4bd4d9f03cab759fd5ea651697e3f20aa9fa02deea2f829

Contents?: true

Size: 1.78 KB

Versions: 4

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

require "openapi3_parser/source_input/file"
require "openapi3_parser/source_input/url"

module Openapi3Parser
  class SourceInput
    class ResolveNext
      # @param  reference            [Source::Reference]
      # @param  current_source_input [SourceInput]
      # @param  base_url             [String, nil]
      # @param  working_directory    [String, nil]
      # @return [SourceInput]
      def self.call(reference,
                    current_source_input,
                    base_url: nil,
                    working_directory: nil)
        new(reference, current_source_input, base_url, working_directory)
          .source_input
      end

      def initialize(reference,
                     current_source_input,
                     base_url,
                     working_directory)
        @reference = reference
        @current_source_input = current_source_input
        @base_url = base_url
        @working_directory = working_directory
      end

      private_class_method :new

      def source_input
        return current_source_input if reference.only_fragment?
        if reference.absolute?
          SourceInput::Url.new(reference.resource_uri)
        else
          base_url ? url_source_input : file_source_input
        end
      end

      private

      attr_reader :reference, :current_source_input, :base_url,
                  :working_directory

      def url_source_input
        url = URI.join(base_url, reference.resource_uri)
        SourceInput::Url.new(url)
      end

      def file_source_input
        path = reference.resource_uri.path
        return SourceInput::File.new(path) if path[0] == "/"

        expanded_path = ::File.expand_path(path, working_directory)
        SourceInput::File.new(expanded_path)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
openapi3_parser-0.5.1 lib/openapi3_parser/source_input/resolve_next.rb
openapi3_parser-0.5.0 lib/openapi3_parser/source_input/resolve_next.rb
openapi3_parser-0.4.0 lib/openapi3_parser/source_input/resolve_next.rb
openapi3_parser-0.3.0 lib/openapi3_parser/source_input/resolve_next.rb