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]
- OPT_ATTRS =
[ :connect, :replicaset, :slaveok, :ssl, :safe, :w, :wtimeout, :fsync, :journal, :connecttimeoutms, :sockettimeoutms, :wtimeoutms, :pool_size ]
- 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)}, :ssl => lambda {|arg| ['true', 'false'].include?(arg)}, :safe => lambda {|arg| ['true', 'false'].include?(arg)}, :w => lambda {|arg| arg =~ /^\w+$/ }, :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+$/ }, :pool_size => lambda {|arg| arg.to_i > 0 } }
- 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'", :ssl => "must be 'true' or 'false'", :safe => "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", :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", :pool_size => "must be an integer greater than zero" }
- OPT_CONV =
be sure to convert ‘false’ to FalseClass
{: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}, :ssl => lambda {|arg| arg == 'true' ? true : false}, :safe => 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}, :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 }, :pool_size => 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.
-
#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) ⇒ 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.
126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/mongo/util/uri_parser.rb', line 126 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.
101 102 103 |
# File 'lib/mongo/util/uri_parser.rb', line 101 def auths @auths end |
#connect ⇒ Object (readonly)
Returns the value of attribute connect.
101 102 103 |
# File 'lib/mongo/util/uri_parser.rb', line 101 def connect @connect end |
#connecttimeoutms ⇒ Object (readonly)
Returns the value of attribute connecttimeoutms.
101 102 103 |
# File 'lib/mongo/util/uri_parser.rb', line 101 def connecttimeoutms @connecttimeoutms end |
#fsync ⇒ Object (readonly)
Returns the value of attribute fsync.
101 102 103 |
# File 'lib/mongo/util/uri_parser.rb', line 101 def fsync @fsync end |
#journal ⇒ Object (readonly)
Returns the value of attribute journal.
101 102 103 |
# File 'lib/mongo/util/uri_parser.rb', line 101 def journal @journal end |
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
101 102 103 |
# File 'lib/mongo/util/uri_parser.rb', line 101 def nodes @nodes end |
#pool_size ⇒ Object (readonly)
Returns the value of attribute pool_size.
101 102 103 |
# File 'lib/mongo/util/uri_parser.rb', line 101 def pool_size @pool_size end |
#replicaset ⇒ Object (readonly)
Returns the value of attribute replicaset.
101 102 103 |
# File 'lib/mongo/util/uri_parser.rb', line 101 def replicaset @replicaset end |
#safe ⇒ Object (readonly)
Returns the value of attribute safe.
101 102 103 |
# File 'lib/mongo/util/uri_parser.rb', line 101 def safe @safe end |
#slaveok ⇒ Object (readonly)
Returns the value of attribute slaveok.
101 102 103 |
# File 'lib/mongo/util/uri_parser.rb', line 101 def slaveok @slaveok end |
#sockettimeoutms ⇒ Object (readonly)
Returns the value of attribute sockettimeoutms.
101 102 103 |
# File 'lib/mongo/util/uri_parser.rb', line 101 def sockettimeoutms @sockettimeoutms end |
#ssl ⇒ Object (readonly)
Returns the value of attribute ssl.
101 102 103 |
# File 'lib/mongo/util/uri_parser.rb', line 101 def ssl @ssl end |
#w ⇒ Object (readonly)
Returns the value of attribute w.
101 102 103 |
# File 'lib/mongo/util/uri_parser.rb', line 101 def w @w end |
#wtimeout ⇒ Object (readonly)
Returns the value of attribute wtimeout.
101 102 103 |
# File 'lib/mongo/util/uri_parser.rb', line 101 def wtimeout @wtimeout end |
#wtimeoutms ⇒ Object (readonly)
Returns the value of attribute wtimeoutms.
101 102 103 |
# File 'lib/mongo/util/uri_parser.rb', line 101 def wtimeoutms @wtimeoutms end |
Instance Method Details
#connect? ⇒ true, false
Whether to immediately connect to the MongoDB node. Defaults to true.
169 170 171 |
# File 'lib/mongo/util/uri_parser.rb', line 169 def connect? connect != false end |
#connection(extra_opts, legacy = false) ⇒ MongoClient, MongoReplicaSetClient
Don’t confuse this with attribute getter method #connect.
Create a Mongo::MongoClient or a Mongo::MongoReplicaSetClient based on the URI.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/mongo/util/uri_parser.rb', line 144 def connection(extra_opts, legacy=false) opts = .merge! extra_opts if(legacy) if replicaset? ReplSetConnection.new(node_strings, opts) else Connection.new(host, port, opts) end else if 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
196 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 |
# File 'lib/mongo/util/uri_parser.rb', line 196 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 @slaveok 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.
178 179 180 |
# File 'lib/mongo/util/uri_parser.rb', line 178 def direct? !replicaset? end |
#host ⇒ String
For direct connections, the host of the (only) node.
184 185 186 |
# File 'lib/mongo/util/uri_parser.rb', line 184 def host nodes[0][0] end |
#node_strings ⇒ Object
245 246 247 |
# File 'lib/mongo/util/uri_parser.rb', line 245 def node_strings nodes.map { |node| node.join(':') } end |
#port ⇒ Integer
For direct connections, the port of the (only) node.
190 191 192 |
# File 'lib/mongo/util/uri_parser.rb', line 190 def port nodes[0][1].to_i end |
#replicaset? ⇒ true, false
Whether this represents a replica set.
163 164 165 |
# File 'lib/mongo/util/uri_parser.rb', line 163 def replicaset? replicaset.is_a?(String) || nodes.length > 1 end |