class Berichtsheft::CSVParser

Implements the CSV parser.

Attributes

data[R]

Public Class Methods

new(csv_file) click to toggle source
# File lib/berichtsheft/parser.rb, line 6
def initialize(csv_file)
  @data = read_file(csv_file)
end

Public Instance Methods

parse_line(line) click to toggle source

Expected values for each line: date(YYYY-MM-DD),description,duration(HH-MM),id_for_week

# File lib/berichtsheft/parser.rb, line 33
def parse_line(line)
  entity = line.force_encoding(Encoding::UTF_8).split(",")
  Hash[
    "date" => entity[0],
    "description" => entity[1],
    "duration" => entity[2],
    "id_for_week" => entity[3].to_i
  ]
end
read_file(csv_file) click to toggle source
# File lib/berichtsheft/parser.rb, line 14
def read_file(csv_file)
  all_activities = []

  begin
    file = File.open(csv_file, "r")
    while line = file.gets
      all_activities.push parse_line(line)
    end
  rescue IOError => e
    puts e
  ensure
    file.close unless file.nil?
  end

  all_activities
end