Permalink
Browse files

* zips will use a hash now to be passed in source -> destination of t…

…he files

* imap always creates a new instance now
  • Loading branch information...
1 parent 206fb5f commit f792f63a53c0a1ac797c37b2617b6451f3c56b4e @hone committed Jan 12, 2009
Showing with 35 additions and 115 deletions.
  1. +18 −20 app/models/imap.rb
  2. +2 −0 app/models/pop3.rb
  3. +6 −8 app/models/remote_mail.rb
  4. +0 −83 spec/models/imap_spec.rb
  5. +9 −4 spec/models/remote_mail_spec.rb
View
38 app/models/imap.rb
@@ -18,21 +18,15 @@ def setup_mailer
end
status = OK_FLAG
- if self.old_server != self.server or self.old_port != self.port or self.old_ssl != self.ssl
- begin
- @mailer = Net::IMAP.new( self.server, self.port, self.ssl )
- @mailer.login( self.username, self.password )
- rescue Net::IMAP::NoResponseError
- status = AUTHENTICATION_ERROR_FLAG
- rescue Errno::ETIMEDOUT
- status = TIMEOUT_ERROR_FLAG
- end
+ begin
+ @mailer = Net::IMAP.new( self.server, self.port, self.ssl )
+ @mailer.login( self.username, self.password )
+ rescue Net::IMAP::NoResponseError
+ status = AUTHENTICATION_ERROR_FLAG
+ rescue Errno::ETIMEDOUT
+ status = TIMEOUT_ERROR_FLAG
end
- self.old_server = self.server
- self.old_port = self.port
- self.old_ssl = self.ssl
-
status
end
@@ -45,15 +39,15 @@ def download
end
parent_dir = File.join( mbox_folder, folders.first.name )
parent_mbox = "#{parent_dir}.mbox"
- folders_to_zip = folders.inject(Array.new) do |sum, folder|
- folder_path = File.join( mbox_folder, folder.name.split(folder.delim).join("/") )
+ folders_to_zip = folders.inject(Hash.new) do |sum, folder|
+ folder_path = File.join( mbox_folder, convert_to_another_delim( folder ))
folder_mbox = "#{folder_path}.mbox"
- files_to_add = Array.new
- files_to_add += [folder_path] if File.exist?( folder_path )
- files_to_add += [folder_mbox] if File.exist?( folder_mbox )
+ files_to_add = Hash.new
+ files_to_add[folder_path] = folder.name if File.exist?( folder_path )
+ files_to_add[folder_mbox] = "#{convert_to_another_delim( folder )}.mbox" if File.exist?( folder_mbox )
- sum + files_to_add
+ sum.merge( files_to_add )
end
zip_output = File.join( FILE_DIR, "#{mbox_name}.zip" )
zip( folders_to_zip, zip_output )
@@ -67,7 +61,7 @@ def folders
end
def download_folder( folder, mbox_name )
- folder_path = "#{TMP_DIR}/#{mbox_name}/#{folder.name.split( folder.delim ).join( "/" )}"
+ folder_path = "#{TMP_DIR}/#{mbox_name}/#{convert_to_another_delim( folder )}"
FileUtils.mkdir( folder_path ) if folder.attr.include?( :Haschildren )
@mailer.examine( folder.name )
uids = @mailer.uid_search(['ALL'])
@@ -98,4 +92,8 @@ def search_folder( path )
end
end
end
+
+ def convert_to_another_delim( folder, other_delim = "/" )
+ folder.name.split( folder.delim ).join( other_delim )
+ end
end
View
2 app/models/pop3.rb
@@ -5,6 +5,8 @@ class Pop3 < RemoteMail
DEFAULT_SSL_PORT = 992
RemoteMailHelper::setup_columns( self )
+ column :old_server, :string
+ column :old_port, :integer
attr_reader :mailer
View
14 app/models/remote_mail.rb
@@ -7,12 +7,10 @@ def self.setup_columns( klass )
klass.class_eval do
column :email_address, :string
column :server , :string
- column :old_server, :string
column :username , :string
column :password , :string
column :ssl , :boolean
column :port , :integer
- column :old_port, :integer
end
end
end
@@ -41,8 +39,8 @@ class RemoteMail < ActiveRecord::BaseWithoutTable
validates_presence_of :username
validates_presence_of :password
validates_presence_of :mail_type
- validates_numericality_of :port, :greater_than_or_equal_to => PORT_MIN, :less_than_or_equal_to => PORT_MAX, :allow_nil => true
- validates_numericality_of :mail_type, :greater_than_or_equal_to => 1, :less_than_or_equal_to => 2
+ validates_numericality_of :port, :greater_than_or_equal_to => PORT_MIN, :less_than_or_equal_to => PORT_MAX, :allow_nil => true, :only_integer => true
+ validates_numericality_of :mail_type, :greater_than_or_equal_to => 1, :less_than_or_equal_to => 2, :only_integer => true
validates_format_of :email_address, :with => /^[\w.]+@\w+\.(\w+\.)*\w+$/
validates_format_of :server, :with => /^\w+\.\w+\.(\w+\.)*\w+$/
validates_length_of :email_address, :maximum => MAX_CHAR_LENGTH
@@ -64,13 +62,13 @@ def zip( files, output_file )
FileUtils.rm( output_file )
end
Zip::ZipFile.open(output_file, Zip::ZipFile::CREATE) do |zipfile|
- files.each do |file|
- base_file = File.basename( file )
- zipfile.add( base_file, file )
+ files.each do |source, destination|
+ base_file = File.basename( source )
+ zipfile.add( destination, source )
end
end
- files.each {|file| remove_file_dir( file ) }
+ files.keys.each {|file| remove_file_dir( file ) }
end
def remove_file_dir( path )
View
83 spec/models/imap_spec.rb
@@ -40,69 +40,6 @@ def setup_mock_net_imap
@imap.port.should == Imap::DEFAULT_SSL_PORT
end
- it "should keep track of old server" do
- setup_mock_net_imap
- Net::IMAP.should_receive(:new).twice.and_return(@net_imap)
- @net_imap.should_receive(:login).twice
- setup_imap( {}, false )
- @imap.server.should == @valid_attributes[:server]
- @imap.old_server.should be_nil
- @imap.setup_mailer
-
- new_server = "mail.wornpath.net"
- @imap.server = new_server
- @imap.server.should == new_server
- @imap.old_server.should == @valid_attributes[:server]
- @imap.setup_mailer
-
- @imap.old_server.should == new_server
- end
-
- it "should keep track of old port" do
- setup_mock_net_imap
- Net::IMAP.should_receive(:new).twice.and_return(@net_imap)
- @net_imap.should_receive(:login).twice
- setup_imap( {}, false )
- @imap.port.should == @valid_attributes[:port]
- @imap.old_port.should be_nil
- @imap.setup_mailer
-
- new_port = 992
- @imap.port = new_port
- @imap.port.should == new_port
- @imap.old_port.should == @valid_attributes[:port]
- @imap.setup_mailer
-
- @imap.old_port.should == new_port
- end
-
- it "should keep track of old ssl" do
- setup_mock_net_imap
- Net::IMAP.should_receive(:new).twice.and_return(@net_imap)
- @net_imap.should_receive(:login).twice
- setup_imap( {}, false )
- @imap.ssl.should == @valid_attributes[:ssl]
- @imap.old_ssl.should be_nil
- @imap.setup_mailer
-
- new_ssl = !@valid_attributes[:ssl]
- @imap.ssl= new_ssl
- @imap.ssl.should == new_ssl
- @imap.old_ssl.should == @valid_attributes[:ssl]
- @imap.setup_mailer
-
- @imap.old_ssl.should == new_ssl
- end
-
- it "should not create a new Net::IMAP if server or port or ssl hasn't been changed" do
- setup_mock_net_imap
- Net::IMAP.should_receive(:new).once.and_return(@net_imap)
- @net_imap.should_receive(:login).once
-
- setup_imap
- @imap.setup_mailer
- end
-
it "should create a new Net::IMAP object upon server change" do
setup_mock_net_imap
Net::IMAP.should_receive(:new).twice.and_return(@net_imap)
@@ -113,26 +50,6 @@ def setup_mock_net_imap
@imap.setup_mailer
end
- it "should create a new Net::IMAP object upon port change" do
- setup_mock_net_imap
- Net::IMAP.should_receive(:new).twice.and_return(@net_imap)
- @net_imap.should_receive(:login).twice
-
- setup_imap
- @imap.port = 992
- @imap.setup_mailer
- end
-
- it "should create a new Net::IMAP object upon ssl change" do
- setup_mock_net_imap
- Net::IMAP.should_receive(:new).twice.and_return(@net_imap)
- @net_imap.should_receive(:login).twice
-
- setup_imap
- @imap.ssl = !@valid_attributes[:ssl]
- @imap.setup_mailer
- end
-
it "should raise error on login problem" do
setup_mock_net_imap
Net::IMAP.should_receive(:new).once.and_return(@net_imap)
View
13 spec/models/remote_mail_spec.rb
@@ -37,13 +37,13 @@ def should_have_error_on_attribute( attribute, value = nil, error_num = 1 )
end
after(:all) do
- remove_file( TMP_MBOX_FILE )
+ remove_dir( TMP_MBOX_FILE )
remove_file( @zip_output )
end
it "should zip a single mbox" do
FileUtils.touch( TMP_MBOX_FILE )
- @remote_mail.zip( [TMP_MBOX_FILE], @zip_output )
+ @remote_mail.zip( { TMP_MBOX_FILE => MBOX_NAME }, @zip_output )
File.should be_exist( @zip_output )
File.should_not be_exist( TMP_MBOX_FILE )
end
@@ -55,12 +55,17 @@ def should_have_error_on_attribute( attribute, value = nil, error_num = 1 )
remove_dir( TMP_MBOX_FILE )
remove_dir( parent_dir )
remove_file( parent_mbox )
+ files = {
+ parent_mbox => "INBOX.mbox",
+ parent_dir => "INBOX",
+ child_mbox => "INBOX/Drafts.mbox"
+ }
FileUtils.mkdir( TMP_MBOX_FILE )
FileUtils.touch( parent_mbox )
FileUtils.mkdir( parent_dir )
FileUtils.touch( child_mbox )
- @remote_mail.zip( [parent_mbox, parent_dir], @zip_output )
+ @remote_mail.zip( files, @zip_output )
File.should be_exist( @zip_output )
File.should_not be_exist( parent_dir )
@@ -71,7 +76,7 @@ def should_have_error_on_attribute( attribute, value = nil, error_num = 1 )
remove_file( @zip_output )
File.open( @zip_output, 'w' ) {|file| file.puts "delete this" }
- @remote_mail.zip( [TMP_MBOX_FILE], @zip_output )
+ @remote_mail.zip( { TMP_MBOX_FILE => MBOX_NAME }, @zip_output )
File.should be_exist( @zip_output )
File.open( @zip_output ) {|file| file.readlines.first.should_not match /delete this/ }
end

0 comments on commit f792f63

Please sign in to comment.