Class: Mongo::URIParser
Constant Summary collapse
- DEFAULT_PORT =
27017
- MONGODB_URI_MATCHER =
/(([-.\w]+):([^@]+)@)?([-.\w]+)(:([\w]+))?(\/([-\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
-
#auths ⇒ Object
readonly
Returns the value of attribute auths.
-
#connect ⇒ Object
readonly
Returns the value of attribute connect.
-
#fsync ⇒ Object
readonly
Returns the value of attribute fsync.
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
-
#replicaset ⇒ Object
readonly
Returns the value of attribute replicaset.
-
#safe ⇒ Object
readonly
Returns the value of attribute safe.
-
#slaveok ⇒ Object
readonly
Returns the value of attribute slaveok.
-
#w ⇒ Object
readonly
Returns the value of attribute w.
-
#wtimeout ⇒ Object
readonly
Returns the value of attribute wtimeout.
Instance Method Summary collapse
- #connection_options ⇒ Object
-
#initialize(string) ⇒ URIParser
constructor
Parse a MongoDB URI.
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) (opts) configure_connect end |
Instance Attribute Details
#auths ⇒ Object (readonly)
Returns the value of attribute auths.
55 56 57 |
# File 'lib/mongo/util/uri_parser.rb', line 55 def auths @auths end |
#connect ⇒ Object (readonly)
Returns the value of attribute connect.
55 56 57 |
# File 'lib/mongo/util/uri_parser.rb', line 55 def connect @connect end |
#fsync ⇒ Object (readonly)
Returns the value of attribute fsync.
55 56 57 |
# File 'lib/mongo/util/uri_parser.rb', line 55 def fsync @fsync end |
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
55 56 57 |
# File 'lib/mongo/util/uri_parser.rb', line 55 def nodes @nodes end |
#replicaset ⇒ Object (readonly)
Returns the value of attribute replicaset.
55 56 57 |
# File 'lib/mongo/util/uri_parser.rb', line 55 def replicaset @replicaset end |
#safe ⇒ Object (readonly)
Returns the value of attribute safe.
55 56 57 |
# File 'lib/mongo/util/uri_parser.rb', line 55 def safe @safe end |
#slaveok ⇒ Object (readonly)
Returns the value of attribute slaveok.
55 56 57 |
# File 'lib/mongo/util/uri_parser.rb', line 55 def slaveok @slaveok end |
#w ⇒ Object (readonly)
Returns the value of attribute w.
55 56 57 |
# File 'lib/mongo/util/uri_parser.rb', line 55 def w @w end |
#wtimeout ⇒ Object (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_options ⇒ Object
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 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 |