Sha256: d3b4cc0c1c00d0e3478be50468a09260bfa9be5938796d982f8a2c601bf81ff9

Contents?: true

Size: 1.17 KB

Versions: 5

Compression:

Stored size: 1.17 KB

Contents

require 'pathname'

module Chronicle
  module ETL
    class FileExtractor < Chronicle::ETL::Extractor
      def extract
        if file?
          extract_file do |data, metadata|
            yield(data, metadata)
          end
        elsif directory?
          extract_from_directory do |data, metadata|
            yield(data, metadata)
          end
        end
      end

      def results_count
        if file?
          return 1
        else
          search_pattern = File.join(@options[:filename], '**/*.eml')
          Dir.glob(search_pattern).count
        end
      end

      private

      def extract_from_directory
        search_pattern = File.join(@options[:filename], '**/*.eml')
        filenames = Dir.glob(search_pattern)
        filenames.each do |filename|
          file = File.open(filename)
          yield(file.read, {filename: file})
        end
      end

      def extract_file
        file = File.open(@options[:filename])
        yield(file.read, {filename: @options[:filename]})
      end

      def directory?
        Pathname.new(@options[:filename]).directory?
      end

      def file?
        Pathname.new(@options[:filename]).file?
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
chronicle-etl-0.2.4 lib/chronicle/etl/extractors/file_extractor.rb
chronicle-etl-0.2.3 lib/chronicle/etl/extractors/file_extractor.rb
chronicle-etl-0.2.2 lib/chronicle/etl/extractors/file_extractor.rb
chronicle-etl-0.2.1 lib/chronicle/etl/extractors/file_extractor.rb
chronicle-etl-0.2.0 lib/chronicle/etl/extractors/file_extractor.rb