lib/mongo/uri.rb in mongo-2.18.3 vs lib/mongo/uri.rb in mongo-2.19.0

- old
+ new

@@ -1,7 +1,7 @@ # frozen_string_literal: true -# encoding: utf-8 +# rubocop:todo all # Copyright (C) 2014-2020 MongoDB Inc. # # Licensed under the Apache License, Version 2.0 (the 'License'); # you may not use this file except in compliance with the License. @@ -18,11 +18,11 @@ module Mongo # The URI class provides a way for users to parse the MongoDB uri as # defined in the connection string format spec. # - # http://docs.mongodb.org/manual/reference/connection-string/ + # https://www.mongodb.com/docs/manual/reference/connection-string/ # # @example Use the uri string to make a client connection. # uri = Mongo::URI.new('mongodb://localhost:27017') # client = Mongo::Client.new(uri.servers, uri.options) # client.login(uri.credentials) @@ -78,11 +78,11 @@ ',...[,hostN[:portN]]][/[database][?options]]'.freeze # MongoDB URI (connection string) documentation url # # @since 2.0.0 - HELP = 'http://docs.mongodb.org/manual/reference/connection-string/'.freeze + HELP = 'https://www.mongodb.com/docs/manual/reference/connection-string/'.freeze # Unsafe characters that must be urlencoded. # # @since 2.1.0 UNSAFE = /[\:\/\@]/ @@ -233,11 +233,11 @@ end if string.empty? raise Error::InvalidURI.new(string, 'Cannot parse an empty URI.') end - scheme, _, remaining = string.partition(SCHEME_DELIM) + scheme, _, _ = string.partition(SCHEME_DELIM) case scheme when MONGODB_SCHEME URI.new(string, opts) when MONGODB_SRV_SCHEME SRVProtocol.new(string, opts) @@ -326,12 +326,49 @@ # @since 2.0.0 def database @database ? @database : Database::ADMIN end + # Get the uri as a string. + # + # @example Get the uri as a string. + # uri.to_s + # + # @return [ String ] The uri string. + def to_s + reconstruct_uri + end + private + # Reconstruct the URI from its parts. Invalid options are dropped and options + # are converted to camelCase. + # + # @return [ String ] the uri. + def reconstruct_uri + servers = @servers.join(',') + options = options_mapper.ruby_to_string(@uri_options).map do |k, vs| + unless vs.nil? + if vs.is_a?(Array) + vs.map { |v| "#{k}=#{v}" }.join('&') + else + "#{k}=#{vs}" + end + end + end.compact.join('&') + + uri = "#{scheme}#{SCHEME_DELIM}" + uri += @user.to_s if @user + uri += "#{AUTH_USER_PWD_DELIM}#{@password}" if @password + uri += "@" if @user || @password + uri += @query_hostname || servers + uri += "/" if @database || !options.empty? + uri += @database.to_s if @database + uri += "?#{options}" unless options.empty? + uri + end + def scheme MONGODB_SCHEME end def parse!(remaining) @@ -378,18 +415,9 @@ if db @database = parse_database!(db) end rescue Error::InvalidAddress => e raise_invalid_error!(e.message) - end - - def extract_db_opts!(string) - db_opts, _, creds_hosts = string.reverse.partition(DATABASE_DELIM) - db_opts, creds_hosts = creds_hosts, db_opts if creds_hosts.empty? - if db_opts.empty? && creds_hosts.include?(URI_OPTS_DELIM) - raise_invalid_error!(INVALID_OPTS_DELIM) - end - [ creds_hosts, db_opts ].map { |s| s.reverse } end def options_mapper @options_mapper ||= OptionsMapper.new( logger: @options[:logger],