Permalink
Browse files

* generates mbox name based off of a hash of the email address and time

* writes mbox to file in /public/download/hash.mbox
  • Loading branch information...
1 parent fbbb3bc commit 06bb85da2f5368fd35419c1115fdc41e43cc571f @hone committed Jan 8, 2009
Showing with 78 additions and 26 deletions.
  1. +23 −4 app/models/pop3.rb
  2. +55 −22 spec/models/pop3_spec.rb
View
@@ -1,4 +1,5 @@
require 'net/pop'
+require 'digest/sha1'
class Pop3 < ActiveRecord::BaseWithoutTable
DEFAULT_PORT = 110
@@ -8,6 +9,8 @@ class Pop3 < ActiveRecord::BaseWithoutTable
AUTHENTICATION_ERROR_FLAG = :authentication_error
TIMEOUT_ERROR_FLAG = :timeout_error
+ FILE_DIR = File.join( RAILS_ROOT, 'public/download' )
+
column :email_address, :string
column :server , :string
column :old_server, :string
@@ -17,7 +20,7 @@ class Pop3 < ActiveRecord::BaseWithoutTable
column :port , :integer
column :old_port, :integer
- attr_reader :mailer, :mails
+ attr_reader :mailer
# callbacks don't seem to be working
# before_save :setup_mailer
@@ -47,19 +50,35 @@ def setup_mailer
def download
# reset mail info
- @mails = nil
+ mail_count = nil
status = OK_FLAG
begin
@mailer.start( self.username, self.password )
- @mails = @mailer.mails
+ mail_count = 3
+ write_mbox
@mailer.finish
rescue Net::POPAuthenticationError
status = AUTHENTICATION_ERROR_FLAG
rescue Timeout::Error
status = TIMEOUT_ERROR_FLAG
end
- { :mails => @mails, :status => status }
+ { :mail_count => mail_count, :status => status }
+ end
+
+ # generate hash based of e-mail address and current time
+ def generate_mbox_name
+ Digest::SHA1.hexdigest( "#{self.email_address}|#{Time.now.to_s}" )
+ end
+
+ def write_mbox
+ File.open( "#{FILE_DIR}/#{generate_mbox_name}.mbox", 'a' ) do |file|
+ @mailer.each_mail do |mail|
+ mail.pop do |chunk|
+ file.write chunk
+ end
+ end
+ end
end
end
View
@@ -1,5 +1,7 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+MBOX_FILE = File.join( RAILS_ROOT, 'public', 'download', 'bd4937b271d8f20c3003489a231b3824943a163f.mbox' )
+
module Pop3SpecHelper
def setup_pop3( opts = {}, setup_mailer = true )
@valid_attributes = {
@@ -16,11 +18,28 @@ def setup_pop3( opts = {}, setup_mailer = true )
@pop3.setup_mailer if setup_mailer
end
- def setup_mock_pop3( opts = {}, setup_mailer = true )
+ def setup_mock_mailer_and_time_pop3( opts = {}, setup_mailer = true )
+ @mailer = mock( "Net::POP3" )
Net::POP3.should_receive(:new).once.and_return(@mailer)
@mailer.should_receive(:enable_ssl).once
+ @time = mock("Time")
+ Time.stub!(:now).and_return(@time)
+ @time.stub!(:to_s).and_return( 'Thu Jan 08 01:22:01 -0500 2009' )
+ setup_pop3( opts, setup_mailer )
+ end
+
+ def setup_mock_time_pop3( opts = {}, setup_mailer = true )
+ @time = mock("Time")
+ Time.should_receive(:now).once.and_return(@time)
+ @time.should_receive(:to_s).once.and_return( 'Thu Jan 08 01:22:01 -0500 2009' )
setup_pop3( opts, setup_mailer )
end
+
+ def remove_file( file )
+ if File.exist?( file )
+ FileUtils.rm( file )
+ end
+ end
end
describe Pop3, "setup mailer" do
@@ -72,7 +91,7 @@ def setup_mock_pop3( opts = {}, setup_mailer = true )
@pop3.old_server.should == new_server
end
- it "should kepe track of old port" do
+ it "should keep track of old port" do
setup_pop3( @valid_attributes, false )
@pop3.port.should == @valid_attributes[:port]
@pop3.old_port.should be_nil
@@ -116,53 +135,67 @@ def setup_mock_pop3( opts = {}, setup_mailer = true )
include Pop3SpecHelper
before(:each) do
- @mailer = mock( "Net::POP3" )
+ remove_file( MBOX_FILE )
end
- it "should not connect due to authentication problem after downloading mail" do
- setup_pop3
-
- result = @pop3.download
- result[:mails].should_not be_nil
- result[:status].should == Pop3::OK_FLAG
-
- @pop3.mailer.should_receive(:start).once.and_raise(Net::POPAuthenticationError)
- result = @pop3.download
-
- result[:mails].should be_nil
- result[:status].should == Pop3::AUTHENTICATION_ERROR_FLAG
+ after(:all) do
+ remove_file( MBOX_FILE )
end
it "should not connect due to authentication problem" do
- setup_mock_pop3( :username => 'boo' )
+ setup_mock_mailer_and_time_pop3( :username => 'boo' )
@mailer.should_receive(:start).once.and_raise(Net::POPAuthenticationError)
result = @pop3.download
- result[:mails].should be_nil
+ result[:mail_count].should be_nil
result[:status].should == Pop3::AUTHENTICATION_ERROR_FLAG
end
it "should not connect due to invalid server" do
- setup_mock_pop3( :server => 'pop.wornpath.net' )
+ setup_mock_mailer_and_time_pop3( :server => 'pop.wornpath.net' )
@mailer.should_receive(:start).once.and_raise(Timeout::Error)
result = @pop3.download
- result[:mails].should be_nil
+ result[:mail_count].should be_nil
result[:status].should == Pop3::TIMEOUT_ERROR_FLAG
end
it "should download mail" do
- setup_pop3
+ setup_mock_time_pop3
result = @pop3.download
- @pop3.mails.size.should == 3
result.should_not be_nil
- result[:mails].should === @pop3.mails
+ result[:mail_count].should === 3
result[:status].should === Pop3::OK_FLAG
end
end
+describe Pop3, "write mbox" do
+ include Pop3SpecHelper
+
+ before(:each) do
+ remove_file( MBOX_FILE )
+ end
+
+ after(:all) do
+ remove_file( MBOX_FILE )
+ end
+
+ it "should generate mbox name" do
+ setup_mock_time_pop3
+
+ @pop3.generate_mbox_name.should == 'bd4937b271d8f20c3003489a231b3824943a163f'
+ end
+
+ it "should generate mbox file" do
+ setup_mock_time_pop3
+ @pop3.download
+
+ File.should be_exist( MBOX_FILE )
+ end
+end
+
describe Pop3, "validations" do
end

0 comments on commit 06bb85d

Please sign in to comment.