Module: URI

Defined in:
lib/spidr/extensions/uri.rb

Class Method Summary

Class Method Details

+ (String) expand_path(path)

Expands a URI decoded path, into a proper absolute path.

Examples:

  URI.expand_path('./path')
  # => "path"

  URI.expand_path('test/../path')
  # => "path"

  URI.exand_path('/test/path/')
  # => "/test/path/"

  URI.expand_path('/test/../path')
  # => "/path"

Parameters:

  • (String) path — The path from a URI.

Returns:

  • (String) — The expanded path.


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/spidr/extensions/uri.rb', line 29

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