Class: Eml2Html::Converter

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

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ Converter

Returns a new instance of Converter.



13
14
15
16
17
18
# File 'lib/eml2html/converter.rb', line 13

def initialize(message)
  @message = Mail.read(message)
  @basename = File.basename(message, '.eml')
  @cids_in_body = []
  read_attachments
end

Instance Method Details

#attachment(file_name) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/eml2html/converter.rb', line 73

def attachment(file_name)
  each_attachment do |name, content|
    if name == file_name
      return content
    end
  end
  nil
end

#attachments(include_in_doc = false) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/eml2html/converter.rb', line 59

def attachments(include_in_doc=false)
  rtn_attachments = []
  if include_in_doc
    rtn_attachments = @attachments
  else
    @attachments.each do |a|
      unless @cids_in_body.include?(a.cid)
        rtn_attachments.push(a)
      end
    end
  end
  rtn_attachments
end

#html_bodyObject



82
83
84
# File 'lib/eml2html/converter.rb', line 82

def html_body
  replace_images_src(@message.html_part.body.to_s)
end

#save_files!Object



20
21
22
23
24
# File 'lib/eml2html/converter.rb', line 20

def save_files!
  [:txt, :html, :zip].each do |ext|
    send(:"save_#{ext}")
  end
end

#save_htmlObject



30
31
32
33
34
35
# File 'lib/eml2html/converter.rb', line 30

def save_html
  File.write(filename(:html), html_body)
  each_attachment do |name, content|
    File.write(name, content)
  end
end

#save_txtObject



26
27
28
# File 'lib/eml2html/converter.rb', line 26

def save_txt
  File.write(filename(:txt), text_body)
end

#save_zip(options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/eml2html/converter.rb', line 37

def save_zip(options = {})
  Zip::File.open(filename(:zip), Zip::File::CREATE) do |zipfile|
    if options[:include_html]
      zipfile.get_output_stream(filename(:html)) do |out|
        out << html_body
      end
    end

    if options[:include_text]
      zipfile.get_output_stream(filename(:txt)) do |out|
        out << text_body
      end
    end

    each_attachment do |name, content|
      zipfile.get_output_stream(name) do |out|
        out << content
      end
    end
  end
end