Class: Moped::MongoUri

Inherits:
Object show all
Defined in:
lib/moped/mongo_uri.rb

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

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ MongoUri

Create the new uri from the provided string.

Examples:

Create the new uri.

MongoUri.new(uri)

Parameters:

  • string (String)

    The uri string.

Since:

  • 1.3.0



64
65
66
67
# File 'lib/moped/mongo_uri.rb', line 64

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

Instance Attribute Details

#matchObject (readonly)



18
19
20
# File 'lib/moped/mongo_uri.rb', line 18

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



28
29
30
# File 'lib/moped/mongo_uri.rb', line 28

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

#databaseString

Get the database provided in the URI.

Examples:

Get the database.

uri.database

Returns:

Since:

  • 1.3.0



40
41
42
# File 'lib/moped/mongo_uri.rb', line 40

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

#hostsArray<String>

Get the hosts provided in the URI.

Examples:

Get the hosts.

uri.hosts

Returns:

Since:

  • 1.3.0



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

Examples:

Raise error and provide guidance on invalid URI

MongoUri.invalid!(uri)

Parameters:

Raises:

Since:

  • 1.3.1



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_argumentsArray

Create Moped usable arguments

Examples:

Get the moped args

uri.moped_arguments

Returns:

  • (Array)

    Array of arguments usable by Moped

Since:

  • 1.3.0



162
163
164
# File 'lib/moped/mongo_uri.rb', line 162

def moped_arguments
  [hosts, options]
end

#optionsHash

Get the options provided in the URI.

Examples:

Get the options

uri.options

Returns:

  • (Hash)

    Options hash usable by Moped

Since:

  • 1.3.0



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 options
  options_string, options = @match[10], {database: database}

  unless options_string.nil?
    options_string.split(/\&/).each do |option_string|
      key, value = option_string.split(/=/)

      if value == "true"
        options[key.to_sym] = true
      elsif value == "false"
        options[key.to_sym] = false
      elsif value =~ /[\d]/
        options[key.to_sym] = value.to_i
      else
        options[key.to_sym] = value.to_sym
      end
    end
  end

  options
end

#passwordString

Get the password provided in the URI.

Examples:

Get the password.

uri.password

Returns:

Since:

  • 1.3.0



134
135
136
# File 'lib/moped/mongo_uri.rb', line 134

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

#to_hashHash

Get the uri as a Mongoid friendly configuration hash.

Examples:

Get the uri as a hash.

uri.to_hash

Returns:

  • (Hash)

    The uri as options.

Since:

  • 1.3.0



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

#usernameString

Get the username provided in the URI.

Examples:

Get the username.

uri.username

Returns:

Since:

  • 1.3.0



174
175
176
# File 'lib/moped/mongo_uri.rb', line 174

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