Class: Moped::MongoUri
Overview
Parses MongoDB uri
Constant Summary collapse
- SCHEME =
/(mongodb:\/\/)/
- USER =
/([-.\w:]+)/
- PASS =
/([^@,]+)/
- NODES =
/((([-.\w]+)(?::(\w+))?,?)+)/
- DATABASE =
/(?:\/([-\w]+))?/
- OPTIONS =
/(?:\?(.+))/
- URI =
/#{SCHEME}(#{USER}:#{PASS}@)?#{NODES}#{DATABASE}#{OPTIONS}?/
Instance Attribute Summary collapse
- #match ⇒ Object readonly
Instance Method Summary collapse
-
#auth_provided? ⇒ true, false
Helper to determine if authentication is provided.
-
#database ⇒ String
Get the database provided in the URI.
-
#hosts ⇒ Array<String>
Get the hosts provided in the URI.
-
#initialize(string) ⇒ MongoUri
constructor
Create the new uri from the provided string.
-
#invalid_uri!(string) ⇒ Object
Raise a human readable error when improper URI provided.
-
#moped_arguments ⇒ Array
Create Moped usable arguments.
-
#options ⇒ Hash
Get the options provided in the URI.
-
#password ⇒ String
Get the password provided in the URI.
-
#to_hash ⇒ Hash
Get the uri as a Mongoid friendly configuration hash.
-
#username ⇒ String
Get the username provided in the URI.
Constructor Details
Instance Attribute Details
Instance Method Details
#auth_provided? ⇒ true, false
Helper to determine if authentication is provided
28 29 30 |
# File 'lib/moped/mongo_uri.rb', line 28 def auth_provided? !username.nil? && !password.nil? end |
#database ⇒ String
Get the database provided in the URI.
40 41 42 |
# File 'lib/moped/mongo_uri.rb', line 40 def database @database ||= match[9] end |
#hosts ⇒ Array<String>
Get the hosts provided in the URI.
52 53 54 |
# File 'lib/moped/mongo_uri.rb', line 52 def hosts @hosts ||= match[5].split(",") end |
#invalid_uri!(string) ⇒ Object
Raise a human readable error when improper URI provided
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/moped/mongo_uri.rb', line 77 def invalid_uri!(string) msg = %{ The given connection string is invalid: #{string.gsub(/[^:]+@/, '<password>@')} MongoDB connection strings must be of the format: mongodb://host:port/database For authentication, include username and password before host: mongodb://username:password@host:port/database For Replica Sets, include multiple host:port entries: mongodb://host:port,host2:port2/database For options, use query string syntax with the option value: mongodb://host:port/database?safe=true&max_retries=30&timeout=5 } raise Errors::InvalidMongoURI, msg end |
#moped_arguments ⇒ Array
Create Moped usable arguments
162 163 164 |
# File 'lib/moped/mongo_uri.rb', line 162 def moped_arguments [hosts, ] end |
#options ⇒ Hash
Get the options provided in the URI.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/moped/mongo_uri.rb', line 104 def , = @match[10], {database: database} unless .nil? .split(/\&/).each do |option_string| key, value = option_string.split(/=/) if value == "true" [key.to_sym] = true elsif value == "false" [key.to_sym] = false elsif value =~ /[\d]/ [key.to_sym] = value.to_i else [key.to_sym] = value.to_sym end end end end |
#password ⇒ String
Get the password provided in the URI.
134 135 136 |
# File 'lib/moped/mongo_uri.rb', line 134 def password @password ||= match[4] end |
#to_hash ⇒ Hash
Get the uri as a Mongoid friendly configuration hash.
146 147 148 149 150 151 152 |
# File 'lib/moped/mongo_uri.rb', line 146 def to_hash config = { database: database, hosts: hosts } if username && password config.merge!(username: username, password: password) end config end |
#username ⇒ String
Get the username provided in the URI.
174 175 176 |
# File 'lib/moped/mongo_uri.rb', line 174 def username @username ||= match[3] end |