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



124
125
126
127
128
129
130
131
132
133
# File 'lib/dr/base/uri.rb', line 124

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



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/dr/base/uri.rb', line 137

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



100
101
102
103
104
105
106
107
108
109
# File 'lib/dr/base/uri.rb', line 100

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



111
112
113
114
115
# File 'lib/dr/base/uri.rb', line 111

def to_json(_state = nil)
  h=to_h
  h[:uri]=h[:uri].to_s #h[:uri] is a URIWrapper, so convert it to string so json does not convert it again
  h.to_json
end

#to_publicObject



117
118
119
120
121
# File 'lib/dr/base/uri.rb', line 117

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