useful. As you can see it's not documented or tested yet. I put this together
mostly in one evening in a coffeeshop. It shows in some ways. caveat programmer.
+=head1 SYNOPSIS
+
+Email::Archive provides an interface to store emails. Storage engines should implement
+
=head1 LICENSE
This library may be used under the same terms as Perl itself.
--- /dev/null
+package Email::Archive::Storage::MongoDB;
+use Moo;
+use Carp;
+use Email::MIME;
+use Email::Abstract;
+use MongoDB;
+use autodie;
+with q/Email::Archive::Storage/;
+
+has host => (
+ is => 'rw',
+ default => 'localhost',
+);
+
+has port => (
+ is => 'rw',
+ default => 27017,
+);
+
+has database => (
+ is => 'rw',
+);
+
+has collection => (
+ is => 'rw',
+);
+
+sub store {
+ my ($self, $email) = @_;
+ $email = Email::Abstract->new($email);
+ $self->collection->insert({
+ message_id => $email->get_header('Message-ID'),
+ from_addr => $email->get_header('From'),
+ to_addr => $email->get_header('To'),
+ date => $email->get_header('Date'),
+ subject => $email->get_header('Subject'),
+ body => $email->get_body,
+ });
+}
+
+sub search {
+ my ($self, $attribs) = @_;
+ my $message = $self->collection->find_one($attribs);
+
+ return Email::MIME->create(
+ header => [
+ From => $message->{from_addr},
+ To => $message->{to_addr},
+ Subject => $message->{subject},
+ ],
+ body => $message->{body},
+ );
+}
+
+sub retrieve {
+ my ($self, $message_id) = @_;
+ $self->search({ message_id => $message_id });
+}
+
+sub storage_connect {
+ my ($self, $mongo_con_info) = @_;
+ if (defined $mongo_con_info){
+ # should look like host:port:database
+ my ($host, $port, $database, $collection) = split ':', $mongo_con_info;
+ $self->host($host);
+ $self->port($port);
+ $self->database($database);
+ $self->collection($collection);
+ }
+
+ my $conn = MongoDB::Connection->new(
+ host => $self->host,
+ port => $self->port,
+ );
+
+ my $datab = $self->database;
+ my $collec = $self->collection;
+
+ my $db = $conn->$datab;
+ my $coll = $db->$collec;
+
+ # replace name with actual collection object
+ $self->collection($coll);
+
+ return 1;
+}
cmp_ok($found->header('subject'), 'eq', "Message in a bottle",
"can find stored message by ID");
-my $e_dbic = Email::Archive->new(
- storage => Email::Archive::Storage::DBIC->new,
-);
-$e_dbic->connect('dbi:SQLite:dbname=t/test_dbic.db');
-$e_dbic->store($email);
-
-$found = $e_dbic->retrieve('helloworld');
-cmp_ok($found->header('subject'), 'eq', "Message in a bottle",
- "can find stored message by ID");
-
-
done_testing;
+
unlink 't/test.db';
-unlink 't/dbic_test.db';
+
--- /dev/null
+#!/usr/bin/perl
+use strict;
+use warnings;
+use Email::MIME;
+
+use Test::More;
+
+use Email::Archive;
+use Email::Archive::Storage::DBIC;
+
+my $email = Email::MIME->create(
+ header => [
+ From => 'foo@example.com',
+ To => 'drain@example.com',
+ Subject => 'Message in a bottle',
+ 'Message-ID' => 'helloworld',
+ ],
+ body => 'hello there!'
+);
+
+my $e = Email::Archive->new(
+ storage => Email::Archive::Storage::DBIC->new,
+);
+$e->connect('dbi:SQLite:dbname=t/test_dbic.db');
+$e->store($email);
+
+my $found = $e->retrieve('helloworld');
+cmp_ok($found->header('subject'), 'eq', "Message in a bottle",
+ "can find stored message by ID");
+
+
+done_testing;
+
+unlink 't/test_dbic.db';
+
--- /dev/null
+#!/usr/bin/perl
+use strict;
+use warnings;
+use Email::MIME;
+
+use Test::More;
+
+use Email::Archive;
+use Email::Archive::Storage::MongoDB;
+
+my $email = Email::MIME->create(
+ header => [
+ From => 'foo@example.com',
+ To => 'drain@example.com',
+ Subject => 'Message in a bottle',
+ 'Message-ID' => 'helloworld',
+ ],
+ body => 'hello there!'
+);
+
+my $storage = Email::Archive::Storage::MongoDB->new(
+ host => 'localhost',
+ port => 27017,
+ database => 'EmailArchiveTestMongoDB',
+ collection => 'emails',
+);
+
+my $e = Email::Archive->new(
+ storage => $storage,
+);
+
+$e->connect;
+$e->store($email);
+
+my $found = $e->retrieve('helloworld');
+
+$found = $e->retrieve('helloworld');
+cmp_ok($found->header('subject'), 'eq', "Message in a bottle",
+ "can find stored message by ID");
+
+
+done_testing;
+
+my $conn = MongoDB::Connection->new(
+ host => 'localhost',
+ port => 27017
+);
+
+$conn->EmailArchiveTestMongoDB->drop;
+