Class Mongo::URI
In: lib/mongo/uri/srv_protocol.rb
lib/mongo/uri.rb
Parent: Object

The URI class provides a way for users to parse the MongoDB uri as defined in the connection string format spec.

docs.mongodb.org/manual/reference/connection-string/

@example Use the uri string to make a client connection.

  uri = Mongo::URI.new('mongodb://localhost:27017')
  client = Mongo::Client.new(uri.servers, uri.options)
  client.login(uri.credentials)
  client[uri.database]

@since 2.0.0

Methods

client_options   credentials   database   get   new  

Included Modules

Loggable

Classes and Modules

Class Mongo::URI::SRVProtocol

Constants

SCHEME = 'mongodb://'.freeze   The mongodb connection string scheme.

@deprecated Will be removed in 3.0.

@since 2.0.0

MONGODB_SCHEME = 'mongodb'.freeze   The mongodb connection string scheme root.

@since 2.5.0

MONGODB_SRV_SCHEME = 'mongodb+srv'.freeze   The mongodb srv protocol connection string scheme root.

@since 2.5.0

INVALID_SCHEME = "Invalid scheme. Scheme must be '#{MONGODB_SCHEME}' or '#{MONGODB_SRV_SCHEME}'".freeze   Error details for an invalid scheme.

@since 2.1.0

FORMAT = 'mongodb://[username:password@]host1[:port1][,host2[:port2]' + ',...[,hostN[:portN]]][/[database][?options]]'.freeze   MongoDB URI format specification.

@since 2.0.0

HELP = 'http://docs.mongodb.org/manual/reference/connection-string/'.freeze   MongoDB URI (connection string) documentation url

@since 2.0.0

UNSAFE = /[\:\/\+\@]/   Unsafe characters that must be urlencoded.

@since 2.1.0

PERCENT_CHAR = /\%/   Percent sign that must be encoded in user creds.

@since 2.5.1

UNIX_SOCKET = /.sock/   Unix socket suffix.

@since 2.1.0

HOST_DELIM = ','.freeze   The character delimiting hosts.

@since 2.1.0

HOST_PORT_DELIM = ':'.freeze   The character separating a host and port.

@since 2.1.0

DATABASE_DELIM = '/'.freeze   The character delimiting a database.

@since 2.1.0

URI_OPTS_DELIM = '?'.freeze   The character delimiting options.

@since 2.1.0

INDIV_URI_OPTS_DELIM = '&'.freeze   The character delimiting multiple options.

@since 2.1.0

URI_OPTS_VALUE_DELIM = '='.freeze   The character delimiting an option and its value.

@since 2.1.0

AUTH_USER_PWD_DELIM = ':'.freeze   The character separating a username from the password.

@since 2.1.0

AUTH_DELIM = '@'.freeze   The character delimiting auth credentials.

@since 2.1.0

SCHEME_DELIM = '://'.freeze   Scheme delimiter.

@since 2.5.0

INVALID_OPTS_VALUE_DELIM = "Options and their values must be delimited" + " by '#{URI_OPTS_VALUE_DELIM}'".freeze   Error details for an invalid options format.

@since 2.1.0

UNESCAPED_USER_PWD = "User name and password must be urlencoded.".freeze   Error details for an non-urlencoded user name or password.

@since 2.1.0

UNESCAPED_UNIX_SOCKET = "UNIX domain sockets must be urlencoded.".freeze   Error details for a non-urlencoded unix socket path.

@since 2.1.0

UNESCAPED_DATABASE = "Auth database must be urlencoded.".freeze   Error details for a non-urlencoded auth database name.

@since 2.1.0

INVALID_OPTS_DELIM = "Database delimiter '#{DATABASE_DELIM}' must be present if options are specified.".freeze   Error details for providing options without a database delimiter.

@since 2.1.0

INVALID_HOST = "Missing host; at least one must be provided.".freeze   Error details for a missing host.

@since 2.1.0

INVALID_PORT = "Invalid port. Port must be an integer greater than 0 and less than 65536".freeze   Error details for an invalid port.

@since 2.1.0

READ_MODE_MAP = { 'primary' => :primary, 'primarypreferred' => :primary_preferred, 'secondary' => :secondary, 'secondarypreferred' => :secondary_preferred, 'nearest' => :nearest   Map of URI read preference modes to Ruby driver read preference modes

@since 2.0.0

AUTH_MECH_MAP = { 'PLAIN' => :plain, # MONGODB-CR is deprecated and will be removed in driver version 3.0 'MONGODB-CR' => :mongodb_cr, 'GSSAPI' => :gssapi, 'MONGODB-X509' => :mongodb_x509, 'SCRAM-SHA-1' => :scram, 'SCRAM-SHA-256' => :scram256   Map of URI authentication mechanisms to Ruby driver mechanisms

@since 2.0.0

REPEATABLE_OPTIONS = [ :tag_sets, :ssl ]   Options that are allowed to appear more than once in the uri.

In order to follow the URI options spec requirement that all instances of ‘tls’ and ‘ssl’ have the same value, we need to keep track of all of the values passed in for those options. Assuming they don‘t conflict, they will be condensed to a single value immediately after parsing the URI.

@since 2.1.0

URI_OPTION_MAP = {}   Hash for storing map of URI option parameters to conversion strategies

Attributes

options  [R]  The uri parser object options.

@since 2.0.0

servers  [R]  The servers specified in the uri.

@since 2.0.0

uri_options  [R]  The options specified in the uri.

@since 2.1.0

Public Class methods

Get either a URI object or a SRVProtocol URI object.

@example Get the uri object.

  URI.get(string)

@return [URI, URI::SRVProtocol] The uri object.

@since 2.5.0

Create the new uri from the provided string.

@example Create the new URI.

  URI.new('mongodb://localhost:27017')

@param [ String ] string The uri string. @param [ Hash ] options The options.

@raise [ Error::InvalidURI ] If the uri does not match the spec.

@since 2.0.0

Public Instance methods

Gets the options hash that needs to be passed to a Mongo::Client on instantiation, so we don‘t have to merge the credentials and database in at that point - we only have a single point here.

@example Get the client options.

  uri.client_options

@return [ Hash ] The options passed to the Mongo::Client

@since 2.0.0

Get the credentials provided in the URI.

@example Get the credentials.

  uri.credentials

@return [ Hash ] The credentials.

  * :user [ String ] The user.
  * :password [ String ] The provided password.

@since 2.0.0

Get the database provided in the URI.

@example Get the database.

  uri.database

@return [String] The database.

@since 2.0.0

[Validate]