Class: URI::Component::UserInfo

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

Overview

Handle an userinfo component in an URI as an object

Constant Summary collapse

RE_ELEMENT_UNSAFE =

:stopdoc:

/[^#{URI::REGEXP::PATTERN::UNRESERVED}]/
RE_ELEMENT =

Same as URI::USERINFO, except ‘;’ and ‘:’

/(?:
  [#{URI::REGEXP::PATTERN::UNRESERVED}&=+$,]|
  #{URI::REGEXP::PATTERN::ESCAPED})*
/x
RE_COMPONENT =
/^(?:(#{RE_ELEMENT});)?(#{RE_ELEMENT})(?::(#{RE_ELEMENT}))?$/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(info_str = nil) ⇒ UserInfo

Returns a new instance of UserInfo.



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/uri/component/userinfo.rb', line 33

def initialize(info_str=nil)
	if info_str
	  unless m = info_str.match(RE_COMPONENT)
	    raise InvalidURIError, "bad UserInfo component for URI: #{info_str}"
	  end
	  @domain = m[1] ? URI.unescape(m[1]) : nil
	  @user = URI.unescape(m[2])
	  @password = m[3] ? URI.unescape(m[3]) : nil
	else
	  @domain = @user = @password = nil
	end
end

Class Method Details

.mixin(klass) ⇒ Object

:startdoc:



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

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

Instance Method Details

#domainObject



46
47
48
# File 'lib/uri/component/userinfo.rb', line 46

def domain
	return @domain
end

#domain=(v) ⇒ Object



50
51
52
53
54
55
# File 'lib/uri/component/userinfo.rb', line 50

def domain=(v)
	if v && !@user
	  raise InvalidURIError, "domain component depends user component"
	end
	@domain = v
end

#escape_element(v) ⇒ Object



29
30
31
# File 'lib/uri/component/userinfo.rb', line 29

def escape_element(v)
	return URI.escape(v, RE_ELEMENT_UNSAFE)
end

#passwordObject



68
69
70
# File 'lib/uri/component/userinfo.rb', line 68

def password
	return @password
end

#password=(v) ⇒ Object



72
73
74
75
76
77
# File 'lib/uri/component/userinfo.rb', line 72

def password=(v)
	if v && !@user
	  raise InvalidURIError, "password component depends user component"
	end
	@password = v
end

#to_uriObject Also known as: to_s



79
80
81
82
83
84
85
86
87
# File 'lib/uri/component/userinfo.rb', line 79

def to_uri
	return nil unless @user

	info_str = ''
	info_str += self.escape_element(@domain) + ';' if @domain
	info_str += @user ? self.escape_element(@user) : '';
	info_str += ':' + self.escape_element(@password) if @password
	return info_str
end

#userObject



57
58
59
# File 'lib/uri/component/userinfo.rb', line 57

def user
	return @user
end

#user=(v) ⇒ Object



61
62
63
64
65
66
# File 'lib/uri/component/userinfo.rb', line 61

def user=(v)
	if !v
	  @domain = @password = nil
	end
	@user = v
end