Class: MongoHqConnector

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo_hq_connector.rb

Constant Summary collapse

VERSION =
'0.0.1'
@@database =

Connect to a MongoHQ database

Example:

>> MongoHqConnector.connection('foo')
=> <#Mongo::Connection ... >

Arguments:

default_collection: String
nil

Class Method Summary collapse

Class Method Details

.connection(default_collection) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mongo_hq_connector.rb', line 16

def self.connection(default_collection)
  return @@database if @@database
  
  if ENV['MONGOHQ_URL']
    url = URI.parse(ENV['MONGOHQ_URL'])
    conn = Mongo::Connection.new(url.host, url.port)
    @@database = conn.db(url.path.gsub(/^\//, ''))
    @@database.authenticate(url.user, url.password)
    
    @@database
  else
    @@database = Mongo::Connection.new.db(default_collection)
  end
end