Instantly share code, notes, and snippets.

Embed
What would you like to do?
"icat", just cat ical data written in Ruby
#! /usr/bin/env ruby
require 'rubygems'
require 'vpim/icalendar'
require 'open-uri'
require 'optparse'
$KCODE = 'u'
class Icat
def initialize
@ics = []
end
def run
opt = define_options
opt.parse!( ARGV )
takein_args( ARGV )
if ( @ics.size > 0 )
publish( fetch( @ics ) )
else
puts opt.help
end
end
def define_options
opt = OptionParser.new
opt.on( '-l', '--list-from-file FILE' ) { |f|
open( f ).each { |e|
@ics << e.chomp
}
}
opt.banner = "Usage: icat [options] ICS_FILES"
return opt
end
def takein_args( argv )
argv.each { |f|
@ics << f
}
end
def fetch( urls )
cals = []
if ( !urls.is_a?( Enumerable ) )
urls = [ urls ]
end
urls.each { |url|
Vpim::Icalendar::decode( open( url ).read ).each { |c|
cals << c
}
}
return cals
end
def publish( cals )
cal = Vpim::Icalendar.create
cals.each { |c|
c.events.each { |e|
cal << e
}
}
puts cal.to_s
end
end
if ( __FILE__ == $0 )
Icat.new.run
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment