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,
  :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

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.



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

Instance Attribute Details

#authsObject (readonly)

Returns the value of attribute auths.



92
93
94
# File 'lib/mongo/util/uri_parser.rb', line 92

def auths
  @auths
end

#connectObject (readonly)

Returns the value of attribute connect.



92
93
94
# File 'lib/mongo/util/uri_parser.rb', line 92

def connect
  @connect
end

#connecttimeoutmsObject (readonly)

Returns the value of attribute connecttimeoutms.



92
93
94
# File 'lib/mongo/util/uri_parser.rb', line 92

def connecttimeoutms
  @connecttimeoutms
end

#fsyncObject (readonly)

Returns the value of attribute fsync.



92
93
94
# File 'lib/mongo/util/uri_parser.rb', line 92

def fsync
  @fsync
end

#journalObject (readonly)

Returns the value of attribute journal.



92
93
94
# File 'lib/mongo/util/uri_parser.rb', line 92

def journal
  @journal
end

#replicasetObject (readonly)

Returns the value of attribute replicaset.



92
93
94
# File 'lib/mongo/util/uri_parser.rb', line 92

def replicaset
  @replicaset
end

#safeObject (readonly)

Returns the value of attribute safe.



92
93
94
# File 'lib/mongo/util/uri_parser.rb', line 92

def safe
  @safe
end

#slaveokObject (readonly)

Returns the value of attribute slaveok.



92
93
94
# File 'lib/mongo/util/uri_parser.rb', line 92

def slaveok
  @slaveok
end

#sockettimeoutmsObject (readonly)

Returns the value of attribute sockettimeoutms.



92
93
94
# File 'lib/mongo/util/uri_parser.rb', line 92

def sockettimeoutms
  @sockettimeoutms
end

#wObject (readonly)

Returns the value of attribute w.



92
93
94
# File 'lib/mongo/util/uri_parser.rb', line 92

def w
  @w
end

#wtimeoutObject (readonly)

Returns the value of attribute wtimeout.



92
93
94
# File 'lib/mongo/util/uri_parser.rb', line 92

def wtimeout
  @wtimeout
end

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

Returns:

  • (true, false)


157
158
159
# File 'lib/mongo/util/uri_parser.rb', line 157

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.



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

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

Returns:



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

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)


166
167
168
# File 'lib/mongo/util/uri_parser.rb', line 166

def direct?
  !replicaset?
end

#hostString

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

Returns:



172
173
174
# File 'lib/mongo/util/uri_parser.rb', line 172

def host
  nodes[0][0]
end

#nodesObject



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

#portInteger

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

Returns:

  • (Integer)


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.

Returns:

  • (true, false)


151
152
153
# File 'lib/mongo/util/uri_parser.rb', line 151

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