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_NODE_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

#[](*index) ⇒ Object



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

def [](*index)
  return @nodes[*index]
end

#escape_node(v) ⇒ Object



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

def escape_node(v)
  return URI.escape(v, RE_NODE_UNSAFE)
end

#nodesObject



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

def nodes
	return @nodes
end

#nodes=(v) ⇒ Object



64
65
66
# File 'lib/uri/component/path.rb', line 64

def nodes=(v)
	@nodes = v
end

#normalize!Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/uri/component/path.rb', line 68

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(node)
	end.join('/')
end