@@ -32,6 +32,7 @@ class Connection
32
32
33
33
Thread . abort_on_exception = true
34
34
35
+ DEFAULT_HOST = 'localhost'
35
36
DEFAULT_PORT = 27017
36
37
GENERIC_OPTS = [ :ssl , :auths , :pool_size , :pool_timeout , :timeout , :op_timeout , :connect_timeout , :safe , :logger , :connect ]
37
38
CONNECTION_OPTS = [ :slave_ok ]
@@ -44,6 +45,8 @@ class Connection
44
45
45
46
# Create a connection to single MongoDB instance.
46
47
#
48
+ # If no args are provided, it will check <code>ENV["MONGODB_URI"]</code>.
49
+ #
47
50
# You may specify whether connection to slave is permitted.
48
51
# In all cases, the default host is "localhost" and the default port is 27017.
49
52
#
@@ -76,7 +79,7 @@ class Connection
76
79
# connection attempt.
77
80
# @option opts [Boolean] :ssl (false) If true, create the connection to the server using SSL.
78
81
#
79
- # @example localhost, 27017
82
+ # @example localhost, 27017 (or <code>ENV["MONGODB_URI"]</code> if available)
80
83
# Mongo::Connection.new
81
84
#
82
85
# @example localhost, 27017
@@ -93,9 +96,22 @@ class Connection
93
96
# @raise [ReplicaSetConnectionError] This is raised if a replica set name is specified and the
94
97
# driver fails to connect to a replica set with that name.
95
98
#
99
+ # @raise [MongoArgumentError] If called with no arguments and <code>ENV["MONGODB_URI"]</code> implies a replica set.
100
+ #
96
101
# @core self.connections
97
102
def initialize ( host = nil , port = nil , opts = { } )
98
- @host_to_try = format_pair ( host , port )
103
+ if host . nil? and ENV . has_key? ( 'MONGODB_URI' )
104
+ parser = URIParser . new ENV [ 'MONGODB_URI' ] , opts
105
+ if parser . replicaset?
106
+ raise MongoArgumentError , "Mongo::Connection.new called with no arguments, but ENV['MONGODB_URI'] implies a replica set."
107
+ end
108
+ opts = parser . connection_options
109
+ @host_to_try = [ parser . host , parser . port ]
110
+ elsif host . is_a? ( String )
111
+ @host_to_try = [ host , ( port || DEFAULT_PORT ) . to_i ]
112
+ else
113
+ @host_to_try = [ DEFAULT_HOST , DEFAULT_PORT ]
114
+ end
99
115
100
116
# Host and port of current master.
101
117
@host = @port = nil
@@ -143,8 +159,7 @@ def initialize(host=nil, port=nil, opts={})
143
159
def self . multi ( nodes , opts = { } )
144
160
warn "Connection.multi is now deprecated and will be removed in v2.0. Please use ReplSetConnection.new instead."
145
161
146
- nodes << opts
147
- ReplSetConnection . new ( *nodes )
162
+ ReplSetConnection . new ( *( nodes +[ opts ] ) )
148
163
end
149
164
150
165
# Initialize a connection to MongoDB using the MongoDB URI spec:
@@ -155,21 +170,9 @@ def self.multi(nodes, opts={})
155
170
# @param opts Any of the options available for Connection.new
156
171
#
157
172
# @return [Mongo::Connection, Mongo::ReplSetConnection]
158
- def self . from_uri ( string , extra_opts = { } )
159
- uri = URIParser . new ( string )
160
- opts = uri . connection_options
161
- opts . merge! ( extra_opts )
162
-
163
- if uri . nodes . length == 1
164
- opts . merge! ( { :auths => uri . auths } )
165
- Connection . new ( uri . nodes [ 0 ] [ 0 ] , uri . nodes [ 0 ] [ 1 ] , opts )
166
- elsif uri . nodes . length > 1
167
- nodes = uri . nodes . clone
168
- nodes_with_opts = nodes << opts
169
- ReplSetConnection . new ( *nodes_with_opts )
170
- else
171
- raise MongoArgumentError , "No nodes specified. Please ensure that you've provided at least one node."
172
- end
173
+ def self . from_uri ( uri , extra_opts = { } )
174
+ parser = URIParser . new uri , extra_opts
175
+ parser . connection
173
176
end
174
177
175
178
# The host name used for this connection.
@@ -337,7 +340,7 @@ def drop_database(name)
337
340
# @param [String] from_host host of the 'from' database.
338
341
# @param [String] username username for authentication against from_db (>=1.3.x).
339
342
# @param [String] password password for authentication against from_db (>=1.3.x).
340
- def copy_database ( from , to , from_host = "localhost" , username = nil , password = nil )
343
+ def copy_database ( from , to , from_host = DEFAULT_HOST , username = nil , password = nil )
341
344
oh = BSON ::OrderedHash . new
342
345
oh [ :copydb ] = 1
343
346
oh [ :fromhost ] = from_host
@@ -585,23 +588,8 @@ def setup(opts)
585
588
write_logging_startup_message
586
589
end
587
590
588
- should_connect = opts . fetch ( :connect , true )
589
- connect if should_connect
590
- end
591
-
592
- ## Configuration helper methods
593
-
594
- # Returns a host-port pair.
595
- #
596
- # @return [Array]
597
- #
598
- # @private
599
- def format_pair ( host , port )
600
- case host
601
- when String
602
- [ host , port ? port . to_i : DEFAULT_PORT ]
603
- when nil
604
- [ 'localhost' , DEFAULT_PORT ]
591
+ if opts . fetch ( :connect , true )
592
+ connect
605
593
end
606
594
end
607
595
0 commit comments