Class: Received::Backend::Mongodb

Inherits:
Base
  • Object
show all
Defined in:
lib/received/backend/mongodb.rb

Instance Method Summary collapse

Methods inherited from Base

add_observer, #notify_observers, notify_observers, observers, remove_observer

Constructor Details

#initialize(params) ⇒ Mongodb

Initialize MongoDB storage backend

Parameters:

  • params (Hash)

Options Hash (params):

  • host (String)
  • database (String)
  • collection (String)


13
14
15
16
# File 'lib/received/backend/mongodb.rb', line 13

def initialize(params)
  db = Mongo::Connection.new(params['host']).db(params['database'])
  @coll = db.collection(params['collection'])
end

Instance Method Details

#store(mail) ⇒ ObjectId

Store mail in MongoDB

Parameters:

  • mail (Hash)

Returns:

  • (ObjectId)

    object_id



22
23
24
25
26
27
# File 'lib/received/backend/mongodb.rb', line 22

def store(mail)
  mail = mail.merge(:ts => Time.now.to_i, :body => BSON::Binary.new(mail[:body]))
  @coll.insert(mail, :w => 1, :fsync => true).tap do |result|
    notify_observers(:after_save, result) if result
  end
end