Sha256: 4cd16336c61628b61d1caf5209e972bf40f706606e6cf147e98a107ffa59bc73

Contents?: true

Size: 1.19 KB

Versions: 4

Compression:

Stored size: 1.19 KB

Contents

require 'uri'
require 'strscan'

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 self.expand_path(path)
    if path.start_with?('/')
      leading_slash, path = path[0,1], path[1..-1]
    else
      leading_slash = ''
    end

    if path.end_with?('/')
      trailing_slash, path = path[-1,1], path[0..-2]
    else
      trailing_slash = ''
    end

    scanner = StringScanner.new(path)
    stack   = []

    until scanner.eos?
      if (dir = scanner.scan(/^[^\/]+/))
        case dir
        when '..' then stack.pop
        when '.'  then false
        else           stack.push(dir)
        end
      else
        scanner.skip(/\/+/)
      end
    end

    unless stack.empty?
      "#{leading_slash}#{stack.join('/')}#{trailing_slash}"
    else
      '/'
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
spidr-0.7.0 lib/spidr/extensions/uri.rb
spidr-0.6.1 lib/spidr/extensions/uri.rb
spidr-0.6.0 lib/spidr/extensions/uri.rb
spidr-0.5.0 lib/spidr/extensions/uri.rb