Class: CalDAV::Client

Inherits:
Object
  • Object
show all
Includes:
Icalendar
Defined in:
lib/caldav/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Client

Returns a new instance of Client.



6
7
8
9
10
11
12
13
14
15
# File 'lib/caldav/client.rb', line 6

def initialize( *args )
    case args.length
    when 3
        __init_from_uri( *args )
    when 5
        __init_from_host_port( *args )
    else
        raise "#{self.class.to_s}: invalid number of arguments: #{args.length}"
    end
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



4
5
6
# File 'lib/caldav/client.rb', line 4

def host
  @host
end

#passwordObject

Returns the value of attribute password.



4
5
6
# File 'lib/caldav/client.rb', line 4

def password
  @password
end

#portObject

Returns the value of attribute port.



4
5
6
# File 'lib/caldav/client.rb', line 4

def port
  @port
end

#sslObject

Returns the value of attribute ssl.



4
5
6
# File 'lib/caldav/client.rb', line 4

def ssl
  @ssl
end

#urlObject

Returns the value of attribute url.



4
5
6
# File 'lib/caldav/client.rb', line 4

def url
  @url
end

#userObject

Returns the value of attribute user.



4
5
6
# File 'lib/caldav/client.rb', line 4

def user
  @user
end

Instance Method Details

#__create_httpObject



31
32
33
34
35
36
37
# File 'lib/caldav/client.rb', line 31

def __create_http
    http = Net::HTTP.new(@host, @port)
    http.use_ssl = @ssl
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    #http.set_debug_output $stderr
    http
end

#__init_from_host_port(host, port, url, user, password) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/caldav/client.rb', line 22

def __init_from_host_port( host, port, url, user, password )
   @host     = host
   @port     = port
   @url      = url
   @user     = user
   @password = password 
   @ssl      = port == 443
end

#__init_from_uri(suri, user, password) ⇒ Object



17
18
19
20
# File 'lib/caldav/client.rb', line 17

def __init_from_uri( suri, user, password )
    uri = URI.new( suri )
    puts "FIXME"
end

#add_alarm(tevent, altCal = "Calendar") ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/caldav/client.rb', line 102

def add_alarm tevent, altCal="Calendar"
#[#<Icalendar::Alarm:0x10b9d1b90 @name=\"VALARM\", @components={}, @properties={\"trigger\"=>\"-PT5M\", \"action\"=>\"DISPLAY\", \"description\"=>\"\"}>]    
    dtstart_string = ( Time.parse(tevent.dtstart.to_s) + Time.now.utc_offset.to_i.abs ).strftime "%Y%m%dT%H%M%S"
    dtend_string = ( Time.parse(tevent.dtend.to_s) + Time.now.utc_offset.to_i.abs ).strftime "%Y%m%dT%H%M%S"
    alarmText = <<EOL
BEGIN:VCALENDAR
VERSION:2.0
PRODID:Ruby iCalendar
BEGIN:VEVENT
UID:#{tevent.uid}
SUMMARY:#{tevent.summary}
DESCRIPTION:#{tevent.description}
DTSTART:#{dtstart_string}
DTEND:#{dtend_string}
BEGIN:VALARM
ACTION:DISPLAY
TRIGGER;RELATED=START:-PT5M
DESCRIPTION:Reminder
END:VALARM
BEGIN:VALARM
TRIGGER:-PT5M
ACTION:EMAIL
ATTENDEE:#{tevent.organizer}
SUMMARY:#{tevent.summary}
DESCRIPTION:#{tevent.description}
TRIGGER:-PT5M
END:VALARM
END:VEVENT
END:VCALENDAR
EOL
    p alarmText
    res = nil
    puts "#{@url}/#{tevent.uid}.ics"
    thttp = __create_http.start
    #thttp.set_debug_output $stderr
    req = Net::HTTP::Put.new("#{@url}/#{tevent.uid}.ics", initheader = {'Content-Type'=>'text/calendar'} )
    req.basic_auth @user, @password
    req.body = alarmText
    res = thttp.request( req )
    p res.inspect
    
    return tevent.uid
end

#create(event) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/caldav/client.rb', line 75

def create event
    nowstr = DateTime.now.strftime "%Y%m%dT%H%M%SZ"
    uuid   = UUID.generate
    dings  = """BEGIN:VCALENDAR
PRODID:Caldav.rb
VERSION:2.0
BEGIN:VEVENT
CREATED:#{nowstr}
UID:#{uuid}
SUMMARY:#{event.summary}
DTSTART:#{event.dtstart.strftime("%Y%m%dT%H%M%S")}
DTEND:#{event.dtend.strftime("%Y%m%dT%H%M%S")}
END:VEVENT
END:VCALENDAR"""

    res = nil
    http = Net::HTTP.new(@host, @port) 
    __create_http.start { |http|
        req = Net::HTTP::Put.new("#{@url}/#{uuid}.ics")
        req['Content-Type'] = 'text/calendar'
        req.basic_auth @user, @password
        req.body = dings
        res = http.request( req )
    }
    return uuid, res
end

#delete(uuid) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/caldav/client.rb', line 67

def delete uuid
    __create_http.start {|http|
        req = Net::HTTP::Delete.new("#{@url}/#{uuid}.ics")
        req.basic_auth @user, @password
        res = http.request( req )
    }
end

#filterTimezone(vcal) ⇒ Object



232
233
234
235
236
237
238
239
240
241
# File 'lib/caldav/client.rb', line 232

def filterTimezone( vcal )
    data = ""
    inTZ = false
    vcal.split("\n").each{ |l| 
        inTZ = true if l.index("BEGIN:VTIMEZONE") 
        data << l+"\n" unless inTZ 
        inTZ = false if l.index("END:VTIMEZONE") 
    }
    return data
end

#get(uuid) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/caldav/client.rb', line 55

def get uuid
    res = nil
    __create_http.start {|http|
        req = Net::HTTP::Get.new("#{@url}/#{uuid}.ics")
        req.basic_auth @user, @password
        res = http.request( req )
    }

    # FIXME: parse event/todo/vcard
    return parse_events( res.body ), res
end

#parse_events(vcal) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/caldav/client.rb', line 219

def parse_events( vcal )
    return_events = Array.new
    cals = Icalendar.parse(vcal)
    cals.each { |tcal|
        tcal.events.each { |tevent|
            if tevent.recurrence_id.to_s.empty? # skip recurring events
                return_events << tevent
            end
        }
    }
    return return_events
end

#parse_tasks(vcal) ⇒ Object



208
209
210
211
212
213
214
215
216
217
# File 'lib/caldav/client.rb', line 208

def parse_tasks( vcal )
    return_tasks = Array.new
    cals = Icalendar.parse(vcal)
    cals.each { |tcal|
        tcal.todos.each { |ttask|  # FIXME
            return_tasks << ttask
        }
    }
    return return_tasks
end

#report(start, stop) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/caldav/client.rb', line 39

def report start, stop
    res = nil
    __create_http.start {|http|
        req = Net::HTTP::Report.new(@url, initheader = {'Content-Type'=>'application/xml'} )
        req.basic_auth @user, @password
        req.body = CalDAV::Request::ReportVEVENT.new( start, stop ).to_xml
        res = http.request( req )
    }
    result = []
    xml = REXML::Document.new( res.body )
    REXML::XPath.each( xml, '//c:calendar-data/', { "c"=>"urn:ietf:params:xml:ns:caldav"} ){ |c|
        result += parse_events( c.text )
    }
    return result
end

#todoObject



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/caldav/client.rb', line 189

def todo 
    res = nil
    __create_http.start {|http|
        req = Net::HTTP::Report.new(@url, initheader = {'Content-Type'=>'application/xml'} )
        req.basic_auth @user, @password
        req.body = CalDAV::Request::ReportVTODO.new.to_xml
        res = http.request( req )
    }
    result = []
    xml = REXML::Document.new( res.body )
    #p res.body
    REXML::XPath.each( xml, '//c:calendar-data/', { "c"=>"urn:ietf:params:xml:ns:caldav"} ){ |c|
        p c.text
        p parse_tasks( c.text )
        result += parse_tasks( c.text )
    }
    return result
end

#update(event) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/caldav/client.rb', line 146

def update event
    dings = """BEGIN:VCALENDAR
PRODID:Caldav.rb
VERSION:2.0

BEGIN:VTIMEZONE
TZID:/Europe/Vienna
X-LIC-LOCATION:Europe/Vienna
BEGIN:DAYLIGHT
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
TZNAME:CEST
DTSTART:19700329T020000
RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=-1SU;BYMONTH=3
END:DAYLIGHT
BEGIN:STANDARD
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
TZNAME:CET
DTSTART:19701025T030000
RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=-1SU;BYMONTH=10
END:STANDARD
END:VTIMEZONE

BEGIN:VEVENT
CREATED:#{event.created}
UID:#{event.uid}
SUMMARY:#{event.summary}
DTSTART;TZID=Europe/Vienna:#{event.dtstart}
DTEND;TZID=Europe/Vienna:#{event.dtend.rfc3339}
END:VEVENT
END:VCALENDAR"""
    
    res = nil
    __create_http.start {|http|
        req = Net::HTTP::Put.new("#{@url}/#{event.uid}.ics", initheader = {'Content-Type'=>'text/calendar'} )
        req.basic_auth @user, @passowrd
        req.body = dings
        res = http.request( req )
    }
    return event.uid
end