Sha256: 5a67bc2f7ecf78a13abdb394e7d7463d5f06576afebf67e89c8140b22915b409

Contents?: true

Size: 854 Bytes

Versions: 8

Compression:

Stored size: 854 Bytes

Contents

require 'uri'

module URI
  #
  # Expands a URI decoded path, into a proper absolute path.
  #
  # @param [String] path
  #   The path from a URI.
  #
  # @return [String]
  #   The expanded path.
  #
  # @example
  #   URI.expand_path('./path')
  #   # => "path"
  #
  # @example
  #   URI.expand_path('test/../path')
  #   # => "path"
  #
  # @example
  #   URI.exand_path('/test/path/')
  #   # => "/test/path/"
  #
  # @example
  #   URI.expand_path('/test/../path')
  #   # => "/path"
  #
  def URI.expand_path(path)
    dirs = path.gsub(/[\/]{2,}/,'/').scan(/[^\/]*\/|[^\/]+$/)
    new_dirs = []

    dirs.each do |dir|
      if (dir == '..' || dir == '../')
        unless new_dirs == ['/']
          new_dirs.pop
        end
      elsif (dir != '.' && dir != './')
        new_dirs.push(dir)
      end
    end

    return new_dirs.join
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
spidr-0.2.7 lib/spidr/extensions/uri.rb
spidr-0.2.6 lib/spidr/extensions/uri.rb
spidr-0.2.5 lib/spidr/extensions/uri.rb
spidr-0.2.4 lib/spidr/extensions/uri.rb
spidr-0.2.3 lib/spidr/extensions/uri.rb
spidr-0.2.2 lib/spidr/extensions/uri.rb
spidr-0.2.1 lib/spidr/extensions/uri.rb
spidr-0.2.0 lib/spidr/extensions/uri.rb