Module: Solaris::Util
- Defined in:
- lib/solaris/util.rb
Overview
Utility module.
Class Method Summary collapse
-
.download!(url, opts = {}) ⇒ Object
Download the given URL, return the document body.
Class Method Details
.download!(url, opts = {}) ⇒ Object
Download the given URL, return the document body. Options:
:agent -- set HTTP user agent
:password -- Oracle support password
:to_file -- a file path to which the patch/readme should be saved
:to_dir -- a directory path to which the patch/readme should be saved
:user -- Oracle support username
(:to_dir and :to_file are mutually exclusive)
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/solaris/util.rb', line 17 def Util.download!(url, opts={}) agent = Mechanize.new dirname, filename = nil, nil opts.each do |key, value| case key when :agent agent.user_agent = value when :to_dir dirname = value when :to_file filename = value when :password, :user # noop else raise ArgumentError, "Unknown option key #{key.inspect}" end end # If we got a filename then open now before attempting download raise ArgumentError, 'Cannot specify both :to_dir and :to_file' if filename && dirname filename = File.join(dirname, File.basename(url)) if dirname begin file = File.open(filename, 'w') if filename # Set agent authentication parameters if opts[:user] && opts[:password] agent.basic_auth(opts[:user], opts[:password]) elsif opts[:user] raise ArgumentError, 'Cannot authenticate without a password' elsif opts[:password] raise ArgumentError, 'Cannot authenticate without a username' end # Download file and save as required page = agent.get(url) if file file.write(page.body) file.close end rescue => exception # Try to remove incomplete file on error if file begin file.close ; rescue ; end begin File.unlink(file) ; rescue ; end end raise exception # rethrow original exception end page.body # return file as string (even if written) end |