wtnabe (owner)

Revisions

gist: 28482 Download_button fork
public
Public Clone URL: git://gist.github.com/28482.git
icat.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#! /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