Sha256: 28d5b5326ac66aae9bc000854b575c53cd53a73b0ae2ba682a0e776aed29a719

Contents?: true

Size: 1.91 KB

Versions: 4

Compression:

Stored size: 1.91 KB

Contents

require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'fathom'))
require 'import'
require 'open-uri'

# TODO: Work this out with Ruby 1.9.  We don't need to load this for all Rubies.
require 'fastercsv'
require 'ext/faster_csv'
 
module Fathom
  class CSVImport < Import
    
    # @content could be a filename, URI, or actual file contents.  We figure out which 
    # it is and then parse the contents with FasterCSV.  We assume that there are column 
    # headers that are the names of each node, and that the values in the node are
    # values for a DataNode.  
    def import_csv
      parsed = parse_contents
      extracted = extract_columns(parsed)
      [DataNode, extracted]
    end
    
    # These are the options we use to parse the file.  They can be overriden 
    # by calling CSVImport.new(:parse_options => {...}, ...)
    def parse_options
      return @parse_options if @parse_options
      @parse_options = @options[:parse_options]
      @parse_options ||= {:converters => [:all], :headers => true, :skip_blanks => true, :header_converters => [:strip, :symbol]}
      @parse_options
    end
    
    protected
      def parse_contents
          arr_of_arrs = FasterCSV.parse(get_contents, parse_options)
      end
      
      # Tries to read a file or URL.  That failing, assumes the contents are CSV contents.
      def get_contents
        begin
          content = open(@content).read
        rescue
          content = @content
        end
        return content
      end
      
      # Returns an array of hashes with :name => row header, :values => values
      def extract_columns(parsed)
        transposed = parsed.to_a.transpose
        transposed.inject([]) do |list, column|
          list << {:name => column.shift, :values => column}
        end
      end
        
  end
end

if __FILE__ == $0
  include Fathom
  # TODO: Is there anything you want to do to run this file on its own?
  # CSV.new
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
fathom-0.2.3 lib/fathom/import/csv_import.rb
fathom-0.2.2 lib/fathom/import/csv_import.rb
fathom-0.2.1 lib/fathom/import/csv_import.rb
fathom-0.2.0 lib/fathom/import/csv_import.rb