Class: Protocol::Caldav::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/protocol/caldav/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, type: :collection, displayname: nil, description: nil, color: nil, props: {}) ⇒ Collection

Returns a new instance of Collection.



13
14
15
16
17
18
19
20
# File 'lib/protocol/caldav/collection.rb', line 13

def initialize(path:, type: :collection, displayname: nil, description: nil, color: nil, props: {})
  @path = path
  @type = type
  @displayname = displayname
  @description = description
  @color = color
  @props = props || {}
end

Instance Attribute Details

#colorObject (readonly)

Returns the value of attribute color.



11
12
13
# File 'lib/protocol/caldav/collection.rb', line 11

def color
  @color
end

#descriptionObject (readonly)

Returns the value of attribute description.



11
12
13
# File 'lib/protocol/caldav/collection.rb', line 11

def description
  @description
end

#displaynameObject (readonly)

Returns the value of attribute displayname.



11
12
13
# File 'lib/protocol/caldav/collection.rb', line 11

def displayname
  @displayname
end

#pathObject (readonly)

Returns the value of attribute path.



11
12
13
# File 'lib/protocol/caldav/collection.rb', line 11

def path
  @path
end

#propsObject (readonly)

Returns the value of attribute props.



11
12
13
# File 'lib/protocol/caldav/collection.rb', line 11

def props
  @props
end

#typeObject (readonly)

Returns the value of attribute type.



11
12
13
# File 'lib/protocol/caldav/collection.rb', line 11

def type
  @type
end

Instance Method Details

#to_propfind_xmlObject



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
75
# File 'lib/protocol/caldav/collection.rb', line 30

def to_propfind_xml
  prop_lines = []

  if @type == :calendar
    prop_lines << '<d:resourcetype><d:collection/><c:calendar/></d:resourcetype>'
  elsif @type == :addressbook
    prop_lines << '<d:resourcetype><d:collection/><cr:addressbook/></d:resourcetype>'
  else
    prop_lines << '<d:resourcetype><d:collection/></d:resourcetype>'
  end

  prop_lines << "<d:displayname>#{Xml.escape(@displayname)}</d:displayname>" if @displayname
  prop_lines << "<c:calendar-description>#{Xml.escape(@description)}</c:calendar-description>" if @description
  prop_lines << "<x:calendar-color>#{Xml.escape(@color)}</x:calendar-color>" if @color

  item_etags = @path.storage_class.list_items(@path.to_s).map { |_, data| data[:etag] }
  ctag = CTag.compute(
    path: @path.to_s,
    displayname: @displayname,
    description: @description,
    color: @color,
    item_etags: item_etags
  )
  prop_lines << "<cs:getctag>#{ctag}</cs:getctag>"
  prop_lines << "<d:sync-token>http://caldav.local/sync/#{ctag}</d:sync-token>"

  if @type == :calendar
    prop_lines << '<c:supported-calendar-component-set><c:comp name="VEVENT"/><c:comp name="VTODO"/><c:comp name="VJOURNAL"/></c:supported-calendar-component-set>'
  end

  @props.each do |key, value|
    prop_lines << "<#{key}>#{Xml.escape(value)}</#{key}>"
  end

  <<~XML
    <d:response>
      <d:href>#{Xml.escape(@path.to_s)}</d:href>
      <d:propstat>
        <d:prop>
          #{prop_lines.join("\n          ")}
        </d:prop>
        <d:status>HTTP/1.1 200 OK</d:status>
      </d:propstat>
    </d:response>
  XML
end

#to_propname_xmlObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/protocol/caldav/collection.rb', line 77

def to_propname_xml
  names = ['<d:resourcetype/>']
  names << '<d:displayname/>' if @displayname
  names << '<c:calendar-description/>' if @description
  names << '<x:calendar-color/>' if @color
  names << '<cs:getctag/>'
  names << '<d:sync-token/>'
  names << '<c:supported-calendar-component-set/>' if @type == :calendar

  <<~XML
    <d:response>
      <d:href>#{Xml.escape(@path.to_s)}</d:href>
      <d:propstat>
        <d:prop>
          #{names.join("\n          ")}
        </d:prop>
        <d:status>HTTP/1.1 200 OK</d:status>
      </d:propstat>
    </d:response>
  XML
end

#update_attrs(updates) ⇒ Object



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

def update_attrs(updates)
  @displayname = updates[:displayname] if updates.key?(:displayname)
  @description = updates[:description] if updates.key?(:description)
  @color = updates[:color] if updates.key?(:color)
  @props = (@props || {}).merge(updates[:props]) if updates.key?(:props)
  self
end