@@ -502,35 +502,15 @@ def hash
502502 def initialize ( addresses_or_uri , options = nil )
503503 options = options ? options . dup : { }
504504
505- srv_uri = nil
506- if addresses_or_uri . is_a? ( ::String )
507- uri = URI . get ( addresses_or_uri , options )
508- if uri . is_a? ( URI ::SRVProtocol )
509- # If the URI is an SRV URI, note this so that we can start
510- # SRV polling if the topology is a sharded cluster.
511- srv_uri = uri
512- end
513- addresses = uri . servers
514- uri_options = uri . client_options . dup
515- # Special handing for :write and :write_concern: allow client Ruby
516- # options to override URI options, even when the Ruby option uses the
517- # deprecated :write key and the URI option uses the current
518- # :write_concern key
519- if options [ :write ]
520- uri_options . delete ( :write_concern )
521- end
522- options = uri_options . merge ( options )
523- @srv_records = uri . srv_records
524- else
525- addresses = addresses_or_uri
526- addresses . each do |addr |
527- if addr =~ /\A mongodb(\+ srv)?:\/ \/ /i
528- raise ArgumentError , "Host '#{ addr } ' should not contain protocol. Did you mean to not use an array?"
529- end
530- end
505+ processed = process_addresses ( addresses_or_uri , options )
531506
532- @srv_records = nil
533- end
507+ uri = processed [ :uri ]
508+ addresses = processed [ :addresses ]
509+ options = processed [ :options ]
510+
511+ # If the URI is an SRV URI, note this so that we can start
512+ # SRV polling if the topology is a sharded cluster.
513+ srv_uri = uri if uri . is_a? ( URI ::SRVProtocol )
534514
535515 options = self . class . canonicalize_ruby_options ( options )
536516
@@ -1217,6 +1197,73 @@ def timeout_sec
12171197
12181198 private
12191199
1200+ # Attempts to parse the given list of addresses, using the provided options.
1201+ #
1202+ # @param [ String | Array<String> ] addresses the list of addresses
1203+ # @param [ Hash ] options the options that may drive how the list is
1204+ # processed.
1205+ #
1206+ # @return [ Hash<:uri, :addresses, :options> ] the results of processing the
1207+ # list of addresses.
1208+ def process_addresses ( addresses , options )
1209+ if addresses . is_a? ( String )
1210+ process_addresses_string ( addresses , options )
1211+ else
1212+ process_addresses_array ( addresses , options )
1213+ end
1214+ end
1215+
1216+ # Attempts to parse the given list of addresses, using the provided options.
1217+ #
1218+ # @param [ String ] addresses the list of addresses
1219+ # @param [ Hash ] options the options that may drive how the list is
1220+ # processed.
1221+ #
1222+ # @return [ Hash<:uri, :addresses, :options> ] the results of processing the
1223+ # list of addresses.
1224+ def process_addresses_string ( addresses , options )
1225+ { } . tap do |processed |
1226+ processed [ :uri ] = uri = URI . get ( addresses , options )
1227+ processed [ :addresses ] = uri . servers
1228+
1229+ uri_options = uri . client_options . dup
1230+ # Special handing for :write and :write_concern: allow client Ruby
1231+ # options to override URI options, even when the Ruby option uses the
1232+ # deprecated :write key and the URI option uses the current
1233+ # :write_concern key
1234+ if options [ :write ]
1235+ uri_options . delete ( :write_concern )
1236+ end
1237+
1238+ processed [ :options ] = uri_options . merge ( options )
1239+
1240+ @srv_records = uri . srv_records
1241+ end
1242+ end
1243+
1244+ # Attempts to parse the given list of addresses, using the provided options.
1245+ #
1246+ # @param [ Array<String> ] addresses the list of addresses
1247+ # @param [ Hash ] options the options that may drive how the list is
1248+ # processed.
1249+ #
1250+ # @return [ Hash<:uri, :addresses, :options> ] the results of processing the
1251+ # list of addresses.
1252+ def process_addresses_array ( addresses , options )
1253+ { } . tap do |processed |
1254+ processed [ :addresses ] = addresses
1255+ processed [ :options ] = options
1256+
1257+ addresses . each do |addr |
1258+ if addr =~ /\A mongodb(\+ srv)?:\/ \/ /i
1259+ raise ArgumentError , "Host '#{ addr } ' should not contain protocol. Did you mean to not use an array?"
1260+ end
1261+ end
1262+
1263+ @srv_records = nil
1264+ end
1265+ end
1266+
12201267 # Create a new encrypter object using the client's auto encryption options
12211268 def build_encrypter
12221269 @encrypter = Crypt ::AutoEncrypter . new (
0 commit comments