Sha256: 4580f348648d7ef5d7316babbd0b457868078d91a3e2d645d09dff692942f7fd

Contents?: true

Size: 1.1 KB

Versions: 10

Compression:

Stored size: 1.1 KB

Contents

module Dap
module Input

  require 'oj'
  
  #
  # Error codes for failed reads
  # 
  module Error
    EOF   = :eof
    Empty = :empty
  end

  module FileSource
    
    attr_accessor :fd

    def open(file_name)
      close
      self.fd = ['-', 'stdin', nil].include?(file_name) ? 
        $stdin : ::File.open(file_name, "rb")
    end

    def close
      self.close if self.fd
      self.fd = nil
    end
  end

  #
  # Line Input
  #
  class InputLines
    
    include FileSource

    def initialize(args)
      self.open(args.first)
    end

    def read_record
      line = self.fd.readline rescue nil
      return Error::EOF unless line
      { 'line' => line.chomp("\n") }
    end

  end

  #
  # JSON Input (line-delimited records)
  #
  class InputJSON
    
    include FileSource

    def initialize(args)
      self.open(args.first)
    end

    def read_record  
      line = self.fd.readline rescue nil
      return Error::EOF unless line
      json = Oj.load(line.strip) rescue nil
      return Error::Empty unless json
      json
    end

  end

end
end

require 'dap/input/warc'
require 'dap/input/csv'

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
dap-0.0.10 lib/dap/input.rb
dap-0.0.9 lib/dap/input.rb
dap-0.0.8 lib/dap/input.rb
dap-0.0.7 lib/dap/input.rb
dap-0.0.6 lib/dap/input.rb
dap-0.0.5 lib/dap/input.rb
dap-0.0.4 lib/dap/input.rb
dap-0.0.3 lib/dap/input.rb
dap-0.0.2 lib/dap/input.rb
dap-0.0.1 lib/dap/input.rb