Class: Tilia::CardDav::Card

Inherits:
Dav::File show all
Includes:
ICard, DavAcl::IAcl
Defined in:
lib/tilia/card_dav/card.rb

Overview

The Card object represents a single Card from an addressbook

Instance Method Summary collapse

Methods included from Dav::INode

#name=

Methods inherited from Dav::Node

#name=

Constructor Details

#initialize(carddav_backend, address_book_info, card_data) ⇒ Card

Constructor

Parameters:

  • Backend\BackendInterface

    carddav_backend

  • array

    address_book_info

  • array

    card_data



32
33
34
35
36
# File 'lib/tilia/card_dav/card.rb', line 32

def initialize(carddav_backend, address_book_info, card_data)
  @carddav_backend = carddav_backend
  @address_book_info = address_book_info
  @card_data = card_data
end

Instance Method Details

#aclObject

Returns a list of ACE’s for this node.

Each ACE has the following properties:

* 'privilege', a string such as {DAV:}read or {DAV:}write. These are
  currently the only supported privileges
* 'principal', a url to the principal who owns the node
* 'protected' (optional), indicating that this ACE is not allowed to
   be updated.

Returns:

  • array



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/tilia/card_dav/card.rb', line 150

def acl
  # An alternative acl may be specified through the cardData array.
  return @card_data['acl'] if @card_data.key?('acl')

  [
    {
      'privilege' => '{DAV:}read',
      'principal' => @address_book_info['principaluri'],
      'protected' => true
    },
    {
      'privilege' => '{DAV:}write',
      'principal' => @address_book_info['principaluri'],
      'protected' => true
    }
  ]
end

#acl=(_acl) ⇒ Object

Updates the ACL

This method will receive a list of new ACE’s.

Parameters:

  • array

    acl

Returns:

  • void



174
175
176
# File 'lib/tilia/card_dav/card.rb', line 174

def acl=(_acl)
  fail Dav::Exception::MethodNotAllowed, 'Changing ACL is not yet supported'
end

#content_typeObject

Returns the mime content-type

Returns:

  • string



83
84
85
# File 'lib/tilia/card_dav/card.rb', line 83

def content_type
  'text/vcard; charset=utf-8'
end

#deleteObject

Deletes the card

Returns:

  • void



76
77
78
# File 'lib/tilia/card_dav/card.rb', line 76

def delete
  @carddav_backend.delete_card(@address_book_info['id'], @card_data['uri'])
end

#etagObject

Returns an ETag for this object

Returns:

  • string



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/tilia/card_dav/card.rb', line 90

def etag
  if @card_data.key?('etag')
    return @card_data['etag']
  else
    data = get
    if data.is_a?(String)
      return "\"#{Digest::MD5.hexdigest(data)}\""
    else
      # We refuse to calculate the md5 if it's a stream.
      return nil
    end
  end
end

#getObject

Returns the VCard-formatted object

Returns:

  • string



48
49
50
51
52
53
54
# File 'lib/tilia/card_dav/card.rb', line 48

def get
  # Pre-populating 'carddata' is optional. If we don't yet have it
  # already, we fetch it from the backend.
  @card_data = @carddav_backend.card(@address_book_info['id'], @card_data['uri']) unless @card_data.key?('carddata')

  @card_data['carddata']
end

#groupObject

Returns a group principal

This must be a url to a principal, or null if there’s no owner

Returns:

  • string|null



136
137
138
# File 'lib/tilia/card_dav/card.rb', line 136

def group
  nil
end

#last_modifiedObject

Returns the last modification date as a unix timestamp

Returns:

  • int



107
108
109
# File 'lib/tilia/card_dav/card.rb', line 107

def last_modified
  @card_data.key?('lastmodified') ? @card_data['lastmodified'] : nil
end

#nameObject

Returns the uri for this object

Returns:

  • string



41
42
43
# File 'lib/tilia/card_dav/card.rb', line 41

def name
  @card_data['uri']
end

#ownerObject

Returns the owner principal

This must be a url to a principal, or null if there’s no owner

Returns:

  • string|null



127
128
129
# File 'lib/tilia/card_dav/card.rb', line 127

def owner
  @address_book_info['principaluri']
end

#put(card_data) ⇒ Object

Updates the VCard-formatted object

Parameters:

  • string

    card_data

Returns:

  • string|null



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/tilia/card_dav/card.rb', line 60

def put(card_data)
  card_data = card_data.read unless card_data.is_a?(String)

  # Converting to UTF-8, if needed
  card_data = Dav::StringUtil.ensure_utf8(card_data)

  etag = @carddav_backend.update_card(@address_book_info['id'], @card_data['uri'], card_data)
  @card_data['carddata'] = card_data
  @card_data['etag'] = etag

  etag
end

#sizeObject

Returns the size of this object in bytes

Returns:

  • int



114
115
116
117
118
119
120
# File 'lib/tilia/card_dav/card.rb', line 114

def size
  if @card_data.key?('size')
    return @card_data['size']
  else
    return get.size
  end
end

#supported_privilege_setObject

Returns the list of supported privileges for this node.

The returned data structure is a list of nested privileges. See SabreDAVACLPlugin::getDefaultSupportedPrivilegeSet for a simple standard structure.

If null is returned from this method, the default privilege set is used, which is fine for most common usecases.

Returns:

  • array|null



188
189
190
# File 'lib/tilia/card_dav/card.rb', line 188

def supported_privilege_set
  nil
end