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.
-
#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 |
#moped_arguments ⇒ Array
Create Moped usable arguments
133 134 135 |
# File 'lib/moped/mongo_uri.rb', line 133 def moped_arguments [hosts, ] end |
#options ⇒ Hash
Get the options provided in the URI.
75 76 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 75 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.
105 106 107 |
# File 'lib/moped/mongo_uri.rb', line 105 def password @password ||= match[4] end |
#to_hash ⇒ Hash
Get the uri as a Mongoid friendly configuration hash.
117 118 119 120 121 122 123 |
# File 'lib/moped/mongo_uri.rb', line 117 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.
145 146 147 |
# File 'lib/moped/mongo_uri.rb', line 145 def username @username ||= match[3] end |