Question

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

Was it helpful?

Solution

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'

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top