Class: MailCleaner

Inherits:
Object
  • Object
show all
Defined in:
lib/mail_cleaner.rb

Instance Method Summary collapse

Constructor Details

#initialize(output, timestamp) ⇒ MailCleaner

Returns a new instance of MailCleaner.



6
7
8
# File 'lib/mail_cleaner.rb', line 6

def initialize(output, timestamp)
    @oh = OutputHelper.new(output, timestamp)
end

Instance Method Details

#cleanObject



10
11
12
13
14
15
16
17
18
19
# File 'lib/mail_cleaner.rb', line 10

def clean
    removeok
    move 'tocc'
    move 'others'

    delmout '.grep'
    delmout '.fok'
    delmout '.ftocc'
    delmout '.fothers'
end

#cleanothersObject



29
30
31
32
33
34
# File 'lib/mail_cleaner.rb', line 29

def cleanothers
    d = @oh.getdir("dothers")
    FileUtils.rm_r(d) if Dir.exists?(d)

    delmout '.grep.others'
end

#cleantoccObject



21
22
23
24
25
26
27
# File 'lib/mail_cleaner.rb', line 21

def cleantocc
    d = @oh.getdir("dtocc")
    FileUtils.rm_r(d) if Dir.exists?(d)

    delmout '.grep.tocc'
    delmout '.mtocc'
end

#delmout(ext) ⇒ Object



50
51
52
53
54
# File 'lib/mail_cleaner.rb', line 50

def delmout(ext)
    mout = @oh.getmout(ext)
    return unless File.exist?(mout)
    File.delete mout
end

#getpath(line) ⇒ Object



78
79
80
81
82
# File 'lib/mail_cleaner.rb', line 78

def getpath(line)
    start = 0
    start = 1 if line[0] == '#'
    line[start..-2]
end

#move(suffix) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mail_cleaner.rb', line 56

def move(suffix)
    f = @oh.getmout(".f#{suffix}")
    return unless File.exist?(f)
    d = @oh.getdir("d#{suffix}")
    raise "#{d} exits." if Dir.exist?(d)

    Dir.mkdir d
    hasfile = false
    File.foreach(f) do |line|
        begin
            path = getpath(line)
            filename = File.basename(path)
            FileUtils.mv path, File.join(d, filename)
            hasfile = true
        rescue Exception => e
            puts "Can't move file [#{path}] to [#{d}]. \nMessage:#{e.message}"
        end
    end

    Dir.rmdir d unless hasfile
end

#removeokObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mail_cleaner.rb', line 36

def removeok
    fok = @oh.getmout('.fok')
    return unless File.exist?(fok)

    File.foreach(fok) do |line|
        begin
            path = getpath(line)
            File.delete path
        rescue Exception => e
            puts "Can't remove file [#{path}]. \nMessage:#{e.message}"
        end
    end
end