Sha256: 48d9fb9b34a73b663bd7955fa05eb9dbbc72b51b5218dfebe4ade0e1ec79bca6

Contents?: true

Size: 950 Bytes

Versions: 5

Compression:

Stored size: 950 Bytes

Contents

# frozen_string_literal: true

require 'csv'
require 'zip'

module BridgeBlueprint
  class DataDump
    @path = nil

    def initialize(path)
      @path = path
      raise "File not found #{path}" unless File.exist?(path)
    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|
            next unless name.to_s == entry.name
            path = "#{dir}/#{entry.name}"
            file = entry.extract(path)
            break
          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

5 entries across 5 versions & 1 rubygems

Version Path
bridge_blueprint-0.0.10 lib/bridge_blueprint/data_dump.rb
bridge_blueprint-0.0.9 lib/bridge_blueprint/data_dump.rb
bridge_blueprint-0.0.8 lib/bridge_blueprint/data_dump.rb
bridge_blueprint-0.0.7 lib/bridge_blueprint/data_dump.rb
bridge_blueprint-0.0.06 lib/bridge_blueprint/data_dump.rb