Module: URI

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

Class Method Summary collapse

Class Method Details

.expand_path(path) ⇒ String

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:

  • path (String)

    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
45
46
47
48
49
50
51
# File 'lib/spidr/extensions/uri.rb', line 29

def URI.expand_path(path)
  dirs = path.split(/\/+/)

  # append any tailing '/' chars, lost due to String#split
  dirs << '' if path[-1,1] == '/'

  new_dirs = []

  dirs.each do |dir|
    if dir == '..'
      new_dirs.pop
    elsif dir != '.'
      new_dirs.push(dir)
    end
  end

  full_path = new_dirs.join('/')

  # default empty paths to '/'
  full_path = '/' if full_path.empty?

  return full_path
end