Class: Fetchers::Zip

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Zip

Returns a new instance of Zip.



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

def initialize(target)
  @target = target
  @contents = {}
  @files = []
  ::Zip::InputStream.open(@target) do |io|
    while (entry = io.get_next_entry)
      @files.push(entry.name.sub(%r{/+$}, ''))
    end
  end
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



12
13
14
# File 'lib/fetchers/zip.rb', line 12

def files
  @files
end

Class Method Details

.resolve(target) ⇒ Object



14
15
16
17
18
19
# File 'lib/fetchers/zip.rb', line 14

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

Instance Method Details

#read(file) ⇒ Object



32
33
34
# File 'lib/fetchers/zip.rb', line 32

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

#read_from_zip(file) ⇒ Object



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

def read_from_zip(file)
  return nil unless @files.include?(file)
  res = nil
  ::Zip::InputStream.open(@target) do |io|
    while (entry = io.get_next_entry)
      next unless file == entry.name
      res = io.read
      break
    end
  end
  res
end