Class: Mongo::URIParser
Constant Summary collapse
- 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]
- READ_PREFERENCES =
{ "primary" => :primary, "primarypreferred" => :primary_preferred, "secondary" => :secondary, "secondarypreferred" => :secondary_preferred, "nearest" => :nearest }
- OPT_ATTRS =
[ :connect, :connecttimeoutms, :fsync, :journal, :pool_size, :readpreference, :replicaset, :safe, :slaveok, :sockettimeoutms, :ssl, :w, :wtimeout, :wtimeoutms ]
- OPT_VALID =
{ :connect => lambda { |arg| [ 'direct', 'replicaset', 'true', 'false', true, false ].include?(arg) }, :connecttimeoutms => lambda { |arg| arg =~ /^\d+$/ }, :fsync => lambda { |arg| ['true', 'false'].include?(arg) }, :journal => lambda { |arg| ['true', 'false'].include?(arg) }, :pool_size => lambda { |arg| arg.to_i > 0 }, :readpreference => lambda { |arg| READ_PREFERENCES.keys.include?(arg) }, :replicaset => lambda { |arg| arg.length > 0 }, :safe => lambda { |arg| ['true', 'false'].include?(arg) }, :slaveok => lambda { |arg| ['true', 'false'].include?(arg) }, :sockettimeoutms => lambda { |arg| arg =~ /^\d+$/ }, :ssl => lambda { |arg| ['true', 'false'].include?(arg) }, :w => lambda { |arg| arg =~ /^\w+$/ }, :wtimeout => lambda { |arg| arg =~ /^\d+$/ }, :wtimeoutms => lambda { |arg| arg =~ /^\d+$/ } }
- OPT_ERR =
{ :connect => "must be 'direct', 'replicaset', 'true', or 'false'", :connecttimeoutms => "must be an integer specifying milliseconds", :fsync => "must be 'true' or 'false'", :journal => "must be 'true' or 'false'", :pool_size => "must be an integer greater than zero", :readpreference => "must be on of #{READ_PREFERENCES.keys.map(&:inspect).join(",")}", :replicaset => "must be a string containing the name of the replica set to connect to", :safe => "must be 'true' or 'false'", :slaveok => "must be 'true' or 'false'", :sockettimeoutms => "must be an integer specifying milliseconds", :ssl => "must be 'true' or 'false'", :w => "must be an integer indicating number of nodes to replicate to or a string " + "specifying that replication is required to the majority or nodes with a " + "particilar getLastErrorMode.", :wtimeout => "must be an integer specifying milliseconds", :wtimeoutms => "must be an integer specifying milliseconds" }
- OPT_CONV =
{ :connect => lambda { |arg| arg == 'false' ? false : arg }, # convert 'false' to FalseClass :connecttimeoutms => lambda { |arg| arg.to_f / 1000 }, # stored as seconds :fsync => lambda { |arg| arg == 'true' ? true : false }, :journal => lambda { |arg| arg == 'true' ? true : false }, :pool_size => lambda { |arg| arg.to_i }, :readpreference => lambda { |arg| READ_PREFERENCES[arg] }, :replicaset => lambda { |arg| arg }, :safe => lambda { |arg| arg == 'true' ? true : false }, :slaveok => lambda { |arg| arg == 'true' ? true : false }, :sockettimeoutms => lambda { |arg| arg.to_f / 1000 }, # stored as seconds :ssl => lambda { |arg| arg == 'true' ? true : false }, :w => lambda { |arg| Mongo::Support.is_i?(arg) ? arg.to_i : arg.to_sym }, :wtimeout => lambda { |arg| arg.to_i }, :wtimeoutms => lambda { |arg| arg.to_i } }
Instance Attribute Summary collapse
-
#auths ⇒ Object
readonly
Returns the value of attribute auths.
-
#connect ⇒ Object
readonly
Returns the value of attribute connect.
-
#connecttimeoutms ⇒ Object
readonly
Returns the value of attribute connecttimeoutms.
-
#fsync ⇒ Object
readonly
Returns the value of attribute fsync.
-
#journal ⇒ Object
readonly
Returns the value of attribute journal.
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
-
#pool_size ⇒ Object
readonly
Returns the value of attribute pool_size.
-
#readpreference ⇒ Object
readonly
Returns the value of attribute readpreference.
-
#replicaset ⇒ Object
readonly
Returns the value of attribute replicaset.
-
#safe ⇒ Object
readonly
Returns the value of attribute safe.
-
#slaveok ⇒ Object
readonly
Returns the value of attribute slaveok.
-
#sockettimeoutms ⇒ Object
readonly
Returns the value of attribute sockettimeoutms.
-
#ssl ⇒ Object
readonly
Returns the value of attribute ssl.
-
#w ⇒ Object
readonly
Returns the value of attribute w.
-
#wtimeout ⇒ Object
readonly
Returns the value of attribute wtimeout.
-
#wtimeoutms ⇒ Object
readonly
Returns the value of attribute wtimeoutms.
Instance Method Summary collapse
-
#connect? ⇒ true, false
Whether to immediately connect to the MongoDB node.
-
#connection(extra_opts, legacy = false, sharded = false) ⇒ MongoClient, MongoReplicaSetClient
Create a Mongo::MongoClient or a Mongo::MongoReplicaSetClient based on the URI.
-
#connection_options ⇒ Hash
Options that can be passed to MongoClient.new or MongoReplicaSetClient.new.
-
#direct? ⇒ true, false
Whether this represents a direct connection.
-
#host ⇒ String
For direct connections, the host of the (only) node.
-
#initialize(uri) ⇒ URIParser
constructor
Parse a MongoDB URI.
- #node_strings ⇒ Object
-
#port ⇒ Integer
For direct connections, the port of the (only) node.
-
#replicaset? ⇒ true, false
Whether this represents a replica set.
Constructor Details
#initialize(uri) ⇒ URIParser
Passwords can contain any character except for ‘,’
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.
125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/mongo/util/uri_parser.rb', line 125 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) (opts) validate_connect end |
Instance Attribute Details
#auths ⇒ Object (readonly)
Returns the value of attribute auths.
99 100 101 |
# File 'lib/mongo/util/uri_parser.rb', line 99 def auths @auths end |
#connect ⇒ Object (readonly)
Returns the value of attribute connect.
99 100 101 |
# File 'lib/mongo/util/uri_parser.rb', line 99 def connect @connect end |
#connecttimeoutms ⇒ Object (readonly)
Returns the value of attribute connecttimeoutms.
99 100 101 |
# File 'lib/mongo/util/uri_parser.rb', line 99 def connecttimeoutms @connecttimeoutms end |
#fsync ⇒ Object (readonly)
Returns the value of attribute fsync.
99 100 101 |
# File 'lib/mongo/util/uri_parser.rb', line 99 def fsync @fsync end |
#journal ⇒ Object (readonly)
Returns the value of attribute journal.
99 100 101 |
# File 'lib/mongo/util/uri_parser.rb', line 99 def journal @journal end |
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
99 100 101 |
# File 'lib/mongo/util/uri_parser.rb', line 99 def nodes @nodes end |
#pool_size ⇒ Object (readonly)
Returns the value of attribute pool_size.
99 100 101 |
# File 'lib/mongo/util/uri_parser.rb', line 99 def pool_size @pool_size end |
#readpreference ⇒ Object (readonly)
Returns the value of attribute readpreference.
99 100 101 |
# File 'lib/mongo/util/uri_parser.rb', line 99 def readpreference @readpreference end |
#replicaset ⇒ Object (readonly)
Returns the value of attribute replicaset.
99 100 101 |
# File 'lib/mongo/util/uri_parser.rb', line 99 def replicaset @replicaset end |
#safe ⇒ Object (readonly)
Returns the value of attribute safe.
99 100 101 |
# File 'lib/mongo/util/uri_parser.rb', line 99 def safe @safe end |
#slaveok ⇒ Object (readonly)
Returns the value of attribute slaveok.
99 100 101 |
# File 'lib/mongo/util/uri_parser.rb', line 99 def slaveok @slaveok end |
#sockettimeoutms ⇒ Object (readonly)
Returns the value of attribute sockettimeoutms.
99 100 101 |
# File 'lib/mongo/util/uri_parser.rb', line 99 def sockettimeoutms @sockettimeoutms end |
#ssl ⇒ Object (readonly)
Returns the value of attribute ssl.
99 100 101 |
# File 'lib/mongo/util/uri_parser.rb', line 99 def ssl @ssl end |
#w ⇒ Object (readonly)
Returns the value of attribute w.
99 100 101 |
# File 'lib/mongo/util/uri_parser.rb', line 99 def w @w end |
#wtimeout ⇒ Object (readonly)
Returns the value of attribute wtimeout.
99 100 101 |
# File 'lib/mongo/util/uri_parser.rb', line 99 def wtimeout @wtimeout end |
#wtimeoutms ⇒ Object (readonly)
Returns the value of attribute wtimeoutms.
99 100 101 |
# File 'lib/mongo/util/uri_parser.rb', line 99 def wtimeoutms @wtimeoutms end |
Instance Method Details
#connect? ⇒ true, false
Whether to immediately connect to the MongoDB node. Defaults to true.
170 171 172 |
# File 'lib/mongo/util/uri_parser.rb', line 170 def connect? connect != false end |
#connection(extra_opts, legacy = false, sharded = false) ⇒ MongoClient, MongoReplicaSetClient
Don’t confuse this with attribute getter method #connect.
Create a Mongo::MongoClient or a Mongo::MongoReplicaSetClient based on the URI.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/mongo/util/uri_parser.rb', line 143 def connection(extra_opts, legacy = false, sharded = false) opts = .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 ⇒ Hash
Options that can be passed to MongoClient.new or MongoReplicaSetClient.new
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/mongo/util/uri_parser.rb', line 197 def 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 if direct? opts[:slave_ok] = true else opts[:read] = :secondary_preferred end end opts[:ssl] = @ssl if direct? opts[:auths] = auths end if replicaset.is_a?(String) opts[:name] = replicaset end opts[:connect] = connect? opts end |
#direct? ⇒ true, false
Specifying :connect => ‘direct’ has no effect… other than to raise an exception if other variables suggest a replicaset.
Whether this represents a direct connection.
179 180 181 |
# File 'lib/mongo/util/uri_parser.rb', line 179 def direct? !replicaset? end |
#host ⇒ String
For direct connections, the host of the (only) node.
185 186 187 |
# File 'lib/mongo/util/uri_parser.rb', line 185 def host nodes[0][0] end |
#node_strings ⇒ Object
250 251 252 |
# File 'lib/mongo/util/uri_parser.rb', line 250 def node_strings nodes.map { |node| node.join(':') } end |
#port ⇒ Integer
For direct connections, the port of the (only) node.
191 192 193 |
# File 'lib/mongo/util/uri_parser.rb', line 191 def port nodes[0][1].to_i end |
#replicaset? ⇒ true, false
Whether this represents a replica set.
164 165 166 |
# File 'lib/mongo/util/uri_parser.rb', line 164 def replicaset? replicaset.is_a?(String) || nodes.length > 1 end |