Class: Mongo::URI
- Inherits:
-
Object
- Object
- Mongo::URI
- Includes:
- Address::Validator, Loggable
- Defined in:
- lib/mongo/uri.rb,
lib/mongo/uri/srv_protocol.rb,
lib/mongo/uri/options_mapper.rb
Overview
The URI class provides a way for users to parse the MongoDB uri as defined in the connection string format spec.
Direct Known Subclasses
Defined Under Namespace
Classes: OptionsMapper, SRVProtocol
Constant Summary collapse
- SCHEME =
Deprecated.
Will be removed in 3.0.
The mongodb connection string scheme.
'mongodb://'.freeze
- MONGODB_SCHEME =
The mongodb connection string scheme root.
'mongodb'.freeze
- MONGODB_SRV_SCHEME =
The mongodb srv protocol connection string scheme root.
'mongodb+srv'.freeze
- INVALID_SCHEME =
Deprecated.
Error details for an invalid scheme.
"Invalid scheme. Scheme must be '#{MONGODB_SCHEME}' or '#{MONGODB_SRV_SCHEME}'".freeze
- FORMAT =
MongoDB URI format specification.
'mongodb://[username:password@]host1[:port1][,host2[:port2]' + ',...[,hostN[:portN]]][/[database][?options]]'.freeze
- HELP =
MongoDB URI (connection string) documentation url
'https://www.mongodb.com/docs/manual/reference/connection-string/'.freeze
- UNSAFE =
Unsafe characters that must be urlencoded.
/[\:\/\@]/
- PERCENT_CHAR =
Percent sign that must be encoded in user creds.
/\%/
- UNIX_SOCKET =
Unix socket suffix.
/.sock/
- HOST_DELIM =
The character delimiting hosts.
','.freeze
- HOST_PORT_DELIM =
The character separating a host and port.
':'.freeze
- DATABASE_DELIM =
The character delimiting a database.
'/'.freeze
- URI_OPTS_DELIM =
The character delimiting options.
'?'.freeze
- INDIV_URI_OPTS_DELIM =
Deprecated.
The character delimiting multiple options.
'&'.freeze
- URI_OPTS_VALUE_DELIM =
The character delimiting an option and its value.
'='.freeze
- AUTH_USER_PWD_DELIM =
The character separating a username from the password.
':'.freeze
- AUTH_DELIM =
The character delimiting auth credentials.
'@'.freeze
- SCHEME_DELIM =
Scheme delimiter.
'://'.freeze
- INVALID_OPTS_VALUE_DELIM =
Error details for an invalid options format.
"Options and their values must be delimited" + " by '#{URI_OPTS_VALUE_DELIM}'".freeze
- UNESCAPED_USER_PWD =
Error details for an non-urlencoded user name or password.
"User name and password must be urlencoded.".freeze
- UNESCAPED_UNIX_SOCKET =
Error details for a non-urlencoded unix socket path.
"UNIX domain sockets must be urlencoded.".freeze
- UNESCAPED_DATABASE =
Error details for a non-urlencoded auth database name.
"Auth database must be urlencoded.".freeze
- INVALID_OPTS_DELIM =
Error details for providing options without a database delimiter.
"Database delimiter '#{DATABASE_DELIM}' must be present if options are specified.".freeze
- INVALID_HOST =
Error details for a missing host.
"Missing host; at least one must be provided.".freeze
- INVALID_PORT =
Error details for an invalid port.
"Invalid port. Port must be an integer greater than 0 and less than 65536".freeze
- READ_MODE_MAP =
Map of URI read preference modes to Ruby driver read preference modes
{ 'primary' => :primary, 'primarypreferred' => :primary_preferred, 'secondary' => :secondary, 'secondarypreferred' => :secondary_preferred, 'nearest' => :nearest }.freeze
- AUTH_MECH_MAP =
Map of URI authentication mechanisms to Ruby driver mechanisms
{ 'GSSAPI' => :gssapi, 'MONGODB-AWS' => :aws, # MONGODB-CR is deprecated and will be removed in driver version 3.0 'MONGODB-CR' => :mongodb_cr, 'MONGODB-X509' => :mongodb_x509, 'PLAIN' => :plain, 'SCRAM-SHA-1' => :scram, 'SCRAM-SHA-256' => :scram256, }.freeze
- REPEATABLE_OPTIONS =
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.
[ :tag_sets, :ssl ]
Constants included from Loggable
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
The uri parser object options.
-
#servers ⇒ Object
readonly
The servers specified in the uri.
-
#uri_options ⇒ Object
readonly
Mongo::Options::Redacted of the options specified in the uri.
Class Method Summary collapse
-
.get(string, opts = {}) ⇒ URI, URI::SRVProtocol
Get either a URI object or a SRVProtocol URI object.
Instance Method Summary collapse
-
#client_options ⇒ Mongo::Options::Redacted
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.
-
#credentials ⇒ Hash
Get the credentials provided in the URI.
-
#database ⇒ String
Get the database provided in the URI.
-
#initialize(string, options = {}) ⇒ URI
constructor
Create the new uri from the provided string.
- #srv_records ⇒ Object
-
#to_s ⇒ String
Get the uri as a string.
Methods included from Address::Validator
Methods included from Loggable
#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger
Constructor Details
#initialize(string, options = {}) ⇒ URI
Create the new uri from the provided string.
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/mongo/uri.rb', line 284 def initialize(string, = {}) unless string raise Error::InvalidURI.new(string, 'URI must be a string, not nil.') end if string.empty? raise Error::InvalidURI.new(string, 'Cannot parse an empty URI.') end @string = string @options = parsed_scheme, _, remaining = string.partition(SCHEME_DELIM) unless parsed_scheme == scheme raise_invalid_error!("Invalid scheme '#{parsed_scheme}'. Scheme must be '#{MONGODB_SCHEME}'. Use URI#get to parse SRV URIs.") end if remaining.empty? raise_invalid_error!('No hosts in the URI') end parse!(remaining) end |
Instance Attribute Details
#options ⇒ Object (readonly)
The uri parser object options.
39 40 41 |
# File 'lib/mongo/uri.rb', line 39 def @options end |
#servers ⇒ Object (readonly)
The servers specified in the uri.
49 50 51 |
# File 'lib/mongo/uri.rb', line 49 def servers @servers end |
#uri_options ⇒ Object (readonly)
Mongo::Options::Redacted of the options specified in the uri.
44 45 46 |
# File 'lib/mongo/uri.rb', line 44 def @uri_options end |
Class Method Details
.get(string, opts = {}) ⇒ URI, URI::SRVProtocol
Get either a URI object or a SRVProtocol URI object.
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/mongo/uri.rb', line 230 def self.get(string, opts = {}) unless string raise Error::InvalidURI.new(string, 'URI must be a string, not nil.') end if string.empty? raise Error::InvalidURI.new(string, 'Cannot parse an empty URI.') end scheme, _, _ = string.partition(SCHEME_DELIM) case scheme when MONGODB_SCHEME URI.new(string, opts) when MONGODB_SRV_SCHEME SRVProtocol.new(string, opts) else raise Error::InvalidURI.new(string, "Invalid scheme '#{scheme}'. Scheme must be '#{MONGODB_SCHEME}' or '#{MONGODB_SRV_SCHEME}'") end end |
Instance Method Details
#client_options ⇒ Mongo::Options::Redacted
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.
259 260 261 262 263 264 265 |
# File 'lib/mongo/uri.rb', line 259 def opts = .tap do |opts| opts[:database] = @database if @database end @user ? opts.merge(credentials) : opts end |
#credentials ⇒ Hash
Get the credentials provided in the URI.
315 316 317 |
# File 'lib/mongo/uri.rb', line 315 def credentials { :user => @user, :password => @password } end |
#database ⇒ String
Get the database provided in the URI.
327 328 329 |
# File 'lib/mongo/uri.rb', line 327 def database @database ? @database : Database::ADMIN end |
#srv_records ⇒ Object
267 268 269 |
# File 'lib/mongo/uri.rb', line 267 def srv_records nil end |
#to_s ⇒ String
Get the uri as a string.
337 338 339 |
# File 'lib/mongo/uri.rb', line 337 def to_s reconstruct_uri end |