Skip to content

Commit

Permalink
move attachment handling code into its own method
Browse files Browse the repository at this point in the history
  • Loading branch information
max-mapper committed Apr 29, 2011
1 parent e0c8fc3 commit dd8e22c
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions gmail/fetch_data.rb
Expand Up @@ -29,20 +29,8 @@ def to_json
:size => @size,
:flags => @flags,
:raw_mail => @mail.to_s,
'_attachments' => {}
'_attachments' => format_attachments
}
if @mail.attachments.length > 0
@mail.attachments.each do |attachment|
begin
obj['_attachments'][attachment.filename] = {
content_type: attachment.content_type.to_s.split("\;")[0],
data: Base64.encode64(attachment.body.decoded)
}
rescue Exception => e
puts "unable to process #{attachment.filename}"
end
end
end
obj.to_json
end

Expand Down Expand Up @@ -73,6 +61,23 @@ def message
#{formatter.process_body}
EOF
end

def format_attachments
attachments = {}
if @mail.attachments.length > 0
@mail.attachments.each do |attachment|
begin
attachments[attachment.filename] = {
content_type: attachment.content_type.to_s.split("\;")[0],
data: Base64.encode64(attachment.body.decoded)
}
rescue Exception => e
puts "unable to process #{attachment.filename}"
end
end
end
attachments
end

def format_subject(subject)
Mail::Encodings.unquote_and_convert_to((subject || ''), 'UTF-8')
Expand Down

0 comments on commit dd8e22c

Please sign in to comment.