Method: Mongo::URI::OptionsMapper#ruby_to_string

Defined in:
lib/mongo/uri/options_mapper.rb

#ruby_to_string(opts) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts Ruby options provided to their representation in a URI string.

Parameters:

  • opts (Hash)

    Ruby options to convert.

Returns:

  • (Hash)

    URI string hash.

Since:

  • 2.0.0



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/mongo/uri/options_mapper.rb', line 169

def ruby_to_string(opts)
  rv = {}
  URI_OPTION_MAP.each do |uri_key, spec|
    if spec[:group]
      v = opts[spec[:group]]
      v = v && v[spec[:name]]
    else
      v = opts[spec[:name]]
    end
    unless v.nil?
      if type = spec[:type]
        v = send("stringify_#{type}", v)
      end
      canonical_key = URI_OPTION_CANONICAL_NAMES[uri_key]
      unless canonical_key
        raise ArgumentError, "Option #{uri_key} is not known"
      end
      rv[canonical_key] = v
    end
  end
  # For options that default to true, remove the value if it is true.
  %w(retryReads retryWrites).each do |k|
    if rv[k]
      rv.delete(k)
    end
  end
  # Remove auth source when it is $external for mechanisms that default
  # (or require) that auth source.
  if %w(MONGODB-AWS).include?(rv['authMechanism']) && rv['authSource'] == '$external'
    rv.delete('authSource')
  end
  # ssl and tls are aliases, remove ssl ones
  rv.delete('ssl')
  # TODO remove authSource if it is the same as the database,
  # requires this method to know the database specified in the client.
  rv
end