Class: Async::Caldav::Client::Addressbook
- Inherits:
-
Object
- Object
- Async::Caldav::Client::Addressbook
- Defined in:
- lib/async/caldav/client/addressbook.rb
Instance Attribute Summary collapse
-
#displayname ⇒ Object
readonly
Returns the value of attribute displayname.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #contacts(filter: nil) ⇒ Object
- #delete ⇒ Object
- #delete_contact(filename) ⇒ Object
- #get_contact(filename) ⇒ Object
-
#initialize(client, path, props = {}) ⇒ Addressbook
constructor
A new instance of Addressbook.
- #proppatch(displayname: nil) ⇒ Object
- #put_contact(filename, body, if_match: nil, if_none_match: nil) ⇒ Object
Constructor Details
#initialize(client, path, props = {}) ⇒ Addressbook
Returns a new instance of Addressbook.
11 12 13 14 15 |
# File 'lib/async/caldav/client/addressbook.rb', line 11 def initialize(client, path, props = {}) @client = client @path = path @displayname = props[:displayname] end |
Instance Attribute Details
#displayname ⇒ Object (readonly)
Returns the value of attribute displayname.
9 10 11 |
# File 'lib/async/caldav/client/addressbook.rb', line 9 def displayname @displayname end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
9 10 11 |
# File 'lib/async/caldav/client/addressbook.rb', line 9 def path @path end |
Instance Method Details
#contacts(filter: nil) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/async/caldav/client/addressbook.rb', line 17 def contacts(filter: nil) x = Builder::XmlMarkup.new x.instruct! :xml, version: "1.0", encoding: "UTF-8" x.tag!("cr:addressbook-query", "xmlns:d" => "DAV:", "xmlns:cr" => "urn:ietf:params:xml:ns:carddav") do x.tag!("d:prop") { x.tag!("d:getetag"); x.tag!("cr:address-data") } if filter x << filter end end body = x.target! status, _, resp_body = @client.request('REPORT', @path, body: body, headers: { 'Content-Type' => 'text/xml' }) unless status == 207 raise Error, "REPORT failed: #{status}" end @client.parse_multistatus_items(resp_body, data_tag: 'address-data') end |
#delete ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/async/caldav/client/addressbook.rb', line 85 def delete status, = @client.request('DELETE', @path) case status when 204 then true when 404 then raise NotFound, "Not found: #{@path}" else raise Error, "DELETE failed: #{status}" end end |
#delete_contact(filename) ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/async/caldav/client/addressbook.rb', line 74 def delete_contact(filename) path = "#{@path}#{filename}" status, = @client.request('DELETE', path) case status when 204 then true when 404 then raise NotFound, "Not found: #{path}" else raise Error, "DELETE failed: #{status}" end end |
#get_contact(filename) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/async/caldav/client/addressbook.rb', line 60 def get_contact(filename) path = "#{@path}#{filename}" status, headers, body = @client.request('GET', path) case status when 200 { path: path, body: body, etag: headers['etag'], content_type: headers['content-type'] } when 404 raise NotFound, "Not found: #{path}" else raise Error, "GET failed: #{status}" end end |
#proppatch(displayname: nil) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/async/caldav/client/addressbook.rb', line 95 def proppatch(displayname: nil) x = Builder::XmlMarkup.new x.instruct! :xml, version: "1.0", encoding: "UTF-8" x.tag!("d:propertyupdate", "xmlns:d" => "DAV:") do x.tag!("d:set") do x.tag!("d:prop") do if displayname x.tag!("d:displayname", displayname) end end end end status, = @client.request('PROPPATCH', @path, body: x.target!, headers: { 'Content-Type' => 'text/xml' }) unless status == 207 raise Error, "PROPPATCH failed: #{status}" end if displayname @displayname = displayname end self end |
#put_contact(filename, body, if_match: nil, if_none_match: nil) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/async/caldav/client/addressbook.rb', line 36 def put_contact(filename, body, if_match: nil, if_none_match: nil) headers = { 'Content-Type' => 'text/vcard' } if if_match headers['If-Match'] = if_match end if if_none_match headers['If-None-Match'] = if_none_match end path = "#{@path}#{filename}" status, resp_headers, = @client.request('PUT', path, body: body, headers: headers) case status when 201, 204 { path: path, etag: resp_headers['etag'], status: status } when 412 raise PreconditionFailed, "Precondition failed for #{path}" when 409 raise Conflict, "UID conflict for #{path}" else raise Error, "PUT failed: #{status}" end end |