Sha256: 03bae8df5930eb473eb0d5c58a320c63a2f8a96432497a7d4636d7c803c1c749

Contents?: true

Size: 1.17 KB

Versions: 2

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

2 entries across 2 versions & 1 rubygems

Version Path
chronicle-etl-0.1.4 lib/chronicle/etl/extractors/file_extractor.rb
chronicle-etl-0.1.3 lib/chronicle/etl/extractors/file_extractor.rb