Sha256: 9dc60721dbc898a213e2f2c02ae5262722d903e929d73385283023a46f818d5f

Contents?: true

Size: 944 Bytes

Versions: 8

Compression:

Stored size: 944 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.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
end

Version data entries

8 entries across 8 versions & 3 rubygems

Version Path
spidr_epg_gem-0.0.1 lib/spidr_epg/extensions/uri.rb
spidr_epg_gem-0.0.0 lib/spidr_epg/extensions/uri.rb
spidr_epg-1.0.0 lib/spidr/extensions/uri.rb
spidr-0.4.1 lib/spidr/extensions/uri.rb
spidr-0.4.0 lib/spidr/extensions/uri.rb
spidr-0.3.2 lib/spidr/extensions/uri.rb
spidr-0.3.1 lib/spidr/extensions/uri.rb
spidr-0.3.0 lib/spidr/extensions/uri.rb