Sha256: a63204b10de32a7e8abe41372d08197b70302b8fde203765dfdab996c398488a

Contents?: true

Size: 1.85 KB

Versions: 7

Compression:

Stored size: 1.85 KB

Contents

require "mongo"
require "logger"

module BarnyardHarvester
  class MongoDbHelper

    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
  end
end

if __FILE__ == $0

  mongo_replica_set = Array.new

  mongo_replica_set << "ip-172-19-31-44.c.qaapollogrp.edu:27017"
  mongo_replica_set << "ip-172-19-30-49.c.qaapollogrp.edu:27017"
  mongo_replica_set << "ip-172-19-31-202.c.qaapollogrp.edu:27017"

  mongo_args = Hash.new
#  mongo_args[:host] = "localhost:27017"
  mongo_args[:host] = mongo_replica_set
  mongo_args[:db] = "aws"
  mongo_args[:collection] = "test_collection"
  mongo_args[:user] = "honeybadger"
  mongo_args[:password] = "0joQuk35vJM05Hj"

  mongo_args[:debug] = true

  my_log = Logger.new(STDOUT)

  mongo_args[:logger] = my_log

  s = ApolMongo::MongoHelper.connect mongo_args

  c = s.collection(mongo_args[:collection])


  c.find.each do |row|
    puts row
  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
barnyard_harvester-0.0.13 lib/barnyard_harvester/mongodb_helper.rb
barnyard_harvester-0.0.12 lib/barnyard_harvester/mongodb_helper.rb
barnyard_harvester-0.0.10 lib/barnyard_harvester/mongodb_helper.rb
barnyard_harvester-0.0.9 lib/barnyard_harvester/mongodb_helper.rb
barnyard_harvester-0.0.8 lib/barnyard_harvester/mongodb_helper.rb
barnyard_harvester-0.0.7 lib/barnyard_harvester/mongodb_helper.rb
barnyard_harvester-0.0.5 lib/barnyard_harvester/mongodb_helper.rb