Class: URI::Component::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/uri/component/path.rb

Overview

Handle a path component in an URI as an object

Constant Summary collapse

RE_UNSAFE =

:stopdoc: Same as URI::UNSAFE, plus ‘;’ (separator for path nodes) and ‘?’ (separator for path and query)

/
  [^#{URI::REGEXP::PATTERN::UNRESERVED}#{URI::REGEXP::PATTERN::RESERVED}]|
  [\/?]
/x
RE_COMPONENT =
/^(?:#{URI::REGEXP::PATTERN::ABS_PATH})?$/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path_str = '') ⇒ Path

Returns a new instance of Path.



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

def initialize(path_str='')
  unless path_str =~ RE_COMPONENT
    raise InvalidURIError, "bad Path component for URI: #{path_str}"
  end

  if path_str
    @nodes = path_str.split('/', -1).map do |node|
      URI.unescape(node)
    end
    @nodes.shift
  else
    @nodes = []
  end
end

Class Method Details

.mixin(klass) ⇒ Object

:startdoc:



24
25
26
27
# File 'lib/uri/component/path.rb', line 24

def self.mixin(klass) #:nodoc:
  PathMixin.__send__(:append_features, klass)
  PathMixin.__send__(:included, klass)
end

Instance Method Details

#escape(v) ⇒ Object



44
45
46
# File 'lib/uri/component/path.rb', line 44

def escape(v)
  return URI.escape(v, RE_UNSAFE)
end

#nodesObject



56
57
58
# File 'lib/uri/component/path.rb', line 56

def nodes
  return @nodes
end

#nodes=(v) ⇒ Object



60
61
62
# File 'lib/uri/component/path.rb', line 60

def nodes=(v)
  @nodes = v
end

#normalize!Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/uri/component/path.rb', line 64

def normalize!
  nodes = []
  @nodes.each do |node|
    case node
    when ''
      next
    when '.'
      next
    when '..'
      nodes.pop
      next
    end
    nodes << node
  end

  @nodes = nodes
end

#to_uriObject Also known as: to_s



48
49
50
51
52
53
# File 'lib/uri/component/path.rb', line 48

def to_uri
  return '' if @nodes.empty?
  return '/' + @nodes.map do |node|
    self.escape(node)
  end.join('/')
end