Module: CsvInZip

Defined in:
lib/csv_in_zip.rb,
lib/csv_in_zip/version.rb

Defined Under Namespace

Classes: CsvFile

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.extract(target_zip, options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/csv_in_zip.rb', line 51

def extract(target_zip, options = {})
  CsvFile.new(options) do 
    zip_file = Zip::ZipFile.open(target_zip)
    Zip::ZipFile.foreach(target_zip) do |entry|
      next if CsvInZip.skip?(entry)
      
      add zip_file.read(entry)
    end
    
    to_csv.each{|row| yield row}
  end
end

.extract_each(target_zip, options = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/csv_in_zip.rb', line 64

def extract_each(target_zip, options = {})
  zip_file = Zip::ZipFile.open(target_zip)
  Zip::ZipFile.foreach(target_zip) do |entry|
    next if CsvInZip.skip?(entry)
    
    CsvFile.new(options) do 
      add zip_file.read(entry)
      csv = to_csv
      yield csv, csv.headers
    end
  end
end

.skip?(entry) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/csv_in_zip.rb', line 46

def skip?(entry)
  file_name = entry.to_s
  file_name =~ /^__MACOSX/ || File.extname(file_name) != ".csv"
end