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'



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/async/caldav/handlers/mkcol.rb', line 10

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

  if storage.collection_exists?(col_path.to_s)
    [405, { 'content-type' => 'text/plain' }, ['Collection already exists']]
  else
    if col_path.parent_exists?
      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')
      if method == 'MKCALENDAR'
        type = :calendar
      elsif body && body.include?('addressbook')
        type = :addressbook
      else
        type = resource_type || :collection
      end
      storage.create_collection(col_path.to_s, type: type, displayname: displayname, description: description, color: color)
      [201, {}, ['']]
    else
      [409, { 'content-type' => 'text/plain' }, ['Parent collection does not exist']]
    end
  end
end