Sha256: 190fec496431ac2f9c7d63199cee7e0fdf34f87e356e1a912e06dcdc47c450e8

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 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'
 
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}
      @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

1 entries across 1 versions & 1 rubygems

Version Path
fathom-0.1.0 lib/fathom/import/csv_import.rb