Object
Downloads the resource to the target.
The target may be a file name (string or task), in which case the file is created from the resource. The target may also be any object that responds to write, e.g. File, StringIO, Pipe.
Use the progress bar when running in verbose mode.
# File lib/contrib/uri_ext.rb, line 108 def download(target, options = {}) case target when String # If download breaks we end up with a partial file which is # worse than not having a file at all, so download to temporary # file and then move over. modified = File.stat(target).mtime if File.exist?(target) temp = nil Tempfile.open(File.basename(target)) do |tf| tf.binmode read(options.merge(:modified => modified)) { |chunk| tf.write chunk } temp = tf end FileUtils.mkpath(File.dirname(target)) FileUtils.move(temp.path, target) when File read(options.merge(:modified => target.mtime)) { |chunk| target.write chunk } target.flush else raise ArgumentError, "Expecting a target that is either a file name (string, task) or object that responds to write (file, pipe)." unless target.respond_to?(:write) read(options) { |chunk| target.write chunk } target.flush end end
Reads from the resource behind this URI. The first form returns the content of the resource, the second form yields to the block with each chunk of content (usually more than one).
For options, see URI::read.
# File lib/contrib/uri_ext.rb, line 95 def read(options = nil, &block) fail "This protocol doesn't support reading (yet, how about helping by implementing it?)" end
Writes to the resource behind the URI. The first form writes the content from a string or an object that responds to read and optionally size. The second form writes the content by yielding to the block. Each yield should return up to the specified number of bytes, the last yield returns nil.
For options, see URI::write.
# File lib/contrib/uri_ext.rb, line 142 def write(*args, &block) options = args.pop if Hash === args.last options ||= {} if String === args.first ios = StringIO.new(args.first, "r") write(options.merge(:size => args.first.size)) { |bytes| ios.read(bytes) } elsif args.first.respond_to?(:read) size = args.first.size rescue nil write({ :size => size }.merge(options)) { |bytes| args.first.read(bytes) } elsif args.empty? && block write_internal(options, &block) else raise ArgumentError, "Either give me the content, or pass me a block, otherwise what would I upload?" end end
Returns the proxy server to use. Obtains the proxy from the relevant environment variable (e.g. HTTP_PROXY). Supports exclusions based on host name and port number from environment variable NO_PROXY.
# File lib/contrib/uri_ext.rb, line 216 def proxy_uri() proxy = ENV["#{scheme.upcase}_PROXY"] proxy = URI.parse(proxy) if String === proxy excludes = (ENV["NO_PROXY"] || "").split(/\s*,\s*/).compact excludes = excludes.map { |exclude| exclude =~ /:\d+$/ ? exclude : "#{exclude}:*" } return proxy unless excludes.any? { |exclude| File.fnmatch(exclude, "#{host}:#{port}") } end
Generated with the Darkfish Rdoc Generator 2.