Class: Vmit::VFS::URI

Inherits:
Handler show all
Defined in:
lib/vmit/vfs.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Handler

from

Constructor Details

#initialize(location) ⇒ URI

Returns a new instance of URI.

Parameters:

  • base_url (String)

    Base location



64
65
66
67
68
69
# File 'lib/vmit/vfs.rb', line 64

def initialize(location)
  @base_uri = case location
    when ::URI then location
    else ::URI.parse(location.to_s)
  end
end

Class Method Details

.accept?(location) ⇒ Boolean

Whether this handler accepts the given location

Parameters:

  • uri (URI, String)

    location

Returns:

  • (Boolean)


55
56
57
58
59
60
61
# File 'lib/vmit/vfs.rb', line 55

def self.accept?(location)
  uri = case location
    when ::URI then location
    else ::URI.parse(location.to_s)
  end
  ['http', 'ftp'].include?(uri.scheme)
end

.open(loc, *rest, &block) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/vmit/vfs.rb', line 71

def self.open(loc, *rest, &block)
  uri = case loc
  when ::URI then loc
  else ::URI.parse(loc.to_s)
  end
  unless accept?(uri)
    raise ArgumentError.new('Only HTTP/FTP supported')
  end
  @pbar = nil
  @filename = File.basename(uri.path)
  ret = OpenURI.open_uri(uri.to_s,
    :content_length_proc => lambda { |t|
      if t && 0 < t
        @pbar = ProgressBar.new(@filename, t)
        @pbar.file_transfer_mode
      end
    },
    :progress_proc => lambda { |s|
      @pbar.set s if @pbar
    }, &block)
  @pbar = nil
  # So that the progress bar line get overwriten
  STDOUT.print "\r"
  STDOUT.flush
  ret
end

Instance Method Details

#open(loc, *rest, &block) ⇒ Object

Open a filename relative to the base location.

Parameters:

  • loc (String)

    Location to open. If a base URI was given for HTTP then the path will be relative to that



103
104
105
106
107
108
# File 'lib/vmit/vfs.rb', line 103

def open(loc, *rest, &block)
  uri = @base_uri.clone
  uri.path = File.join(uri.path, loc.to_s)

  Vmit::VFS::URI.open(uri, *rest, &block)
end