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.
13 14 15 16 17 |
# File 'lib/async/caldav/client/addressbook.rb', line 13 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.
11 12 13 |
# File 'lib/async/caldav/client/addressbook.rb', line 11 def displayname @displayname end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
11 12 13 |
# File 'lib/async/caldav/client/addressbook.rb', line 11 def path @path end |
Instance Method Details
#contacts(filter: nil) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/async/caldav/client/addressbook.rb', line 19 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") } x << filter if filter end body = x.target! status, _, resp_body = @client.request('REPORT', @path, body: body, headers: { 'Content-Type' => 'text/xml' }) raise Error, "REPORT failed: #{status}" unless status == 207 @client.parse_multistatus_items(resp_body, data_tag: 'address-data') end |
#delete ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/async/caldav/client/addressbook.rb', line 79 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
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/async/caldav/client/addressbook.rb', line 68 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
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/async/caldav/client/addressbook.rb', line 54 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
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/async/caldav/client/addressbook.rb', line 89 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 x.tag!("d:displayname", displayname) if displayname end end end status, = @client.request('PROPPATCH', @path, body: x.target!, headers: { 'Content-Type' => 'text/xml' }) raise Error, "PROPPATCH failed: #{status}" unless status == 207 @displayname = displayname if displayname self end |
#put_contact(filename, body, if_match: nil, if_none_match: nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/async/caldav/client/addressbook.rb', line 34 def put_contact(filename, body, if_match: nil, if_none_match: nil) headers = { 'Content-Type' => 'text/vcard' } headers['If-Match'] = if_match if if_match headers['If-None-Match'] = if_none_match if if_none_match 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 |