Class: Mongo::URIParser

Inherits:
Object show all
Defined in:
lib/mongo/util/uri_parser.rb

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

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ URIParser

Note:

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.

Parameters:

  • uri (String)

    The MongoDB URI string.

  • extra_opts (Hash, nil)

    Extra options. Will override anything already specified in the URI.



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)
  parse_options(opts)
  validate_connect
end

Instance Attribute Details

#authsObject (readonly)

Returns the value of attribute auths.



101
102
103
# File 'lib/mongo/util/uri_parser.rb', line 101

def auths
  @auths
end

#connectObject (readonly)

Returns the value of attribute connect.



101
102
103
# File 'lib/mongo/util/uri_parser.rb', line 101

def connect
  @connect
end

#connecttimeoutmsObject (readonly)

Returns the value of attribute connecttimeoutms.



101
102
103
# File 'lib/mongo/util/uri_parser.rb', line 101

def connecttimeoutms
  @connecttimeoutms
end

#fsyncObject (readonly)

Returns the value of attribute fsync.



101
102
103
# File 'lib/mongo/util/uri_parser.rb', line 101

def fsync
  @fsync
end

#journalObject (readonly)

Returns the value of attribute journal.



101
102
103
# File 'lib/mongo/util/uri_parser.rb', line 101

def journal
  @journal
end

#nodesObject (readonly)

Returns the value of attribute nodes.



101
102
103
# File 'lib/mongo/util/uri_parser.rb', line 101

def nodes
  @nodes
end

#pool_sizeObject (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

#replicasetObject (readonly)

Returns the value of attribute replicaset.



101
102
103
# File 'lib/mongo/util/uri_parser.rb', line 101

def replicaset
  @replicaset
end

#safeObject (readonly)

Returns the value of attribute safe.



101
102
103
# File 'lib/mongo/util/uri_parser.rb', line 101

def safe
  @safe
end

#slaveokObject (readonly)

Returns the value of attribute slaveok.



101
102
103
# File 'lib/mongo/util/uri_parser.rb', line 101

def slaveok
  @slaveok
end

#sockettimeoutmsObject (readonly)

Returns the value of attribute sockettimeoutms.



101
102
103
# File 'lib/mongo/util/uri_parser.rb', line 101

def sockettimeoutms
  @sockettimeoutms
end

#sslObject (readonly)

Returns the value of attribute ssl.



101
102
103
# File 'lib/mongo/util/uri_parser.rb', line 101

def ssl
  @ssl
end

#wObject (readonly)

Returns the value of attribute w.



101
102
103
# File 'lib/mongo/util/uri_parser.rb', line 101

def w
  @w
end

#wtimeoutObject (readonly)

Returns the value of attribute wtimeout.



101
102
103
# File 'lib/mongo/util/uri_parser.rb', line 101

def wtimeout
  @wtimeout
end

#wtimeoutmsObject (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.

Returns:

  • (true, false)


169
170
171
# File 'lib/mongo/util/uri_parser.rb', line 169

def connect?
  connect != false
end

#connection(extra_opts, legacy = false) ⇒ MongoClient, MongoReplicaSetClient

Note:

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 = connection_options.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_optionsHash

Options that can be passed to MongoClient.new or MongoReplicaSetClient.new

Returns:



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 connection_options
  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

Note:

Specifying :connect => ‘direct’ has no effect… other than to raise an exception if other variables suggest a replicaset.

Whether this represents a direct connection.

Returns:

  • (true, false)


178
179
180
# File 'lib/mongo/util/uri_parser.rb', line 178

def direct?
  !replicaset?
end

#hostString

For direct connections, the host of the (only) node.

Returns:



184
185
186
# File 'lib/mongo/util/uri_parser.rb', line 184

def host
  nodes[0][0]
end

#node_stringsObject



245
246
247
# File 'lib/mongo/util/uri_parser.rb', line 245

def node_strings
  nodes.map { |node| node.join(':') }
end

#portInteger

For direct connections, the port of the (only) node.

Returns:

  • (Integer)


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.

Returns:

  • (true, false)


163
164
165
# File 'lib/mongo/util/uri_parser.rb', line 163

def replicaset?
  replicaset.is_a?(String) || nodes.length > 1
end