Class: BarnyardHarvester::MongoDbHelper

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

Class Method Summary collapse

Class Method Details

.connect(args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/barnyard_harvester/mongodb_helper.rb', line 7

def self.connect(args)

  @debug = args.fetch(:debug) { false }
  @log = args.fetch(:logger) { Logger.new(STDOUT) }

  @log.debug "Connection parameters #{args[:host]} db: #{args[:db]} collection: #{args[:collection]}" if @debug

  # Connect to Mongo
  if args.has_key? :host
    if args[:host].is_a? Array
      @log.debug "Connecting to replica set #{args[:host]}"
      @mongo = Mongo::ReplSetConnection.new(args[:host]).db(args[:db])
    else
      @log.debug "Connecting to single host #{args[:host]}"
      @mongo = Mongo::Connection.new(args[:host].split(":")[0], args[:host].split(":")[1]).db(args[:db])
    end
  else
    @log.info "Connecting to localhost #{args[:host]}"
    @mongo = Mongo::Connection.new.db(args[:db])
  end

  #db = @mongo.db((mongo_args[:db])
  #auth = db.authenticate

  unless args[:user].to_s == ''
    @log.debug "Authenticating as #{args[:user]}"
    @mongo.authenticate(args[:user], args[:password])
  end

  @mongo
end