Class: SimpleDav::Card
- Inherits:
-
Object
- Object
- SimpleDav::Card
- Defined in:
- lib/simple_dav/card.rb
Class Attribute Summary collapse
-
.adb ⇒ Object
Returns the value of attribute adb.
Class Method Summary collapse
-
.create(params) ⇒ Object
create address resource.
- .find(uid) ⇒ Object
-
.where(conditions) ⇒ Object
find where RoR style.
Instance Method Summary collapse
- #add_attribute(a, v) ⇒ Object
- #delete ⇒ Object
-
#initialize(text = BASEVCF) ⇒ Card
constructor
A new instance of Card.
- #method_missing(meth, *args, &block) ⇒ Object
- #retreive ⇒ Object
- #run_find_by_method(attrs, *args, &block) ⇒ Object
- #run_on_field(attrs, *args, &block) ⇒ Object
- #to_s ⇒ Object
- #update(params) ⇒ Object
- #update_attribute(a, v) ⇒ Object
Constructor Details
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/simple_dav/card.rb', line 120 def method_missing(meth, *args, &block) case meth.to_s when /^((n|email|title|nickname|tel|bday|fn|org|note|uid|X-ADDRESSBOOKSERVER-KIND)=?)$/ run_on_field($1, *args, &block) when /^find_by_(.+)$/ run_find_by_method($1, *args, &block) else super end end |
Class Attribute Details
.adb ⇒ Object
Returns the value of attribute adb.
5 6 7 |
# File 'lib/simple_dav/card.rb', line 5 def adb @adb end |
Class Method Details
.create(params) ⇒ Object
create address resource
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/simple_dav/card.rb', line 18 def self.create(params) @vcard = Card.new params.each do |k,v| @vcard.update_attribute(k,v) end headers = { "If-None-Match" => "*", "Content-Type" => "text/vcard", "Content-Length" => @vcard.to_s.size } uid = "#{adb.gen_uid}.vcf" @vcard.update_attribute(:uid, uid) if adb && adb.group @vcard.add_attribute("X-ADDRESSBOOKSERVER-MEMBER", "urn:uuid:#{adb.group}") end unc = adb.uri.clone unc.path += uid res = adb.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 = uid end @vcard end |
.find(uid) ⇒ Object
13 14 15 |
# File 'lib/simple_dav/card.rb', line 13 def self.find(uid) where(:uid => uid) end |
.where(conditions) ⇒ Object
find where RoR style
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/simple_dav/card.rb', line 148 def self.where(conditions) limit = nil #1 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('A:prop', 'xmlns:A' => "DAV:") do xml.send('A:getetag') xml.send('B:address-data') end #xml.send('C:filter', 'test' => "anyof") do xml.send('B:filter', 'test' => 'anyof') do conditions.each do |k,v| xml.send('B:prop-filter', 'test' => 'allof','name' => k.to_s) do #xml.send('C:text-match', 'collation' => "i;unicode-casemap", 'match-type' => "contains") do xml.send('B:text-match', 'collation' => "i;unicode-casemap", 'match-type' => "contains") do xml << v end end end end if limit xml.send('C:limit') do xml.send('C:nresults') do xml << "#{limit}" end end end end end headers = { "content-Type" => "text/xml; charset=\"utf-8\"", "depth" => 1, "Content-Length" => "#{query.to_xml.to_s.size}" } content = adb.client.request('REPORT', adb.uri, nil, query.to_xml.to_s, headers) xml = Nokogiri::XML(content.body) vcards = [] xml.xpath('//C:address-data').each do |card| vcards << Card.new(card.text) end return vcards end |
Instance Method Details
#add_attribute(a, v) ⇒ Object
116 117 118 |
# File 'lib/simple_dav/card.rb', line 116 def add_attribute(a, v) @plain["END:VCARD"] = "#{a.to_s.upcase}:#{v}\nEND:VCARD" end |
#delete ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/simple_dav/card.rb', line 73 def delete if @uid && Card.adb headers = { #"If-None-Match" => "*", "Content-Type" => "text/xml; charset=\"utf-8\"" } unc = adb.uri.clone unc.path += @uid res = adb.client.request('DELETE', unc, nil, nil, headers) if res.status < 200 or res.status >= 300 @uid = nil raise "delete failed: #{res.inspect}" else @uid = nil true end else raise "Failed : no connection or null id" end end |
#retreive ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/simple_dav/card.rb', line 96 def retreive path = "#{self.uid}.vcf" unc = adb.uri.clone unc.path += path res = adb.client.request('GET', unc) if res.status < 200 or res.status >= 300 @uid = nil raise "delete failed: #{res.inspect}" else @plain = res.body @uid = uid true end end |
#run_find_by_method(attrs, *args, &block) ⇒ Object
193 194 195 196 197 198 |
# File 'lib/simple_dav/card.rb', line 193 def run_find_by_method(attrs, *args, &block) attrs = attrs.split('_and_') attrs_with_args = [attrs, args].transpose conditions = Hash[attrs_with_args] where(conditions) end |
#run_on_field(attrs, *args, &block) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/simple_dav/card.rb', line 131 def run_on_field(attrs, *args, &block) field = attrs.upcase field["EMAIL"] = "EMAIL;TYPE=work" if field.match("EMAIL") if field =~ /=/ field = field[0..-2] update_attribute(field, args) else if m = @plain.match(/#{field}:(.+)$/) m[1] else nil end end end |
#to_s ⇒ Object
200 201 202 |
# File 'lib/simple_dav/card.rb', line 200 def to_s @plain.to_s end |
#update(params) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/simple_dav/card.rb', line 49 def update(params) params.each do |k,v| update_attribute(k,v) end headers = { "Content-Type" => "text/vcard", "Content-Length" => @plain.size } uid = self.uid unc = Card.adb.uri.clone unc.path += uid res = Card.adb.client.request('PUT', unc, nil, @plain, headers) if res.status < 200 or res.status >= 300 @uid = nil raise "create failed: #{res.inspect}" else @uid = uid end self end |
#update_attribute(a, v) ⇒ Object
112 113 114 |
# File 'lib/simple_dav/card.rb', line 112 def update_attribute(a, v) @plain.match(/^#{a.to_s.upcase}:(.+)$/) ? @plain.gsub!(/^#{a.to_s.upcase}:(.+)$/, "#{a.to_s.upcase}:#{v}") : add_attribute(a, v) end |