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
# File 'lib/moped/mongo_uri.rb', line 64

def initialize(string)
  @match = string.match(URI)
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

#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



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

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



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 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



105
106
107
# File 'lib/moped/mongo_uri.rb', line 105

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



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

#usernameString

Get the username provided in the URI.

Examples:

Get the username.

uri.username

Returns:

Since:

  • 1.3.0



145
146
147
# File 'lib/moped/mongo_uri.rb', line 145

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