Class: Async::Caldav::Client::Calendar

Inherits:
Object
  • Object
show all
Defined in:
lib/async/caldav/client/calendar.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, path, props = {}) ⇒ Calendar

Returns a new instance of Calendar.



11
12
13
14
15
16
17
18
# File 'lib/async/caldav/client/calendar.rb', line 11

def initialize(client, path, props = {})
  @client = client
  @path = path
  @displayname = props[:displayname]
  @description = props[:description]
  @color = props[:color]
  @ctag = props[:ctag]
end

Instance Attribute Details

#colorObject (readonly)

Returns the value of attribute color.



9
10
11
# File 'lib/async/caldav/client/calendar.rb', line 9

def color
  @color
end

#ctagObject (readonly)

Returns the value of attribute ctag.



9
10
11
# File 'lib/async/caldav/client/calendar.rb', line 9

def ctag
  @ctag
end

#descriptionObject (readonly)

Returns the value of attribute description.



9
10
11
# File 'lib/async/caldav/client/calendar.rb', line 9

def description
  @description
end

#displaynameObject (readonly)

Returns the value of attribute displayname.



9
10
11
# File 'lib/async/caldav/client/calendar.rb', line 9

def displayname
  @displayname
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/async/caldav/client/calendar.rb', line 9

def path
  @path
end

Instance Method Details

#deleteObject



88
89
90
91
92
93
94
95
96
# File 'lib/async/caldav/client/calendar.rb', line 88

def delete
  status, = @client.request('DELETE', @path)

  case status
  when 204 then true
  when 404 then raise NotFound, "Not found: #{@path}"
  else raise Error, "DELETE failed: #{status}"
  end
end

#delete_event(filename) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/async/caldav/client/calendar.rb', line 77

def delete_event(filename)
  path = "#{@path}#{filename}"
  status, = @client.request('DELETE', path)

  case status
  when 204 then true
  when 404 then raise NotFound, "Not found: #{path}"
  else raise Error, "DELETE failed: #{status}"
  end
end

#events(filter: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/async/caldav/client/calendar.rb', line 20

def events(filter: nil)
  x = Builder::XmlMarkup.new
  x.instruct! :xml, version: "1.0", encoding: "UTF-8"
  x.tag!("c:calendar-query", "xmlns:d" => "DAV:", "xmlns:c" => "urn:ietf:params:xml:ns:caldav") do
    x.tag!("d:prop") { x.tag!("d:getetag"); x.tag!("c:calendar-data") }
    if filter
      x << filter
    end
  end
  body = x.target!

  status, _, resp_body = @client.request('REPORT', @path, body: body, headers: { 'Content-Type' => 'text/xml' })
  unless status == 207
    raise Error, "REPORT failed: #{status}"
  end

  @client.parse_multistatus_items(resp_body, data_tag: 'calendar-data')
end

#get_event(filename) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/async/caldav/client/calendar.rb', line 63

def get_event(filename)
  path = "#{@path}#{filename}"
  status, headers, body = @client.request('GET', path)

  case status
  when 200
    { path: path, body: body, etag: headers['etag'], content_type: headers['content-type'] }
  when 404
    raise NotFound, "Not found: #{path}"
  else
    raise Error, "GET failed: #{status}"
  end
end

#propfindObject



98
99
100
101
102
103
104
# File 'lib/async/caldav/client/calendar.rb', line 98

def propfind
  status, _, body = @client.request('PROPFIND', @path, headers: { 'Depth' => '0' })
  unless status == 207
    raise Error, "PROPFIND failed: #{status}"
  end
  @client.parse_collection_props(body)
end

#proppatch(displayname: nil, description: nil, color: nil) ⇒ Object



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
# File 'lib/async/caldav/client/calendar.rb', line 106

def proppatch(displayname: nil, description: nil, color: nil)
  x = Builder::XmlMarkup.new
  x.instruct! :xml, version: "1.0", encoding: "UTF-8"
  x.tag!("d:propertyupdate", "xmlns:d" => "DAV:", "xmlns:c" => "urn:ietf:params:xml:ns:caldav", "xmlns:x" => "http://apple.com/ns/ical/") do
    x.tag!("d:set") do
      x.tag!("d:prop") do
        if displayname
          x.tag!("d:displayname", displayname)
        end
        if description
          x.tag!("c:calendar-description", description)
        end
        if color
          x.tag!("x:calendar-color", color)
        end
      end
    end
  end

  status, = @client.request('PROPPATCH', @path, body: x.target!, headers: { 'Content-Type' => 'text/xml' })
  unless status == 207
    raise Error, "PROPPATCH failed: #{status}"
  end

  if displayname
    @displayname = displayname
  end
  if description
    @description = description
  end
  if color
    @color = color
  end
  self
end

#put_event(filename, body, if_match: nil, if_none_match: nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/async/caldav/client/calendar.rb', line 39

def put_event(filename, body, if_match: nil, if_none_match: nil)
  headers = { 'Content-Type' => 'text/calendar' }
  if if_match
    headers['If-Match'] = if_match
  end
  if if_none_match
    headers['If-None-Match'] = if_none_match
  end

  path = "#{@path}#{filename}"
  status, resp_headers, = @client.request('PUT', path, body: body, headers: headers)

  case status
  when 201, 204
    { path: path, etag: resp_headers['etag'], status: status }
  when 412
    raise PreconditionFailed, "Precondition failed for #{path}"
  when 409
    raise Conflict, "UID conflict for #{path}"
  else
    raise Error, "PUT failed: #{status}"
  end
end

#sync(token: nil) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/async/caldav/client/calendar.rb', line 142

def sync(token: nil)
  x = Builder::XmlMarkup.new
  x.instruct! :xml, version: "1.0", encoding: "UTF-8"
  x.tag!("d:sync-collection", "xmlns:d" => "DAV:") do
    x.tag!("d:prop") { x.tag!("d:getetag") }
    if token
      x.tag!("d:sync-token", token)
    else
      x.tag!("d:sync-token")
    end
  end
  body = x.target!

  status, _, resp_body = @client.request('REPORT', @path, body: body, headers: { 'Content-Type' => 'text/xml' })

  if status == 403
    raise InvalidSyncToken, "Invalid sync token"
  end
  unless status == 207
    raise Error, "REPORT failed: #{status}"
  end

  new_token = resp_body.match(/<[^>]*sync-token[^>]*>([^<]+)</)[1] rescue nil
  items = @client.parse_sync_items(resp_body)
  [items, new_token]
end