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.



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

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
# File 'lib/fetchers/tar.rb', line 15

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

Instance Method Details

#read(file) ⇒ Object



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

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

#read_from_tar(file) ⇒ Object



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

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