Class: Mongo::URIParser

Inherits:
Object show all
Defined in:
lib/mongo/util/uri_parser.rb

Constant Summary collapse

DEFAULT_PORT =
27017
MONGODB_URI_MATCHER =
/(([-_.\w\d]+):([^@]+)@)?([-.\w\d]+)(:([\w\d]+))?(\/([-\d\w]+))?/
MONGODB_URI_SPEC =
"mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/database]"
SPEC_ATTRS =
[:nodes, :auths]
OPT_ATTRS =
[:connect, :replicaset, :slaveok, :safe, :w, :wtimeout, :fsync]
OPT_VALID =
{:connect    => lambda {|arg| ['direct', 'replicaset'].include?(arg)},
 :replicaset => lambda {|arg| arg.length > 0},
 :slaveok    => lambda {|arg| ['true', 'false'].include?(arg)},
 :safe       => lambda {|arg| ['true', 'false'].include?(arg)},
 :w          => lambda {|arg| arg =~ /^\d+$/ },
 :wtimeout   => lambda {|arg| arg =~ /^\d+$/ },
 :fsync      => lambda {|arg| ['true', 'false'].include?(arg)}
}
OPT_ERR =
{:connect    => "must be 'direct' or 'replicaset'",
 :replicaset => "must be a string containing the name of the replica set to connect to",
 :slaveok    => "must be 'true' or 'false'",
 :safe       => "must be 'true' or 'false'",
 :w          => "must be an integer specifying number of nodes to replica to",
 :wtimeout   => "must be an integer specifying milliseconds",
 :fsync      => "must be 'true' or 'false'"
}
OPT_CONV =
{:connect    => lambda {|arg| arg},
 :replicaset => lambda {|arg| arg},
 :slaveok    => lambda {|arg| arg == 'true' ? true : false},
 :safe       => lambda {|arg| arg == 'true' ? true : false},
 :w          => lambda {|arg| arg.to_i},
 :wtimeout   => lambda {|arg| arg.to_i},
 :fsync      => lambda {|arg| arg == 'true' ? true : false}
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ URIParser

Parse a MongoDB URI. This method is used by Connection.from_uri. Returns an array of nodes and an array of db authorizations, if applicable.

Note: passwords can contain any character except for a ‘,’.



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mongo/util/uri_parser.rb', line 63

def initialize(string)
  if string =~ /^mongodb:\/\//
    string = string[10..-1]
  else
    raise MongoArgumentError, "MongoDB URI must match this spec: #{MONGODB_URI_SPEC}"
  end

  hosts, opts = string.split('?')
  parse_hosts(hosts)
  parse_options(opts)
  configure_connect
end

Instance Attribute Details

#authsObject (readonly)

Returns the value of attribute auths.



55
56
57
# File 'lib/mongo/util/uri_parser.rb', line 55

def auths
  @auths
end

#connectObject (readonly)

Returns the value of attribute connect.



55
56
57
# File 'lib/mongo/util/uri_parser.rb', line 55

def connect
  @connect
end

#fsyncObject (readonly)

Returns the value of attribute fsync.



55
56
57
# File 'lib/mongo/util/uri_parser.rb', line 55

def fsync
  @fsync
end

#nodesObject (readonly)

Returns the value of attribute nodes.



55
56
57
# File 'lib/mongo/util/uri_parser.rb', line 55

def nodes
  @nodes
end

#replicasetObject (readonly)

Returns the value of attribute replicaset.



55
56
57
# File 'lib/mongo/util/uri_parser.rb', line 55

def replicaset
  @replicaset
end

#safeObject (readonly)

Returns the value of attribute safe.



55
56
57
# File 'lib/mongo/util/uri_parser.rb', line 55

def safe
  @safe
end

#slaveokObject (readonly)

Returns the value of attribute slaveok.



55
56
57
# File 'lib/mongo/util/uri_parser.rb', line 55

def slaveok
  @slaveok
end

#wObject (readonly)

Returns the value of attribute w.



55
56
57
# File 'lib/mongo/util/uri_parser.rb', line 55

def w
  @w
end

#wtimeoutObject (readonly)

Returns the value of attribute wtimeout.



55
56
57
# File 'lib/mongo/util/uri_parser.rb', line 55

def wtimeout
  @wtimeout
end

Instance Method Details

#connection_optionsObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/mongo/util/uri_parser.rb', line 76

def connection_options
  opts = {}

  if (@w || @wtimeout || @fsync) && !@safe
    raise MongoArgumentError, "Safe must be true if w, wtimeout, or fsync is specified"
  end

  if @safe
    if @w || @wtimeout || @fsync
      safe_opts = {}
      safe_opts[:w] = @w if @w
      safe_opts[:wtimeout] = @wtimeout if @wtimeout
      safe_opts[:fsync] = @fsync if @fsync
    else
      safe_opts = true
    end

    opts[:safe] = safe_opts
  end

  if @slaveok
    if @connect == 'direct'
      opts[:slave_ok] = true
    else
      opts[:read_secondary] = true
    end
  end

  opts[:rs_name] = @replicaset if @replicaset

  opts
end