/
connection.rb
45 lines (38 loc) · 1.19 KB
/
connection.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
require 'net/imap'
module Imap
module Backup
module Account
class Connection
attr_reader :username
attr_reader :imap
def initialize(options)
@username = options[:username]
@local_path, @backup_folders = options[:local_path], options[:folders]
@imap = Net::IMAP.new('imap.gmail.com', 993, true)
@imap.login(@username, options[:password])
end
def disconnect
@imap.disconnect
end
def folders
@imap.list('/', '*')
end
def status
@backup_folders.map do |folder|
f = Imap::Backup::Account::Folder.new(self, folder[:name])
s = Imap::Backup::Serializer::Directory.new(@local_path, folder[:name])
{:name => folder[:name], :local => s.uids, :remote => f.uids}
end
end
def run_backup
@backup_folders.each do |folder|
f = Imap::Backup::Account::Folder.new(self, folder[:name])
s = Imap::Backup::Serializer::Directory.new(@local_path, folder[:name])
d = Imap::Backup::Downloader.new(f, s)
d.run
end
end
end
end
end
end