Sha256: 6e35ed3d954ba2c749701a0a1863276123859f0fd7379fee99141efa35579454

Contents?: true

Size: 957 Bytes

Versions: 2

Compression:

Stored size: 957 Bytes

Contents

require 'csv'
require 'zip'

module BridgeBlueprint
  class DataDump

    @path = nil

    def initialize(path)
      @path = path
      unless File.exists?(path)
        raise "File not found #{path}"
      end
    end

    def each_row(name)
      extract_from_zip("#{name}.csv") do |file|
        CSV.foreach(file, headers: true) do |row|
          yield(row)
        end
      end
    end

    private

    def extract_from_zip(name)
      Dir.mktmpdir do |dir|
        path = nil
        file = nil
        Zip::File.open(@path) do |zip_file|
          zip_file.each do |entry|
            if "#{name.to_s}" == entry.name
              path = "#{dir}/#{entry.name}"
              file = entry.extract(path)
              break
            end
          end
        end
        if file
          yield "#{dir}/#{name}" if block_given?
        else
          raise "File #{name} not found in zip archive #{@path}"
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bridge_blueprint-0.0.05 lib/bridge_blueprint/data_dump.rb
bridge_blueprint-0.0.04 lib/bridge_blueprint/data_dump.rb