Class: Bones::RPC::Uri

Inherits:
Object
  • Object
show all
Defined in:
lib/bones/rpc/uri.rb

Overview

Parses Bones::RPC uri

Since:

  • 1.3.0

Constant Summary collapse

SCHEME =

Get the scheme pattern.

Since:

  • 1.3.0

/(bones-rpc:\/\/)/
USER =

The user name pattern.

Since:

  • 1.3.0

/([-.\w:]+)/
PASS =

The password pattern.

Since:

  • 1.3.0

/([^@,]+)/
NODES =

The nodes pattern.

Since:

  • 1.3.0

/((([-.\w]+)(?::(\w+))?,?)+)/
DATABASE =

The database pattern.

Since:

  • 1.3.0

/(?:\/([-\w]+))?/
OPTIONS =

The options pattern.

Since:

  • 1.3.0

/(?:\?(.+))/
URI =

The full URI pattern.

Since:

  • 1.3.0

/#{SCHEME}(#{USER}:#{PASS}@)?#{NODES}#{DATABASE}#{OPTIONS}?/
WRITE_OPTIONS =

The options that have to do with write concerns.

Since:

  • 2.0.0

[ "w", "j", "fsync", "wtimeout" ].freeze
READ_MAPPINGS =

The mappings from read preferences in the URI to Bones::RPC’s.

Since:

  • 2.0.0

{
  "nearest" => :nearest,
  "primary" => :primary,
  "primarypreferred" => :primary_preferred,
  "secondary" => :secondary,
  "secondarypreferred" => :secondary_preferred
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ Uri

Create the new uri from the provided string.

Examples:

Create the new uri.

Bones::RPC::Uri.new(uri)

Parameters:

  • string (String)

    The uri string.

Since:

  • 1.3.0



109
110
111
112
# File 'lib/bones/rpc/uri.rb', line 109

def initialize(string)
  @match = string.match(URI)
  invalid_uri!(string) unless @match
end

Instance Attribute Details

#matchObject

Since:

  • 1.3.0



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

Examples:

Boolean response if username/password given

uri.auth_provided?

Returns:

  • (true, false)

    If authorization is provided.

Since:

  • 1.3.0



73
74
75
# File 'lib/bones/rpc/uri.rb', line 73

def auth_provided?
  !username.nil? && !password.nil?
end

#bones_rpc_argumentsArray

Create Bones::RPC usable arguments

Examples:

Get the bones_rpc args

uri.bones_rpc_arguments

Returns:

  • (Array)

    Array of arguments usable by bones_rpc

Since:

  • 1.3.0



191
192
193
# File 'lib/bones/rpc/uri.rb', line 191

def bones_rpc_arguments
  [ hosts, options ]
end

#databaseString

Get the database provided in the URI.

Examples:

Get the database.

uri.database

Returns:

  • (String)

    The database.

Since:

  • 1.3.0



85
86
87
# File 'lib/bones/rpc/uri.rb', line 85

def database
  @database ||= match[9]
end

#hostsArray<String>

Get the hosts provided in the URI.

Examples:

Get the hosts.

uri.hosts

Returns:

  • (Array<String>)

    The hosts.

Since:

  • 1.3.0



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

Examples:

Raise error and provide guidance on invalid URI

Bones::RPC::Uri.invalid!(uri)

Parameters:

  • Invalid (String)

    string

Raises:

Since:

  • 1.3.1



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

#optionsHash

Note:

The options provided in the URI string must match the Bones::RPC specification.

Get the options provided in the URI.

Examples:

Get the options

uri.options

Returns:

  • (Hash)

    Options hash usable by Moped

Since:

  • 1.3.0



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 options
  options_string, options = match[10], {}
  unless options_string.nil?
    options_string.split(/\&/).each do |option_string|
      key, value = option_string.split(Regexp.new('='))
      if WRITE_OPTIONS.include?(key)
        options[:write] = { key.to_sym => cast(value) }
      elsif read = READ_MAPPINGS[value.downcase]
        options[:read] = read
      else
        options[key.to_sym] = cast(value)
      end
    end
  end
  options
end

#passwordString

Get the password provided in the URI.

Examples:

Get the password.

uri.password

Returns:

  • (String)

    The password.

Since:

  • 1.3.0



163
164
165
# File 'lib/bones/rpc/uri.rb', line 163

def password
  @password ||= match[4]
end

#to_hashHash

Get the uri as a Bones::RPC friendly configuration hash.

Examples:

Get the uri as a hash.

uri.to_hash

Returns:

  • (Hash)

    The uri as options.

Since:

  • 1.3.0



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

#usernameString

Get the username provided in the URI.

Examples:

Get the username.

uri.username

Returns:

  • (String)

    The username.

Since:

  • 1.3.0



203
204
205
# File 'lib/bones/rpc/uri.rb', line 203

def username
  @username ||= match[3]
end