Class: Fetchers::Tar

Inherits:
Object
  • Object
show all
Defined in:
lib/fetchers/tar.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Tar

Returns a new instance of Tar.



22
23
24
25
26
27
28
29
# File 'lib/fetchers/tar.rb', line 22

def initialize(target)
  @target = target
  @contents = {}
  @files = []
  Gem::Package::TarReader.new(Zlib::GzipReader.open(@target)) do |tar|
    @files = tar.map(&:full_name)
  end
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



13
14
15
# File 'lib/fetchers/tar.rb', line 13

def files
  @files
end

Class Method Details

.resolve(target) ⇒ Object



15
16
17
18
19
20
# File 'lib/fetchers/tar.rb', line 15

def self.resolve(target)
  unless target.is_a?(String) && File.file?(target) && target.end_with?('.tar.gz', '.tgz')
    return nil
  end
  new(target)
end

Instance Method Details

#read(file) ⇒ Object



31
32
33
# File 'lib/fetchers/tar.rb', line 31

def read(file)
  @contents[file] ||= read_from_tar(file)
end

#read_from_tar(file) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fetchers/tar.rb', line 35

def read_from_tar(file)
  return nil unless @files.include?(file)
  res = nil
  # NB `TarReader` includes `Enumerable` beginning with Ruby 2.x
  Gem::Package::TarReader.new(Zlib::GzipReader.open(@target)) do |tar|
    tar.each do |entry|
      next unless entry.file? && file == entry.full_name
      res = entry.read
      break
    end
  end
  res
end