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.



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

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.



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

def color
  @color
end

#descriptionObject (readonly)

Returns the value of attribute description.



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

def description
  @description
end

#displaynameObject (readonly)

Returns the value of attribute displayname.



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

def displayname
  @displayname
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#propsObject (readonly)

Returns the value of attribute props.



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

def props
  @props
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#build_propfind(xml) ⇒ Object



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

def build_propfind(xml)
  XmlBuilder.response(xml, href: @path.to_s) do
    XmlBuilder.propstat_ok(xml) do
      xml.tag!("d:resourcetype") do
        xml.tag!("d:collection")
        xml.tag!("c:calendar") if @type == :calendar
        xml.tag!("cr:addressbook") if @type == :addressbook
      end

      xml.tag!("d:displayname", @displayname) if @displayname
      xml.tag!("c:calendar-description", @description) if @description
      xml.tag!("x:calendar-color", @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
      )
      xml.tag!("cs:getctag", ctag)
      xml.tag!("d:sync-token", "http://caldav.local/sync/#{ctag}")

      if @type == :calendar
        xml.tag!("c:supported-calendar-component-set") do
          xml.tag!("c:comp", name: "VEVENT")
          xml.tag!("c:comp", name: "VTODO")
          xml.tag!("c:comp", name: "VJOURNAL")
        end
      end

      xml.tag!("d:current-user-privilege-set") do
        xml.tag!("d:privilege") { xml.tag!("d:read") }
        xml.tag!("d:privilege") { xml.tag!("d:write") }
        xml.tag!("d:privilege") { xml.tag!("d:all") }
      end

      @props.each do |key, value|
        xml.tag!(key, value)
      end
    end
  end
end

#build_propname(xml) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/protocol/caldav/collection.rb', line 76

def build_propname(xml)
  XmlBuilder.response(xml, href: @path.to_s) do
    XmlBuilder.propstat_ok(xml) do
      xml.tag!("d:resourcetype")
      xml.tag!("d:displayname") if @displayname
      xml.tag!("c:calendar-description") if @description
      xml.tag!("x:calendar-color") if @color
      xml.tag!("cs:getctag")
      xml.tag!("d:sync-token")
      xml.tag!("c:supported-calendar-component-set") if @type == :calendar
    end
  end
end

#build_xml(xml) ⇒ Object



90
91
92
# File 'lib/protocol/caldav/collection.rb', line 90

def build_xml(xml)
  build_propfind(xml)
end

#to_propfind_xmlObject



94
95
96
97
98
# File 'lib/protocol/caldav/collection.rb', line 94

def to_propfind_xml
  x = Builder::XmlMarkup.new
  build_propfind(x)
  x.target!
end

#to_propname_xmlObject



100
101
102
103
104
# File 'lib/protocol/caldav/collection.rb', line 100

def to_propname_xml
  x = Builder::XmlMarkup.new
  build_propname(x)
  x.target!
end

#update_attrs(updates) ⇒ Object



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

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