Class | Mongo::URIParser |
In: |
lib/mongo/util/uri_parser.rb
|
Parent: | Object |
USER_REGEX | = | /([-.\w:]+)/ |
PASS_REGEX | = | /([^@,]+)/ |
AUTH_REGEX | = | /(#{USER_REGEX}:#{PASS_REGEX}@)?/ |
HOST_REGEX | = | /([-.\w]+)/ |
PORT_REGEX | = | /(?::(\w+))?/ |
NODE_REGEX | = | /((#{HOST_REGEX}#{PORT_REGEX},?)+)/ |
PATH_REGEX | = | /(?:\/([-\w]+))?/ |
MONGODB_URI_MATCHER | = | /#{AUTH_REGEX}#{NODE_REGEX}#{PATH_REGEX}/ |
MONGODB_URI_SPEC | = | "mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]" |
SPEC_ATTRS | = | [:nodes, :auths] |
OPT_ATTRS | = | [:connect, :replicaset, :slaveok, :safe, :w, :wtimeout, :fsync, :journal, :connecttimeoutms, :sockettimeoutms, :wtimeoutms] |
OPT_VALID | = | {:connect => lambda {|arg| ['direct', 'replicaset', 'true', 'false', true, false].include?(arg)}, :replicaset => lambda {|arg| arg.length > 0}, :slaveok => lambda {|arg| ['true', 'false'].include?(arg)}, :safe => lambda {|arg| ['true', 'false'].include?(arg)}, :w => lambda {|arg| arg =~ /^\d+$/ }, :wtimeout => lambda {|arg| arg =~ /^\d+$/ }, :fsync => lambda {|arg| ['true', 'false'].include?(arg)}, :journal => lambda {|arg| ['true', 'false'].include?(arg)}, :connecttimeoutms => lambda {|arg| arg =~ /^\d+$/ }, :sockettimeoutms => lambda {|arg| arg =~ /^\d+$/ }, :wtimeoutms => lambda {|arg| arg =~ /^\d+$/ } |
OPT_ERR | = | {:connect => "must be 'direct', 'replicaset', 'true', or 'false'", :replicaset => "must be a string containing the name of the replica set to connect to", :slaveok => "must be 'true' or 'false'", :safe => "must be 'true' or 'false'", :w => "must be an integer specifying number of nodes to replica to", :wtimeout => "must be an integer specifying milliseconds", :fsync => "must be 'true' or 'false'", :journal => "must be 'true' or 'false'", :connecttimeoutms => "must be an integer specifying milliseconds", :sockettimeoutms => "must be an integer specifying milliseconds", :wtimeoutms => "must be an integer specifying milliseconds" |
OPT_CONV | = | {:connect => lambda {|arg| arg == 'false' ? false : arg}, # be sure to convert 'false' to FalseClass :replicaset => lambda {|arg| arg}, :slaveok => lambda {|arg| arg == 'true' ? true : false}, :safe => lambda {|arg| arg == 'true' ? true : false}, :w => lambda {|arg| arg.to_i}, :wtimeout => lambda {|arg| arg.to_i}, :fsync => lambda {|arg| arg == 'true' ? true : false}, :journal => lambda {|arg| arg == 'true' ? true : false}, :connecttimeoutms => lambda {|arg| arg.to_f / 1000 }, # stored as seconds :sockettimeoutms => lambda {|arg| arg.to_f / 1000 }, # stored as seconds :wtimeoutms => lambda {|arg| arg.to_i } |
auths | [R] | |
connect | [R] | |
connecttimeoutms | [R] | |
fsync | [R] | |
journal | [R] | |
nodes | [R] | |
replicaset | [R] | |
safe | [R] | |
slaveok | [R] | |
sockettimeoutms | [R] | |
w | [R] | |
wtimeout | [R] | |
wtimeoutms | [R] |
Parse a MongoDB URI. This method is used by Connection.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 90 def initialize(uri, extra_opts={}) 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, extra_opts) validate_connect end
Whether to immediately connect to the MongoDB node[s]. Defaults to true. @return [true, false]
# File lib/mongo/util/uri_parser.rb, line 124 def connect? connect != false end
Create a Mongo::Connection or a Mongo::ReplSetConnection based on the URI.
@note Don‘t confuse this with attribute getter method connect.
@return [Connection,ReplSetConnection]
# File lib/mongo/util/uri_parser.rb, line 108 def connection if replicaset? ReplSetConnection.new(*(nodes+[connection_options])) else Connection.new(host, port, connection_options) end end
Options that can be passed to Mongo::Connection.new or Mongo::ReplSetConnection.new @return [Hash]
# File lib/mongo/util/uri_parser.rb, line 151 def connection_options opts = {} if (@w || @journal || @wtimeout || @fsync || @wtimeoutms) && !@safe raise MongoArgumentError, "Safe must be true if w, journal, wtimeoutMS, or fsync is specified" end if @safe if @w || @journal || @wtimeout || @fsync || @wtimeoutms safe_opts = {} safe_opts[:w] = @w if @w safe_opts[:j] = @journal if @journal if @wtimeout warn "Using wtimeout in a URI is deprecated, please use wtimeoutMS. It will be removed in v2.0." safe_opts[:wtimeout] = @wtimeout end if @wtimeoutms safe_opts[:wtimeout] = @wtimeoutms end safe_opts[:fsync] = @fsync if @fsync else safe_opts = true end opts[:safe] = safe_opts end if @connecttimeoutms opts[:connect_timeout] = @connecttimeoutms end if @sockettimeoutms opts[:op_timeout] = @sockettimeoutms end if @slaveok if direct? opts[:slave_ok] = true else opts[:read] = :secondary end end if direct? opts[:auths] = auths end if replicaset.is_a?(String) opts[:name] = replicaset end 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/util/uri_parser.rb, line 133 def direct? !replicaset? end