Class: Bones::RPC::Uri
- Inherits:
-
Object
- Object
- Bones::RPC::Uri
- Defined in:
- lib/bones/rpc/uri.rb
Overview
Parses Bones::RPC uri
Constant Summary collapse
- SCHEME =
Get the scheme pattern.
/(bones-rpc:\/\/)/
- 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 Bones::RPC’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.
-
#bones_rpc_arguments ⇒ Array
Create Bones::RPC usable arguments.
-
#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.
-
#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 Bones::RPC friendly configuration hash.
-
#username ⇒ String
Get the username provided in the URI.
Constructor Details
Instance Attribute Details
#match ⇒ Object
63 64 65 |
# File 'lib/bones/rpc/uri.rb', line 63 def match @match end |
Instance Method Details
#auth_provided? ⇒ true, false
Helper to determine if authentication is provided
73 74 75 |
# File 'lib/bones/rpc/uri.rb', line 73 def auth_provided? !username.nil? && !password.nil? end |
#bones_rpc_arguments ⇒ Array
Create Bones::RPC usable arguments
191 192 193 |
# File 'lib/bones/rpc/uri.rb', line 191 def bones_rpc_arguments [ hosts, ] end |
#database ⇒ String
Get the database provided in the URI.
85 86 87 |
# File 'lib/bones/rpc/uri.rb', line 85 def database @database ||= match[9] end |
#hosts ⇒ Array<String>
Get the hosts provided in the URI.
97 98 99 |
# File 'lib/bones/rpc/uri.rb', line 97 def hosts @hosts ||= match[5].split(",") end |
#invalid_uri!(string) ⇒ Object
Raise a human readable error when improper URI provided
122 123 124 125 |
# File 'lib/bones/rpc/uri.rb', line 122 def invalid_uri!(string) scrubbed = string.gsub(/[^:]+@/, '<password>@') raise Errors::InvalidBonesRPCURI, "The provided connection string is not a value URI: #{scrubbed}" end |
#options ⇒ Hash
The options provided in the URI string must match the Bones::RPC specification.
Get the options provided in the URI.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/bones/rpc/uri.rb', line 138 def , = match[10], {} unless .nil? .split(/\&/).each do |option_string| key, value = option_string.split(Regexp.new('=')) 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.
163 164 165 |
# File 'lib/bones/rpc/uri.rb', line 163 def password @password ||= match[4] end |
#to_hash ⇒ Hash
Get the uri as a Bones::RPC friendly configuration hash.
175 176 177 178 179 180 181 |
# File 'lib/bones/rpc/uri.rb', line 175 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.
203 204 205 |
# File 'lib/bones/rpc/uri.rb', line 203 def username @username ||= match[3] end |