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, :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 =
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}, :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 } }
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.
-
#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.
-
#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.
- #nodes ⇒ 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.
114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/mongo/util/uri_parser.rb', line 114 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.
92 93 94 |
# File 'lib/mongo/util/uri_parser.rb', line 92 def auths @auths end |
#connect ⇒ Object (readonly)
Returns the value of attribute connect.
92 93 94 |
# File 'lib/mongo/util/uri_parser.rb', line 92 def connect @connect end |
#connecttimeoutms ⇒ Object (readonly)
Returns the value of attribute connecttimeoutms.
92 93 94 |
# File 'lib/mongo/util/uri_parser.rb', line 92 def connecttimeoutms @connecttimeoutms end |
#fsync ⇒ Object (readonly)
Returns the value of attribute fsync.
92 93 94 |
# File 'lib/mongo/util/uri_parser.rb', line 92 def fsync @fsync end |
#journal ⇒ Object (readonly)
Returns the value of attribute journal.
92 93 94 |
# File 'lib/mongo/util/uri_parser.rb', line 92 def journal @journal end |
#replicaset ⇒ Object (readonly)
Returns the value of attribute replicaset.
92 93 94 |
# File 'lib/mongo/util/uri_parser.rb', line 92 def replicaset @replicaset end |
#safe ⇒ Object (readonly)
Returns the value of attribute safe.
92 93 94 |
# File 'lib/mongo/util/uri_parser.rb', line 92 def safe @safe end |
#slaveok ⇒ Object (readonly)
Returns the value of attribute slaveok.
92 93 94 |
# File 'lib/mongo/util/uri_parser.rb', line 92 def slaveok @slaveok end |
#sockettimeoutms ⇒ Object (readonly)
Returns the value of attribute sockettimeoutms.
92 93 94 |
# File 'lib/mongo/util/uri_parser.rb', line 92 def sockettimeoutms @sockettimeoutms end |
#w ⇒ Object (readonly)
Returns the value of attribute w.
92 93 94 |
# File 'lib/mongo/util/uri_parser.rb', line 92 def w @w end |
#wtimeout ⇒ Object (readonly)
Returns the value of attribute wtimeout.
92 93 94 |
# File 'lib/mongo/util/uri_parser.rb', line 92 def wtimeout @wtimeout end |
#wtimeoutms ⇒ Object (readonly)
Returns the value of attribute wtimeoutms.
92 93 94 |
# File 'lib/mongo/util/uri_parser.rb', line 92 def wtimeoutms @wtimeoutms end |
Instance Method Details
#connect? ⇒ true, false
Whether to immediately connect to the MongoDB node. Defaults to true.
157 158 159 |
# File 'lib/mongo/util/uri_parser.rb', line 157 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.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/mongo/util/uri_parser.rb', line 132 def connection(extra_opts, legacy=false) opts = .merge! extra_opts if(legacy) if replicaset? ReplSetConnection.new(nodes, opts) else Connection.new(host, port, opts) end else if replicaset? MongoReplicaSetClient.new(nodes, opts) else MongoClient.new(host, port, opts) end end end |
#connection_options ⇒ Hash
Options that can be passed to MongoClient.new or MongoReplicaSetClient.new
184 185 186 187 188 189 190 191 192 193 194 195 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 |
# File 'lib/mongo/util/uri_parser.rb', line 184 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 @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 |
#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.
166 167 168 |
# File 'lib/mongo/util/uri_parser.rb', line 166 def direct? !replicaset? end |
#host ⇒ String
For direct connections, the host of the (only) node.
172 173 174 |
# File 'lib/mongo/util/uri_parser.rb', line 172 def host nodes[0][0] end |
#nodes ⇒ Object
227 228 229 230 231 232 233 |
# File 'lib/mongo/util/uri_parser.rb', line 227 def nodes if @nodes.length == 1 @nodes else @nodes.collect {|node| "#{node[0]}:#{node[1]}"} end end |
#port ⇒ Integer
For direct connections, the port of the (only) node.
178 179 180 |
# File 'lib/mongo/util/uri_parser.rb', line 178 def port nodes[0][1].to_i end |
#replicaset? ⇒ true, false
Whether this represents a replica set.
151 152 153 |
# File 'lib/mongo/util/uri_parser.rb', line 151 def replicaset? replicaset.is_a?(String) || nodes.length > 1 end |