Module: DR::URIlikeWrapper

Included in:
URIWrapper
Defined in:
lib/dr/base/uri.rb

Instance Method Summary collapse

Instance Method Details

#reverse_merge(u2) ⇒ Object

uri=u2.merge(uri) does not work if uri is absolute



109
110
111
112
113
114
115
116
117
118
# File 'lib/dr/base/uri.rb', line 109

def reverse_merge(u2)
	# return self unless uri.scheme
	u2 = u2.clone
	u2 = self.class.new(u2) unless u2.is_a?(self.class)
	if opaque.nil? == u2.opaque.nil?
		u2.soft_merge(self)
	else
		self
	end
end

#soft_merge(u2) ⇒ Object

merge(u2) replace self by u2 if u2 is aboslute soft_merge looks at each u2 components



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/dr/base/uri.rb', line 122

def soft_merge(u2)
	# we want automatic unescaping of u2 components
	u2 = self.class.new(u2) unless u2.is_a?(self.class)
	# only merge if we are both opaque or path like
	if opaque.nil? == u2.opaque.nil?
		components = uri.component
		if components.include?(:userinfo)
			components += %i[user password]
			components.delete(:userinfo)
		end
		components.each do |m|
			# path returns "" by default but we don't want to merge in this case
			if u2.respond_to?(m) && (v = u2.public_send(m)) && !((v == "") && (m == :path))
				uri.public_send(:"#{m}=", v)
			end
		end
	end
	self
end

#to_hObject



87
88
89
90
91
92
93
94
95
96
# File 'lib/dr/base/uri.rb', line 87

def to_h
	h = { uri: uri }
	components = uri.component
	components += %i[user password] if components.include?(:userinfo)
	components.each do |m|
		v = uri.public_send(m)
		v && h[m] = v
	end
	h
end

#to_json(_state = nil) ⇒ Object



98
99
100
# File 'lib/dr/base/uri.rb', line 98

def to_json(_state = nil)
	to_h.to_json
end

#to_publicObject



102
103
104
105
106
# File 'lib/dr/base/uri.rb', line 102

def to_public
	pub = dup
	pub.password = nil
	pub.to_s
end