Class: GrepMail

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

Instance Method Summary collapse

Constructor Details

#initialize(file, lines_has_newline, maxval) ⇒ GrepMail

Returns a new instance of GrepMail.



6
7
8
9
10
# File 'lib/grep_mail.rb', line 6

def initialize(file, lines_has_newline, maxval)
    @maxval = maxval
    @pcontent= file.length + 1
    grouplines(lines_has_newline)
end

Instance Method Details

#finish_multiline_valueObject



41
42
43
44
45
# File 'lib/grep_mail.rb', line 41

def finish_multiline_value
    return if @groups.length == 0
    curgrp = @groups[-1]
    curgrp[:valend] = true if !curgrp[:afterlines].nil? and curgrp[:afterlines].length < @maxval
end

#grouplines(lines_has_newline) ⇒ Object



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
# File 'lib/grep_mail.rb', line 12

def grouplines(lines_has_newline)
    @groups = []
    valend = false
    lines_has_newline.each do |line|
        dlm = line[@pcontent-1]
        if dlm == ':'
            finish_multiline_value 

            valend = false
            k,v = keyvalue(line)
            @groups << {key: k, value: v}
        else
            next if valend

            v = valueonly(line)
            if v
                curgrp = @groups[-1]
                curgrp[:afterlines] = [] unless curgrp[:afterlines]
                curgrp[:afterlines] << v
            else
                valend = true
                finish_multiline_value
            end
        end
    end

    finish_multiline_value
end

#is_content_key?(line, start) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
# File 'lib/grep_mail.rb', line 59

def is_content_key?(line, start)
    return false unless line and line.length > start + 1
    return false unless line[start].upcase == 'C' and line[start+1].downcase == 'o'
    true
end

#iscomplete?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/grep_mail.rb', line 92

def iscomplete?
    @iscomplete
end

#istocc?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/grep_mail.rb', line 96

def istocc?
    @istocc
end

#keyvalue(line) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/grep_mail.rb', line 47

def keyvalue(line)
    if is_content_key?(line, @pcontent)
        dlm = ';' 
    else
        dlm = ':'
    end
    pos = line.index(dlm, @pcontent)
    return nil, nil unless pos
    
    return line[@pcontent..pos], line[(pos+2)..-2]
end

#tocolsObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/grep_mail.rb', line 76

def tocols
    lh = LogHelper.new(@groups)
    cols = lh.tocols

    toccok = lh.tocc_complete?
    othersok = lh.others_complete?
    @iscomplete = false
    @istocc = false
    if toccok and othersok
        @iscomplete = true
    elsif othersok
        @istocc = true
    end
    cols
end

#tologObject



71
72
73
74
# File 'lib/grep_mail.rb', line 71

def tolog
    cols = tocols
    return cols[1], cols.join("\t")
end

#valueonly(line) ⇒ Object



65
66
67
68
69
# File 'lib/grep_mail.rb', line 65

def valueonly(line)
    cap = line[@pcontent]
    return nil if cap != ' ' and cap != "\t"
    line[(@pcontent+1)..-2]
end