| Class | Mongo::URIParser |
| In: |
lib/mongo/util/uri_parser.rb
|
| Parent: | Object |
| DEFAULT_PORT | = | 27017 |
| MONGODB_URI_MATCHER | = | /(([-.\w:]+):([^@,]+)@)?((?:(?:[-.\w]+)(?::(?:[\w]+))?,?)+)(\/([-\w]+))?/ |
| MONGODB_URI_SPEC | = | "mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/database]" |
| SPEC_ATTRS | = | [:nodes, :auths] |
| OPT_ATTRS | = | [:connect, :replicaset, :slaveok, :safe, :w, :wtimeout, :fsync] |
| OPT_VALID | = | {:connect => lambda {|arg| ['direct', 'replicaset'].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)} |
| OPT_ERR | = | {:connect => "must be 'direct' or 'replicaset'", :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'" |
| OPT_CONV | = | {:connect => lambda {|arg| arg}, :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} |
| auths | [R] | |
| connect | [R] | |
| fsync | [R] | |
| nodes | [R] | |
| replicaset | [R] | |
| safe | [R] | |
| slaveok | [R] | |
| w | [R] | |
| wtimeout | [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 a ’,’.
@core connections
# File lib/mongo/util/uri_parser.rb, line 63 def initialize(string) if string =~ /^mongodb:\/\// string = string[10..-1] else raise MongoArgumentError, "MongoDB URI must match this spec: #{MONGODB_URI_SPEC}" end hosts, opts = string.split('?') parse_hosts(hosts) parse_options(opts) configure_connect end
# File lib/mongo/util/uri_parser.rb, line 76 def connection_options opts = {} if (@w || @wtimeout || @fsync) && !@safe raise MongoArgumentError, "Safe must be true if w, wtimeout, or fsync is specified" end if @safe if @w || @wtimeout || @fsync safe_opts = {} safe_opts[:w] = @w if @w safe_opts[:wtimeout] = @wtimeout if @wtimeout safe_opts[:fsync] = @fsync if @fsync else safe_opts = true end opts[:safe] = safe_opts end if @slaveok if @connect == 'direct' opts[:slave_ok] = true else opts[:read_secondary] = true end end opts[:rs_name] = @replicaset if @replicaset opts end