Class: Moped::Uri
- Inherits:
-
Object
- Object
- Moped::Uri
- Defined in:
- lib/moped/uri.rb
Overview
Parses MongoDB uri
Constant Summary collapse
- SCHEME =
Get the scheme pattern.
/(mongodb:\/\/)/
- USER =
The user name pattern.
/([-.\w:]+)/
- PASS =
The password pattern.
/([^@,]+)/
- NODES =
The nodes pattern.
/((([-.\w]+)(?::(\w+))?,?)+)/
- DATABASE =
The database pattern.
/(?:\/([-\w]+))?/
- OPTIONS =
The options pattern.
/(?:\?(.+))/
- URI =
The full URI pattern.
/#{SCHEME}(#{USER}:#{PASS}@)?#{NODES}#{DATABASE}#{OPTIONS}?/
- WRITE_OPTIONS =
The options that have to do with write concerns.
[ "w", "j", "fsync", "wtimeout" ].freeze
- READ_MAPPINGS =
The mappings from read preferences in the URI to Moped’s.
{ "nearest" => :nearest, "primary" => :primary, "primarypreferred" => :primary_preferred, "secondary" => :secondary, "secondarypreferred" => :secondary_preferred }.freeze
Instance Attribute Summary collapse
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) ⇒ Uri
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
#match ⇒ Object
62 63 64 |
# File 'lib/moped/uri.rb', line 62 def match @match end |
Instance Method Details
#auth_provided? ⇒ true, false
Helper to determine if authentication is provided
72 73 74 |
# File 'lib/moped/uri.rb', line 72 def auth_provided? !username.nil? && !password.nil? end |
#database ⇒ String
Get the database provided in the URI.
84 85 86 |
# File 'lib/moped/uri.rb', line 84 def database @database ||= match[9] end |
#hosts ⇒ Array<String>
Get the hosts provided in the URI.
96 97 98 |
# File 'lib/moped/uri.rb', line 96 def hosts @hosts ||= match[5].split(",") end |
#invalid_uri!(string) ⇒ Object
Raise a human readable error when improper URI provided
121 122 123 124 |
# File 'lib/moped/uri.rb', line 121 def invalid_uri!(string) scrubbed = string.gsub(/[^:]+@/, '<password>@') raise Errors::InvalidMongoURI, "The provided connection string is not a value URI: #{scrubbed}" end |
#moped_arguments ⇒ Array
Create Moped usable arguments
192 193 194 |
# File 'lib/moped/uri.rb', line 192 def moped_arguments [ hosts, ] end |
#options ⇒ Hash
The options provided in the URI string must match the MongoDB specification.
Get the options provided in the URI.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/moped/uri.rb', line 139 def , = match[10], { database: database } unless .nil? .split(/\&/).each do |option_string| key, value = option_string.split(/=/) if WRITE_OPTIONS.include?(key) [:write] = { key.to_sym => cast(value) } elsif read = READ_MAPPINGS[value.downcase] [:read] = read else [key.to_sym] = cast(value) end end end end |
#password ⇒ String
Get the password provided in the URI.
164 165 166 |
# File 'lib/moped/uri.rb', line 164 def password @password ||= match[4] end |
#to_hash ⇒ Hash
Get the uri as a Mongoid friendly configuration hash.
176 177 178 179 180 181 182 |
# File 'lib/moped/uri.rb', line 176 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.
204 205 206 |
# File 'lib/moped/uri.rb', line 204 def username @username ||= match[3] end |