Sha256: dfeaadc61ae7f75596850a6e8ca711616d36570718973bf959126c8b509d62e7

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 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.authenticate(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.2 lib/jschat/storage/mongo.rb
jschat-0.2.1 lib/jschat/storage/mongo.rb