Class: SimpleDav::AddressBook

Inherits:
Base
  • Object
show all
Defined in:
lib/simple_dav/address_book.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#client, #headers, #uri

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#gen_uid

Constructor Details

#initialize(params) ⇒ AddressBook

Returns a new instance of AddressBook.



4
5
6
7
8
9
10
11
12
# File 'lib/simple_dav/address_book.rb', line 4

def initialize(params)
  abu = "personal"
  @vcard = nil
  super(params)
  @abu = @type == "sogo" ? "Contacts/#{abu}/" : "#{abu}"
  @uri.path += @abu
  @group = nil #store group uid
  Card.adb = self
end

Instance Attribute Details

#groupObject (readonly)

Returns the value of attribute group.



3
4
5
# File 'lib/simple_dav/address_book.rb', line 3

def group
  @group
end

Class Method Details

.find(uid) ⇒ Object

find addresse resource by uid



85
86
87
# File 'lib/simple_dav/address_book.rb', line 85

def self.find(uid)
  Card.find(self, uid)
end

Instance Method Details

#change_group(abu) ⇒ Object

select address book



15
16
17
18
19
20
21
22
23
# File 'lib/simple_dav/address_book.rb', line 15

def change_group(abu)
  #todo select uid or nil if personnal
  # old style folders
  #@uri.path -= @abu
  #@uri.path += (@abu = abu)
  # new style v4 group
  groups = Card.where({"X-ADDRESSBOOKSERVER-KIND" => "group", "f" => abu})
  @group = groups && groups.first && groups.first.uid
end

#create(name, description = "") ⇒ Object

create address book group



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/simple_dav/address_book.rb', line 124

def create(name, description = "")
  uid = "#{gen_uid}"

  @vcard = Card.new(GROUPVCF)
  @vcard.add_attribute("X-ADDRESSBOOKSERVER-KIND", "group")
  @vcard.add_attribute("rev", Time.now.utc.iso8601(2))
  @vcard.add_attribute("uid", uid)
  @vcard.add_attribute(:f, name)
  @vcard.add_attribute(:fn, description)

  headers = {
    "If-None-Match" => "*",
    "Content-Type" => "text/vcard",
    "Content-Length" => @vcard.to_s.size
    }

  unc = @uri.clone
  unc.path += "#{uid}.vcf"
  res = @client.request('PUT', unc, nil, @vcard.to_s, headers)

  if res.status < 200 or res.status >= 300
    @uid = nil
    raise "create failed: #{res.inspect}"
  else
    @uid = @group = uid
  end
  @vcard

end

#create_folder(name, displayname = "", description = "") ⇒ Object

create collection : not working actually



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/simple_dav/address_book.rb', line 90

def create_folder(name, displayname = "", description = "")
  query = Nokogiri::XML::Builder.new(:encoding => "UTF-8") do |xml|
    xml.send('D:mkcol', 'xmlns:D' => "DAV:", 'xmlns:C' => "urn:ietf:params:xml:ns:carddav") do
      xml.send('D:set') do
        xml.send('D:prop')
        xml.send('D:resourcetype') do
          xml.send('D:collection')
          xml.send('C:addressbook')
        end
      end
      xml.send('D:displayname') do
        xml << displayname
      end
      xml.send('C:addressbook-description', 'xml:lang' => "en") do
        xml << description
      end
    end
  end

  headers = {
    "content-Type" => "text/xml; charset=\"utf-8\"",
    "Content-Length" => query.to_xml.to_s.size
    }

  res = @client.request('MKCOL', @uri, nil, query.to_xml.to_s, headers)

  if res.status < 200 or res.status >= 300
    raise "create failed: #{res.inspect}"
  else
    true
  end
end

#debug_dev=(dev) ⇒ Object



159
160
161
# File 'lib/simple_dav/address_book.rb', line 159

def debug_dev=(dev)
  @client.debug_dev = dev
end

#list(name) ⇒ Object

list available address books find all X-ADDRESSBOOKSERVER-KIND



27
28
29
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
76
77
78
79
80
81
82
# File 'lib/simple_dav/address_book.rb', line 27

def list(name)
  #Card.where("X-ADDRESSBOOKSERVER-KIND" => "group")
  if name
    query = Nokogiri::XML::Builder.new(:encoding => "UTF-8") do |xml|
      #xml.send('B:addressbook-query', 'xmlns:B' => "urn:ietf:params:xml:ns:carddav") do
        xml.send('D:principal-property-search', 'xmlns:D' => "DAV:") do
          xml.send('D:property-search') do
            xml.send('D:prop') do
              xml.send('D:displayname')
            end
            xml.send('D:match') do
              xml << name
            end
          end
          xml.send('D:prop') do
            xml.send('C:addressbook-home-set', 'xmlns:C' => 'urn:ietf:params:xml:ns:carddav')
            xml.send('D:displayname')
          end
      
        end
      
      #end

    end
  else
    query = Nokogiri::XML::Builder.new(:encoding => "UTF-8") do |xml|
      xml.send('A:propfind', 'xmlns:A' => 'DAV:') do
        xml.send('A:prop') do
          xml.send('D:addressbook-home-set', 'xmlns:D' => "urn:ietf:params:xml:ns:carddav")
          xml.send('D:directory-gateway', 'xmlns:D' => "urn:ietf:params:xml:ns:carddav")
          xml.send('A:displayname')
          xml.send('C:email-address-set', 'xmlns:C' => "http://calendarserver.org/ns/")
          xml.send('A:principal-collection-set')
          xml.send('A:principal-URL')
          xml.send('A:resource-id')
          xml.send('A:supported-report-set')
        end
      end
    end
  end

  headers = {
    "content-Type" => "text/xml; charset=\"utf-8\"",
    "depth" => 0,
    "brief" => "t",
    "Content-Length" => "#{query.to_xml.to_s.size}"
    }
  content = @client.request('REPORT', @uri, nil, query.to_xml.to_s, headers)
  puts content.body + "<<<<<\n\n"
  xml = Nokogiri::XML(content.body)
  vcards = []
  xml.xpath('//C:address-data').each do |card|
    vcards << Card.new(card.text)
  end
  return vcards
end

#vcardObject

access to current vcard object



155
156
157
# File 'lib/simple_dav/address_book.rb', line 155

def vcard
  @vcard
end