Skip to content

Instantly share code, notes, and snippets.

@davorb
Created August 31, 2011 20:17
Show Gist options
  • Save davorb/1184593 to your computer and use it in GitHub Desktop.
Save davorb/1184593 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby1.9.1
# -*- coding: iso-8859-1 -*-
require 'icalendar'
require 'date'
include Icalendar
module Csv2Ical
def self.create_ical(input_file_name)
cal = Calendar.new
events(input_file_name) do |data|
cal.event do
dtstart DateTime.parse "#{data[:date]} #{data[:timeBegin]}"
dtend DateTime.parse "#{data[:date]} #{data[:timeEnd]}"
description "#{data[:courseCode]}, #{data[:room]}"
summary "#{data[:courseName]} #{data[:type]}"
end
end
cal.to_ical
end
def self.events(file)
File.open(file, 'r') do |file|
file.each do |line|
date=line[/^2011-\d{2}-\d{2}/]
timeBegin=line[/#{date}, \d{2}:\d{2}/][/\d{2}:\d{2}/]
timeEnd=line[/#{timeBegin}, 2011-\d{2}-\d{2}, \d{2}:\d{2}/][/\d{2}:\d{2}$/]
courseCode=line[/#{date}, #{timeBegin}, #{date}, #{timeEnd}, [a-zA-Z]+\d{2,}/][/[a-zA-Z]+\d{2,}$/]
type=line[/#{date}, #{timeBegin}, #{date}, #{timeEnd}, #{courseCode}, \S+,/][/\S+,$/].gsub(/,/, '')
room=line[/#{date}, #{timeBegin}, #{date}, #{timeEnd}, #{courseCode}, #{type}, (.{1,4}:.{1,4},)+/][/([a-zA-Z]{1,4}:[0-9a-zA-Z]{1,4},)+$/]#.gsub(/,/, '')
courseName=line[/#{date}, #{timeBegin}, #{date}, #{timeEnd}, #{courseCode}, #{type}, .*#{room} (".+",|[^,]+)/][/(".+",|[^,]+)$/]
returnHash = Hash.new
returnHash[:date]=date
returnHash[:timeBegin]=timeBegin
returnHash[:timeEnd]=timeEnd
returnHash[:courseCode]=courseCode
returnHash[:type]=type
returnHash[:room]=room
returnHash[:courseName]=courseName
yield returnHash
end
end
end
end
puts Csv2Ical.create_ical("schema.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment