Class: URI::FILE
Overview
File URL. Keep in mind that file URLs take the form of file://host/path, although the host is not used, so typically all you will see are three backslashes. This methods accept common variants, like file:/path but always returns a valid URL.
Constant Summary collapse
- COMPONENT =
[ :host, :path ].freeze
Instance Method Summary collapse
-
#initialize(*args) ⇒ FILE
constructor
A new instance of FILE.
-
#read(options = nil, &block) ⇒ Object
See URI::Generic#read.
-
#real_path ⇒ Object
Returns the file system path based that corresponds to the URL path.
- #to_s ⇒ Object
- #upload(source, options = nil) ⇒ Object
Methods inherited from Generic
Constructor Details
#initialize(*args) ⇒ FILE
Returns a new instance of FILE.
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/buildr/core/transports.rb', line 389 def initialize(*args) super # file:something (opaque) becomes file:///something if path.nil? set_path "/#{opaque}" unless opaque.nil? set_opaque nil warn "#{caller[2]}: We'll accept this URL, but just so you know, it needs three slashes, as in: #{to_s}" end end # Sadly, file://something really means file://something/ (something being server) set_path '/' if path.empty? # On windows, file://c:/something is not a valid URL, but people do it anyway, so if we see a drive-as-host, # we'll just be nice enough to fix it. (URI actually strips the colon here) if host =~ /^[a-zA-Z]$/ set_path "/#{host}:#{path}" set_host nil end end |
Instance Method Details
#read(options = nil, &block) ⇒ Object
See URI::Generic#read
411 412 413 414 415 416 417 418 419 420 421 422 423 424 |
# File 'lib/buildr/core/transports.rb', line 411 def read( = nil, &block) ||= {} raise ArgumentError, 'Either you\'re attempting to read a file from another host (which we don\'t support), or you used two slashes by mistake, where you should have file:///<path>.' if host path = real_path # TODO: complain about clunky URLs raise NotFoundError, "Looking for #{self} and can't find it." unless ::File.exists?(path) raise NotFoundError, "Looking for the file #{self}, and it happens to be a directory." if ::File.directory?(path) ::File.open path, 'rb' do |input| [:progress], path.split('/').last, input.stat.size do |progress| block ? block.call(input.read) : input.read end end end |
#real_path ⇒ Object
Returns the file system path based that corresponds to the URL path. On all platforms this method unescapes the URL path.
432 433 434 |
# File 'lib/buildr/core/transports.rb', line 432 def real_path #:nodoc: CGI.unescape(path) end |
#to_s ⇒ Object
426 427 428 |
# File 'lib/buildr/core/transports.rb', line 426 def to_s "file://#{host}#{path}" end |