Class: URI::Generic

Inherits:
Object show all
Defined in:
lib/pik/contrib/uri_ext.rb

Instance Method Summary collapse

Instance Method Details

#download(target, options = {}) ⇒ Object

:call-seq:

download(target, options?)

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.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/pik/contrib/uri_ext.rb', line 109

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
    result = nil
    Tempfile.open(File.basename(target)) do |tf|
      tf.binmode
      result = read(options.merge(:modified => modified)) { |chunk| tf.write chunk }
      temp = tf
    end
    FileUtils.mkpath(File.dirname(target))
    FileUtils.move(temp.path, target) if result
  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

#read(options = nil, &block) ⇒ Object

:call-seq:

read(options?) => content
read(options?) { |chunk| ... }

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.



96
97
98
# File 'lib/pik/contrib/uri_ext.rb', line 96

def read(options = nil, &block)
  fail "This protocol doesn't support reading (yet, how about helping by implementing it?)"
end

#write(*args, &block) ⇒ Object

:call-seq:

write(content, options?)
write(options?) { |bytes| .. }

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.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/pik/contrib/uri_ext.rb', line 144

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