Class: Bestchange::ZipExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/bestchange/zip_extractor.rb

Instance Method Summary collapse

Constructor Details

#initialize(zip_file, on_extract: nil) ⇒ ZipExtractor

Returns a new instance of ZipExtractor.



7
8
9
10
# File 'lib/bestchange/zip_extractor.rb', line 7

def initialize(zip_file, on_extract: nil)
  @zip_file = zip_file
  @on_extract = on_extract
end

Instance Method Details

#call(filename) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bestchange/zip_extractor.rb', line 12

def call(filename)
  tempfile = Tempfile.new(filename)

  Zip::File.open(@zip_file) do |zip_file|
    entry = zip_file.find_entry(filename)

    @on_extract&.call(entry)
    entry.extract(tempfile) { true }
  end

  tempfile.rewind

  if block_given?
    begin
      yield tempfile
    ensure
      tempfile.close
    end
  else
    tempfile
  end
end