Ruby - Working with Mechanize::File response without saving to disk - ruby

I'm working on my first ORM project and am using Mechanize. Here's the situation:
I'm downloading a zip file from my website into a Mechanize::File object. Inside the zip is a file buried three folders deep (folder_1/folder_2/file.txt). I'd like to pull file.txt out of the zip file and return that instead of the zip file itself.
My first thought was to use zip/zipfilesystem. I can do this fine if I save the file to the disk first and use Zip::ZipFile.open(src) but can anyone tell me how/if it is possible to send it over straight from the Mechanize::File.body.
My gut says this has to be possible and I'm just missing something basic. I tried...
zipfile = Mechanize::File.body
Zip::ZipFile.open(zipfile)
...but from what I can tell Zip::ZipFile is only set up to locate a source from a filesystem.
Any direction would be very appreciated and let me know if there are any questions
Thanks in advance
Rob

It seems what you want to do is not possible with rubyzip. From rubyzip library's TODO file:
SUggestion: ZipInputStream/ZipOutputStream should accept an IO object in addition to a filename.

Related

check if file exist on Sharepoint Online by File Name with Power Automate

I am trying to create a flow in Power Automate Cloud that allows me to extract some files from Google Drive and copy them to a Sharepoint folder. But before copying it, it has to check by the file name, that the file does not already exist in the sharepoint folder. I have managed to do the whole flow, except the checking.
I would be very grateful for your help.
Thank you very much. Best regards.
A File exists action doesn't currently exist but you can use the Get file metadata using path action from the SharePoint connector to do the same sort of thing.
This flow below shows you an example of what you can do ...
For demonstration purposes, I've created an array with a list of file names (full path required) that I will then test the existence for.
One of those files doesn't exist.
I then loop through each one of those and try and get the metadata for each file.
If the resulting statusCode from the call to get the metadata is not equal to 200, then we know the file doesn't exist.
Examples ...
To make this work however, you need to make sure that the Condition step will execute after the metadata call no matter what the result.
Do this by making sure the Configure run after settings are correct ...
So after all of that, all you need to do is deal with the true and false sections of the condition.

The document \"<?xml version='1.0'?>\\n\" does not have a valid root

I am new to parsing the xml in the ruby and I am stuck with an issue. I'll try my best to explain.
I get the below response from an api
"PK\x03\x04\x14\x00\b\b\b\x00,\x18ET\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1D\x00\x00\x00"
I believe it is .tds format.
I am trying to parse it into a valid xml so this is what I tried.
xml = Nokogiri::XML(response)
which gives me #<Nokogiri::XML::Document:0xf744 name="document">
Then I tried to do Hash.from_xml(xml.to_xml)
But this throws error The document \"<?xml version='1.0'?>\\n\" does not have a valid root
Any idea what am I missing here?
This string starts with "PK", which are initials of Phil Katz, author of the ZIP format. Which means it's a ZIP file. There are certain formats that are ZIP files, but actually follow some further structure conventions, like Java JAR files, but also all OpenDocument formats like .ods, .odt and all MS Office Open XML formats.
Since you are probably expecting an .ods file... While you could unzip it and then use Nokogiri to parse the XML, there's a better way to proceed.
There's an interesting Gem called Roo, that supports all most common spreadsheet formats and produces a nice Ruby API to deal with them: https://github.com/roo-rb/roo
I would recommend you to save the string to a temporary file and then open it with Roo.

How to simple test the file download and upload?

I have two api for file download and upload. (/static/{filename} api for downloading, /upload api for uploading) For consistency, it need a test file to ensure the functionality of them two. I check the official fs_test.go, but it is too massive to use, is there a trick way to do it?
Do you need a unit test? If you are supposed to test saving the file you can use a brute force.
Create file
Check if it exists (optional: check the file data)
Delete file

How to create a variable in env.rb or hook.rb and call in cucumber feature ?

Thank you in advance for help.
I am trying to create variable in env.rb
File_path =("#{File.dirname(__FILE__)}/../../features/TestData/Testdata.html")
and call File_path in the Cucumber feature directly.
for example
...
And I upload a file from path "<File_path>"
...
Is the is a better way of doing this ?
Yes, there's a better way.
In your feature text, use meaningful names instead of file paths.
For example, any of these, depending on your goal:
And I upload a file of test data
And I upload my web page
And I upload some previously saved information
Then put the file path in your step file.
In general, feature descriptions should be plain language, not source code, file paths, env vars, etc

How do I open or create word documents in CodeIgniter?

I have a form on my CodeIgniter app that has users uploading doc files to a directory on my server. I need to be able to open these files from the app. I am trying to create a method that downloads these files so that I can open them in a word processor. No errors display on my method's output(php errors), but the files do not come out correctly in open office. There are 217 pages in the downloaded file(it should really only have 3 pages) and they are filled with random characters.
Example:
ÐÏ#ࡱ#á################;###þÿ #################<###################þÿÿÿ########ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿýÿÿÿÿÿÿÿþÿÿÿþÿÿÿ################ ###
#######
So this is what I have for my controller method.
function downloadresume($filename, $ext){
$this->load->helper('download');
$this->load->helper('file');
$content = read_file('./_resumes/'.$filename.'.'.$ext);
force_download($filename.'.'.$ext, $content);
}
Is there a better way to do this? Am I missing something?
You could have a look at http://phpword.codeplex.com/ I have used it in several projects and it seems to work very well. You will need to write a bridging library so you can use it but that is really not so difficult.
I hope this helps, good luck

Resources