class Openapi3Parser::SourceInput::File

An input of a file on the file system

@attr_reader [String] path The absolute path to this file @attr_reader [String] working_directory The abolsute path of the

working directory to use when
opening relative references to
this file

Attributes

path[R]
working_directory[R]

Public Class Methods

new(path, working_directory: nil) click to toggle source

@param [String] path The path to the file to open

as this source

@param [String, nil] working_directory The path to the

working directory to use, will
be calculated from path if not
provided
Calls superclass method Openapi3Parser::SourceInput::new
# File lib/openapi3_parser/source_input/file.rb, line 23
def initialize(path, working_directory: nil)
  @path = ::File.absolute_path(path)
  working_directory ||= resolve_working_directory
  @working_directory = ::File.absolute_path(working_directory)
  super()
end

Public Instance Methods

==(other) click to toggle source

@see SourceInput#other @param [SourceInput] other @return [Boolean]

# File lib/openapi3_parser/source_input/file.rb, line 40
def ==(other)
  return false unless other.instance_of?(self.class)

  path == other.path &&
    working_directory == other.working_directory
end
inspect() click to toggle source

return [String]

# File lib/openapi3_parser/source_input/file.rb, line 48
def inspect
  %{#{self.class.name}(path: #{path}, working_directory: } +
    %{#{working_directory})}
end
relative_to(source_input) click to toggle source

Attempt to return a shorter relative path to the other source input so we can produce succinct output

@return [String]

# File lib/openapi3_parser/source_input/file.rb, line 62
def relative_to(source_input)
  other_path = if source_input.respond_to?(:path)
                 ::File.dirname(source_input.path)
               elsif source_input.respond_to?(:working_directory)
                 source_input.working_directory
               end

  return path unless other_path

  other_path ? relative_path(other_path, path) : path
end
resolve_next(reference) click to toggle source

@see SourceInput#resolve_next @param [Source::Reference] reference @return [SourceInput]

# File lib/openapi3_parser/source_input/file.rb, line 33
def resolve_next(reference)
  ResolveNext.call(reference, self, working_directory: working_directory)
end
to_s() click to toggle source

@return [String]

# File lib/openapi3_parser/source_input/file.rb, line 54
def to_s
  path
end

Private Instance Methods

parse_contents() click to toggle source
# File lib/openapi3_parser/source_input/file.rb, line 80
def parse_contents
  begin
    contents = ::File.read(path)
  rescue ::StandardError => e
    @access_error = Error::InaccessibleInput.new(e.message)
    return
  end

  filename = ::File.basename(path)
  StringParser.call(contents, filename)
end
relative_path(from, to) click to toggle source
# File lib/openapi3_parser/source_input/file.rb, line 92
def relative_path(from, to)
  from_path = Pathname.new(from)
  to_path = Pathname.new(to)
  relative = to_path.relative_path_from(from_path).to_s

  relative.size > to.size ? to : relative
end
resolve_working_directory() click to toggle source
# File lib/openapi3_parser/source_input/file.rb, line 76
def resolve_working_directory
  ::File.dirname(path)
end