ruby - Unzip binary email attachment -


A railway app has a background job that generates a large CSV file and sends it as an attachment for an email message.

To manage the size of the email, I'm zipping the CSV before applying it. It works fine; We have letter openers in development and I can open zip attached attachments and read data. I am using the "rubypip" gem to zip.

This trick does this test. By using RSPC, I can verify that the message has an attachment and that the type of attachment is application / x-zip-encoded , which is all good, what I would like to do, attachment to Opening and validating the data within the trial, though, and it is proving to be more difficult: attachment is an object of mail :: part type. If I call Attachment part , then I will call it part.body.raw_source , part.body.encoded or I can get data using Part.decode_body . (The first and the third are identical.) part.body.encoding tells me that the body is binary when I have a zip :: inputstream object ( zip :: InputStream.open (part.body. Raw_source) or any other data methods) I get something like code> Logic Error: There is an empty byte in the string .

How can I open this attachment and read its data within RSPC? I have a hip which requires a decoding step here.

Errors, but there is no answer there.

Its solution was slightly less than ideal, but it works. I had to write the attachment to the mail library using the file, then use zip :: file to remove the original data and verify it.

  This is a zip attachment 'additional_filds = ["student_identifiers"] is expected (message.attachments.size) .to eq (1) message.attachments.first.tap do | Zip | Expect (zip.content_type) .to eq ('application / x-zip-compressed') tmpfile_name = "tmp / # {zip.filename}" file. Open (tmpfile_name, "w + b", 0644) {| F | F.write zip.decoded} csv = Zip :: File.open (tmpfile_name) do | Zip_file | Entry = zip_file.glob ('* .csv') First entry.get_input_stream read and csv.split ("\ n"). Tap | Rows | Expectations (rows.size). (2) Expected for rows (CSV.parse_line (line). Size). AEC (Dimension_com) End And File Delete (TMP FileName) End End  

Comments

Popular posts from this blog

winforms - C# Form - Property Change -

java - Messages from .properties file do not display UTF-8 characters -

javascript - amcharts makechart not working -