Using Mechanize with Ruby I get a certain file using agent.get('http://example.com/foo.torrent'), with FileUtils or otherwise, how do I save this file to my hard drive (for instance, in a directory wherefrom the script is running)?
P.S. class => WWW::Mechanize::File
Well, WWW::Mechanize::File has a save_as instance method, so I suppose something like this might work:
agent.get('http://example.com/foo.torrent').save_as 'a_file_name'
Please note that the Mechanize::File class is not the most appropriate for large files. In those cases, one should use the Mechanize::Download class instead, as it downloads the content in small chunks to disk. The file will be downloaded to where the script is running (although you can specify a different path as well). You need to set the default parser first, create a new one or modify an existing parser. Then, save it to the desired path:
agent.pluggable_parser.default = Mechanize::Download
agent.get( "http://example.com/foo.torrent}").save("path/to/a_file_name")
Check here and here for more details. Also, there's a similar question here in Stackoverflow.
Related
I'm using Tempfile to store a generated PDF before uploading to a new destination.
pdf_file = WickedPdf.new.pdf_from_string(msgbody)
tempfile = Tempfile.new(['Bob', '.pdf'], Rails.root.join('public','pdf-test'))
tempfile.binmode
tempfile.write pdf_file
tempfile.close
While this works fine, the resulting file names, eg- bob20140331-19260-1g6rzr1.pdf are not user friendly.
I understand that Tempfile creates a unique name and why, but I ultimately need to change the name to make it more intuitive/easier to digest for my users.
Is there a recommended way to do so? Even if its to simply remove the middle (19260)? Thanks for your time and assistance.
A Tempfile is used to create a temporary file with a unique file name, which will be cleaned up by the garbage collector or when the ruby interpreter exits.
Tempfiles behave like File objects, but I am not sure if you can rename files and if you can, if the automatic cleanup described above will still work. Additionally you might break the constraint of unique file names if you change the temporary file name manually.
I suggest creating an ordinary file and specify the entire name by yourself (the succ method can be helpful to prevent name clashes).
Another solution might be setting the file name during or after the upload process, you mentioned.
Note sure if there is one with Tempfile, but can you not rename the file after creation time via FileUtils module? That way you could achieve that the file that was created still has a valid and user-friendly name.
I had the impression that I should use require 'some_path' to get a file or a library I want to use, but I couldn't get it to work, and load 'some_path' worked.
When are the different times when I should use these?
Also, is it possible to import a directory with the load or require commmands? I am trying to use a whole directory, but so far I could only get one file at a time with load.
You can use Dir to list all the files ending with .rb and require/load them
Dir["/path/to/dir/*.rb"].each { |file| load_your_file_here(file) }
I recommend requiring file and then including the module that file loads... If you are not using module or class inside your file than maybe you should reconsider your structure.
load might have some unintended consequences and it's not performant.
Once you call require for a file further calls of require will no longer require it again(i.e. will have no effect), while load will reload it every time you call it. As far as I know there is no way to load a whole directory.
I have a simple FileCreator Ruby class that has 1 method create which creates a blank txt file on my desktop. Using RSpec, how would I test this create method to make sure
that the file was created, without having to create the file? Would I use RSpec::Mocks? Can someone please point me in the right directory? Thanks!
After calling file_creator.create(100) you could search the folder for all File*.txt files and make sure the count matches. (Make sure to have your spec remove the test files after completion).
Dir.glob(File.join(File.expand_path("~/Desktop"), "File*.txt")).length.should == 100
Using Mocks: You could do something like this to verify that the File.open method is actually being called (to test that the files actually get created, though, you may want to consider actually creating the files like the first half of my answer).
File.should_receive(:open).exactly(100).times
You could also try using something like FakeFS which mocks the actual file system.
The simplest way to do it is as below:
FileCreator.count.should eq 100
I am very new to Ruby so could you please suggest the best practice to separating files and including them.
What is the preferred design structure of the file layout. When do you decide to separate the algorithm into a new file?
When do you use load to include other files and when do you use require?
And is there a performance hit when you include files?
Thanks.
I make one file per class, except classes that are small helper classes, not needed by other files. I separate my different modules in subdirectories also.
The difference between load and require is require will only load the file once, even if it's called multiple times, while load will load it again regardless of whether it's been loaded before. You'll almost always want to use require, except maybe in irb when you want to manually want to reload a file.
I'm not sure on the performance hit. When you load or require a file, the interpreter has to interpret the file. Most Ruby's will compile it to virtual machine code after being required. Obviously, require is more performant when the file may have already been included once, because it may not have to load it again.
I'm writing some code that at run time may create or delete directories within the project path. I haven't really used ruby for file processing so i'm really uneasy about having code that, with a few mistypes weeks down the line, could result in wiping other directories outside of my project path.
Is there anyway to make it impossible for the program to delete files outside of its own path regardless of whats typed in destructive calls?
Pathname is a wrapper class for almost any file operations.
require "pathname"
path= Pathname.new("/home/johannes")
path.directory? # => true
path.children # => [#<Pathname:.bash_history>, #<Pathname:Documents>, #<Pathname:Desktop>]
path.children.each do |p|
p.delete if p.file?
end
Pathname#children does not contain . or .. so you don't accidently walk up the tree instead of down. If you still don't trust in the code, you can even check if on path is contained in another
Pathname.new("test") <=> Pathname.new("test/123") # => -1
You might want to create a wrapper method around your favourite delete method (or, perhaps, around whole class, because not only deleting files is potentially destructive file operation), which would expand all the submitted paths and check whether they begin with your "sandbox" path). You can also try to redefine delete method, if you are willing to cripple it through whole application.
And maybe the cleanest solution of them all would be to create a new user on your system and run your program as him.
On a POSIX system, you can use Dir.chroot to change the root that your application sees. Then ALL actions, not just delete ones, will be limited to the project directory. This does mean that external commands will be unavailable unless you make them part of your project directory as well.
This is the standard 'sandboxing' method used in Unix based systems. It can be difficult to setup (eliminating all external dependancies is sometimes hard), but affords significant protection when configured properly.
You could generate an Array of filenames in your project directory using
my_files = Dir["/bla/bla/your/directory/**/*"]
and then simply check if the filename passed to your "delete" function exist in your my_files array.
I'm sure there is a more elegant solution, but this could work ^_^
You could use File.expand_path and File.dirname on the input, and check that against __FILE__. So something like this might work:
File.delete(path) if File.dirname(File.expand_path(path)).include? File.dirname(File.expand_path(__FILE__))
I've got automated tests that routinely create and wipe out directories. I've taken two approaches:
Use /tmp as much as possible. The 'tmpdir' standard library module will create temporary directories which will be destroyed when your program exits. Or,
When the code creates a directory that it will later be deleting, it drops a marker file into the directory. When it comes time to delete the directory, if the marker file is not found, the code refuses to delete the directory. A marker file might be called ".ok_to_delete", for example.