Class: Attachments::Extract
- Inherits:
-
Object
- Object
- Attachments::Extract
- Defined in:
- lib/attachments/extract.rb
Overview
Extract attachments of specific types from emails and save them in files
Constant Summary collapse
- UNCERTAIN_TYPES =
[ "application/octet-stream" ].to_set
Instance Method Summary collapse
- #close ⇒ Object
- #files ⇒ Object
- #from ⇒ Object
- #html_body ⇒ Object
-
#initialize(include_types = [ "text/plain" ], options = { :cache_in_memory => false }) ⇒ Extract
constructor
A new instance of Extract.
- #last_parsed ⇒ Object
- #mail ⇒ Object
- #name ⇒ Object
- #parse(filename_or_hash) ⇒ Object
- #parse_data(raw_mail_data) ⇒ Object
- #parse_file(filename) ⇒ Object
- #parse_mail(mail) ⇒ Object
- #subject ⇒ Object
- #text_body ⇒ Object
- #to ⇒ Object
Constructor Details
#initialize(include_types = [ "text/plain" ], options = { :cache_in_memory => false }) ⇒ Extract
Returns a new instance of Extract.
23 24 25 26 27 |
# File 'lib/attachments/extract.rb', line 23 def initialize include_types = [ "text/plain" ], = { :cache_in_memory => false } @include_types = include_types @cache_in_memory = [ :cache_in_memory ] || false reset end |
Instance Method Details
#close ⇒ Object
29 30 31 32 33 34 |
# File 'lib/attachments/extract.rb', line 29 def close files.each do |f| FileUtils::rm(f[:tmpfile]) unless @cache_in_memory end reset end |
#files ⇒ Object
133 134 135 |
# File 'lib/attachments/extract.rb', line 133 def files @files end |
#from ⇒ Object
86 87 88 |
# File 'lib/attachments/extract.rb', line 86 def from (@mail && @mail.from) || nil end |
#html_body ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/attachments/extract.rb', line 115 def html_body if(@mail && @mail.text_part && @mail.text_part.body) m = @mail.text_part.body.decoded charset = @mail.text_part.charset charset ? Iconv.conv("utf-8", charset, m) : m else nil end end |
#last_parsed ⇒ Object
137 138 139 |
# File 'lib/attachments/extract.rb', line 137 def last_parsed @last_parsed end |
#mail ⇒ Object
125 126 127 |
# File 'lib/attachments/extract.rb', line 125 def mail @mail end |
#name ⇒ Object
129 130 131 |
# File 'lib/attachments/extract.rb', line 129 def name @name end |
#parse(filename_or_hash) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/attachments/extract.rb', line 36 def parse filename_or_hash hash = case when filename_or_hash.is_a?(String) then { :filename => filename_or_hash } when filename_or_hash.is_a?(Hash) then filename_or_hash else {} end content = case when hash[:filename] then read_file(hash[:filename]) when hash[:content] then hash[:content] when hash[:stream] then hash[:stream].read() else nil end if content parse_data content elsif hash[:mail] parse_mail hash[:mail] end end |
#parse_data(raw_mail_data) ⇒ Object
70 71 72 73 |
# File 'lib/attachments/extract.rb', line 70 def parse_data raw_mail_data mail = Mail.new(raw_mail_data) parse_mail mail end |
#parse_file(filename) ⇒ Object
64 65 66 67 68 |
# File 'lib/attachments/extract.rb', line 64 def parse_file filename @last_parsed = filename content = read_file filename parse_data content end |
#parse_mail(mail) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/attachments/extract.rb', line 75 def parse_mail mail @mail = mail # Parse parts recursively until it is not multipart # Ignore types that are not suited for forwarding parse_part @mail end |
#subject ⇒ Object
90 91 92 93 94 95 96 97 |
# File 'lib/attachments/extract.rb', line 90 def subject s = (@mail && @mail.subject) || nil if s && s.respond_to?(:encode) s.encode("utf-8") else s end end |
#text_body ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/attachments/extract.rb', line 99 def text_body if(@mail && @mail.text_part && @mail.text_part.body) m = @mail.text_part.body.decoded charset = @mail.text_part.charset text = charset ? Iconv.conv("utf-8", charset, m) : m (text.respond_to? :force_encoding) ? text.force_encoding("utf-8") : text elsif(@mail && @mail.body && @mail.content_type.to_s.include?("text/plain")) m = @mail.body.decoded charset = @mail.charset text = charset ? Iconv.conv("utf-8", charset, m) : m (text.respond_to? :force_encoding) ? text.force_encoding("utf-8") : text else nil end end |
#to ⇒ Object
82 83 84 |
# File 'lib/attachments/extract.rb', line 82 def to (@mail && @mail.to) || nil end |