Parse a MongoDB URI. This method is used by MongoClient.from_uri. Returns an array of nodes and an array of db authorizations, if applicable.
@note Passwords can contain any character except for ‘,’
@param [String] uri The MongoDB URI string.
# File lib/mongo/functional/uri_parser.rb, line 162 def initialize(uri) if uri.start_with?('mongodb://') uri = uri[10..-1] else raise MongoArgumentError, "MongoDB URI must match this spec: #{MONGODB_URI_SPEC}" end hosts, opts = uri.split('?') parse_options(opts) parse_hosts(hosts) validate_connect end
Whether to immediately connect to the MongoDB node. Defaults to true. @return [true, false]
# File lib/mongo/functional/uri_parser.rb, line 207 def connect? connect != false end
Create a Mongo::MongoClient or a Mongo::MongoReplicaSetClient based on the URI.
@note Don’t confuse this with attribute getter method connect.
@return [MongoClient,MongoReplicaSetClient]
# File lib/mongo/functional/uri_parser.rb, line 180 def connection(extra_opts={}, legacy = false, sharded = false) opts = connection_options.merge!(extra_opts) if(legacy) if replicaset? ReplSetConnection.new(node_strings, opts) else Connection.new(host, port, opts) end else if sharded MongoShardedClient.new(node_strings, opts) elsif replicaset? MongoReplicaSetClient.new(node_strings, opts) else MongoClient.new(host, port, opts) end end end
Options that can be passed to MongoClient.new or MongoReplicaSetClient.new @return [Hash]
# File lib/mongo/functional/uri_parser.rb, line 234 def connection_options opts = {} if @wtimeout warn "Using wtimeout in a URI is deprecated, please use wtimeoutMS. It will be removed in v2.0." opts[:wtimeout] = @wtimeout end opts[:wtimeout] = @wtimeoutms if @wtimeoutms opts[:w] = 1 if @safe opts[:w] = @w if @w opts[:j] = @journal if @journal opts[:fsync] = @fsync if @fsync opts[:connect_timeout] = @connecttimeoutms if @connecttimeoutms opts[:op_timeout] = @sockettimeoutms if @sockettimeoutms opts[:pool_size] = @pool_size if @pool_size opts[:read] = @readpreference if @readpreference if @slaveok && !@readpreference unless replicaset? opts[:slave_ok] = true else opts[:read] = :secondary_preferred end end if replicaset.is_a?(String) opts[:name] = replicaset end opts[:db_name] = @db_name if @db_name opts[:auths] = @auths if @auths opts[:ssl] = @ssl if @ssl opts[:connect] = connect? opts end
Whether this represents a direct connection.
@note Specifying :connect => ‘direct’ has no effect… other than to raise an exception if other variables suggest a replicaset.
@return [true,false]
# File lib/mongo/functional/uri_parser.rb, line 216 def direct? !replicaset? end
For direct connections, the host of the (only) node. @return [String]
# File lib/mongo/functional/uri_parser.rb, line 222 def host nodes[0][0] end
# File lib/mongo/functional/uri_parser.rb, line 273 def node_strings nodes.map { |node| node.join(':') } end
Generated with the Darkfish Rdoc Generator 2.