Parent

Mongo::URIParser

Attributes

auths[R]
connect[R]
connecttimeoutms[R]
fsync[R]
journal[R]
nodes[R]
pool_size[R]
readpreference[R]
replicaset[R]
safe[R]
slaveok[R]
sockettimeoutms[R]
ssl[R]
w[R]
wtimeout[R]
wtimeoutms[R]

Public Class Methods

new(uri) click to toggle source

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. @param [Hash,nil] extra_opts Extra options. Will override anything already specified in the URI.

@core connections

# File lib/mongo/util/uri_parser.rb, line 139
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_hosts(hosts)
  parse_options(opts)
  validate_connect
end

Public Instance Methods

connect?() click to toggle source

Whether to immediately connect to the MongoDB node. Defaults to true. @return [true, false]

# File lib/mongo/util/uri_parser.rb, line 184
def connect?
  connect != false
end
connection(extra_opts, legacy = false, sharded = false) click to toggle source

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/util/uri_parser.rb, line 157
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
connection_options() click to toggle source

Options that can be passed to MongoClient.new or MongoReplicaSetClient.new @return [Hash]

# File lib/mongo/util/uri_parser.rb, line 211
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

  opts[:w] = 1 if @safe
  opts[:w] = @w if @w
  opts[:j] = @journal
  opts[:fsync] = @fsync

  if @connecttimeoutms
    opts[:connect_timeout] = @connecttimeoutms
  end

  if @sockettimeoutms
    opts[:op_timeout] = @sockettimeoutms
  end

  if @pool_size
    opts[:pool_size] = @pool_size
  end

  if @readpreference
    opts[:read] = @readpreference
  end

  if @slaveok && !@readpreference
    unless replicaset?
      opts[:slave_ok] = true
    else
      opts[:read] = :secondary_preferred
    end
  end

  opts[:ssl] = @ssl
  opts[:auths] = auths

  if replicaset.is_a?(String)
    opts[:name] = replicaset
  end

  opts[:connect] = connect?

  opts
end
direct?() click to toggle source

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/util/uri_parser.rb, line 193
def direct?
  !replicaset?
end
host() click to toggle source

For direct connections, the host of the (only) node. @return [String]

# File lib/mongo/util/uri_parser.rb, line 199
def host
  nodes[0][0]
end
node_strings() click to toggle source
# File lib/mongo/util/uri_parser.rb, line 261
def node_strings
  nodes.map { |node| node.join(':') }
end
port() click to toggle source

For direct connections, the port of the (only) node. @return [Integer]

# File lib/mongo/util/uri_parser.rb, line 205
def port
  nodes[0][1].to_i
end
replicaset?() click to toggle source

Whether this represents a replica set. @return [true,false]

# File lib/mongo/util/uri_parser.rb, line 178
def replicaset?
  replicaset.is_a?(String) || nodes.length > 1
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.