Sha256: eb83a579cd136ef4a6bb1e9889b81f32231ee17e742032dedef88752f2fdb736
Contents?: true
Size: 1.24 KB
Versions: 2
Compression:
Stored size: 1.24 KB
Contents
begin require 'mongo' rescue LoadError end module JsChat::Storage module MongoDriver def self.connect! @db = Mongo::Connection.new(ServerConfig['db_host'], ServerConfig['db_port']).db(ServerConfig['db_name']) if ServerConfig['db_username'] and ServerConfig['db_password'] unless @db.add_auth(ServerConfig['db_name'], ServerConfig['db_username'], ServerConfig['db_password']) raise 'Bad Mongo username or password' end end end def self.log(message, room) message['room'] = room @db['events'].insert(message) end def self.lastlog(number, room) @db['events'].find({ :room => room }, { :limit => number, :sort => ['time', Mongo::ASCENDING] }).to_a end # TODO: use twitter oauth for the key def self.find_user(options) @db['users'].find_one(options) end def self.save_user(user) @db['users'].save user end def self.set_rooms(name, rooms) user = find_user({ 'name' => name }) user ||= { 'name' => name } user['rooms'] = rooms save_user user end def self.available? return unless Object.const_defined?(:Mongo) connect! rescue puts 'Failed to connect to mongo' false end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
jschat-0.2.4 | lib/jschat/storage/mongo.rb |
jschat-0.2.3 | lib/jschat/storage/mongo.rb |