Skip to content

Instantly share code, notes, and snippets.

@wtnabe
Created November 24, 2008 14:48
Show Gist options
  • Save wtnabe/28482 to your computer and use it in GitHub Desktop.
Save wtnabe/28482 to your computer and use it in GitHub Desktop.
"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