<commit_before>require 'json'
require 'csv'
require 'erb'

module Asciinurse
  module Chart

    Series = Struct::new :name, :data

    class CSVData
      def initialize(attrs, csv_content)
        @title = attrs['title']
        @type = attrs['type']
        @csv = CSV.parse(csv_content, :converters => :all)
        @has_id = @csv.first.first == '$ID'
        parse_data
      end

      def to_chart_json
        ERB.new(get_template).result binding
      end

      private

      def get_template(default = 'generic.json.erb')
        basedir = File.dirname __FILE__
        begin
          IO.read('%s/templates/%s.json.erb' % [basedir, @type])
        rescue
          IO.read('%s/templates/%s' % [basedir, default])
        end
      end

      def parse_data
        @series = @csv.collect do |row|
          if @has_id
            Series::new row.first, row[1..-1]
          else
            Series::new nil, row
          end
        end
      end

    end
  end
end<commit_msg>parse csv data correctly<commit_after>require 'json'
require 'csv'
require 'erb'

module Asciinurse
  module Chart

    class CSVData
      def initialize(attrs, csv_content)
        @title = attrs['title']
        @type = attrs['type']
        @csv = CSV.parse(csv_content, :converters => :all)
        @has_id = @csv.first.first == '$ID'
        @header = @has_id ? @csv.first[1..-1] : @csv.first
        parse_data
      end

      def to_chart_json
        ERB.new(get_template).result binding
      end

      private

      def get_template(default = 'generic.json.erb')
        basedir = File.dirname __FILE__
        begin
          IO.read('%s/templates/%s.json.erb' % [basedir, @type])
        rescue
          IO.read('%s/templates/%s' % [basedir, default])
        end
      end

      def parse_data
        @series = @csv[1..-1].collect do |row|
          if @has_id
            {
                name: row.first,
                data: row[1..-1]
            }
          else
            {
                data: row
            }
          end
        end
      end

    end
  end
end
{'fraction_non_alphanumeric': 0.11345141215892772, 'fraction_numerical': 0.003829583532790809, 'mean_word_length': 1.6027397260273972, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3098605', 'n_tokens_mistral': 677, 'n_tokens_neox': 657, 'n_words': 152}