Module: Async::Caldav::Handlers::Mkcol

Defined in:
lib/async/caldav/handlers/mkcol.rb

Class Method Summary collapse

Class Method Details

.call(path:, body:, storage:, method: 'MKCOL', resource_type: nil) ⇒ Object

method: ‘MKCALENDAR’ or ‘MKCOL’



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
# File 'lib/async/caldav/handlers/mkcol.rb', line 14

def call(path:, body:, storage:, method: 'MKCOL', resource_type: nil, **)
  col_path = path.ensure_trailing_slash

  if storage.collection_exists?(col_path.to_s)
    return [405, { 'content-type' => 'text/plain' }, ['Collection already exists']]
  end

  unless col_path.parent_exists?
    return [409, { 'content-type' => 'text/plain' }, ['Parent collection does not exist']]
  end

  displayname = Protocol::Caldav::Xml.extract_value(body, 'displayname')
  description = Protocol::Caldav::Xml.extract_value(body, 'calendar-description')
  color = Protocol::Caldav::Xml.extract_value(body, 'calendar-color')

  type = if method == 'MKCALENDAR'
    :calendar
  elsif body && body.include?('addressbook')
    :addressbook
  else
    resource_type || :collection
  end

  storage.create_collection(col_path.to_s, type: type, displayname: displayname, description: description, color: color)
  [201, {}, ['']]
end