Module: URI
- Defined in:
- lib/buildr/core/transports.rb
Overview
Not quite open-uri, but similar. Provides read and write methods for the resource represented by the URI. Currently supports reads for URI::HTTP and writes for URI::SFTP. Also provides convenience methods for downloads and uploads.
Defined Under Namespace
Classes: FILE, Generic, HTTP, NotFoundError, SFTP
Constant Summary collapse
- RW_CHUNK_SIZE =
How many bytes to read/write at once. Do not change without checking BUILDR-214 first.
128 * 1024
Class Method Summary collapse
-
.download(uri, target, options = nil) ⇒ Object
:call-seq: download(uri, target, options?).
-
.read(uri, options = nil, &block) ⇒ Object
:call-seq: read(uri, options?) => content read(uri, options?) { |chunk| … }.
-
.upload(uri, source, options = nil) ⇒ Object
:call-seq: upload(uri, source, options?).
-
.write(uri, *args, &block) ⇒ Object
:call-seq: write(uri, content, options?) write(uri, options?) { |bytes| .. }.
Class Method Details
.download(uri, target, options = nil) ⇒ Object
:call-seq:
download(uri, target, )
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.
75 76 77 78 |
# File 'lib/buildr/core/transports.rb', line 75 def download(uri, target, = nil) uri = URI.parse(uri.to_s) unless URI === uri uri.download target, end |
.read(uri, options = nil, &block) ⇒ Object
:call-seq:
read(uri, options?) => content
read(uri, 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 example:
File.open 'image.jpg', 'w' do |file|
URI.read('http://example.com/image.jpg') { |chunk| file.write chunk }
end
Shorter version:
File.open('image.jpg', 'w') { |file| file.write URI.read('http://example.com/image.jpg') }
Supported options:
-
:modified – Only download if file modified since this timestamp. Returns nil if not modified.
-
:progress – Show the progress bar while reading.
61 62 63 64 |
# File 'lib/buildr/core/transports.rb', line 61 def read(uri, = nil, &block) uri = URI.parse(uri.to_s) unless URI === uri uri.read , &block end |
.upload(uri, source, options = nil) ⇒ Object
:call-seq:
upload(uri, source, )
Uploads from source to the resource.
The source may be a file name (string or task), in which case the file is uploaded to the resource. The source may also be any object that responds to read
(and optionally size
), e.g. File, StringIO, Pipe.
Use the progress bar when running in verbose mode.
111 112 113 114 |
# File 'lib/buildr/core/transports.rb', line 111 def upload(uri, source, = nil) uri = URI.parse(uri.to_s) unless URI === uri uri.upload source, end |
.write(uri, *args, &block) ⇒ Object
:call-seq:
write(uri, content, options?)
write(uri, 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 example:
File.open 'killer-app.jar', 'rb' do |file|
write('sftp://localhost/jars/killer-app.jar') { |chunk| file.read(chunk) }
end
Or:
write 'sftp://localhost/jars/killer-app.jar', File.read('killer-app.jar')
Supported options:
-
:progress – Show the progress bar while reading.
97 98 99 100 |
# File 'lib/buildr/core/transports.rb', line 97 def write(uri, *args, &block) uri = URI.parse(uri.to_s) unless URI === uri uri.write *args, &block end |