class SabredavClient::Calendar

Attributes

client[RW]

Public Class Methods

new(data) click to toggle source
# File lib/sabredav_client/calendar.rb, line 6
def initialize(data)
  @client = SabredavClient::Client.new(data)
end

Public Instance Methods

create(displayname: "", description: "") click to toggle source
# File lib/sabredav_client/calendar.rb, line 31
def create(displayname: "", description: "")
  body    = SabredavClient::XmlRequestBuilder::Mkcalendar.new(displayname, description).to_xml
  header  = {dav: "resource-must-be-null", content_type: "application/xml"}

  req = client.create_request(:mkcalendar, header: header, body: body)

  res = req.run

  SabredavClient::Errors.errorhandling(res)
  info
end
delete() click to toggle source
# File lib/sabredav_client/calendar.rb, line 58
def delete
  req = client.create_request(:delete)
  res = req.run

  if res.code.to_i.between?(200,299)
    true
  else
    SabredavClient::Errors::errorhandling(res)
  end
end
events() click to toggle source
# File lib/sabredav_client/calendar.rb, line 10
def events
  @events ||= SabredavClient::Events.new(client)
end
fetch_changes(sync_token) click to toggle source
# File lib/sabredav_client/calendar.rb, line 134
def fetch_changes(sync_token)

  body    = SabredavClient::XmlRequestBuilder::ReportEventChanges.new(sync_token).to_xml
  header  = {content_type: "application/xml"}

  req     = client.create_request(:report, header: header, body: body)

  res     = req.run

  SabredavClient::Errors::errorhandling(res)

  changes   = []
  deletions = []
  xml = REXML::Document.new(res.body)

  REXML::XPath.each(xml, "//d:response/", {"d"=> "DAV:"}) do |response|
    entry = REXML::Document.new.add(response)
    if (REXML::XPath.first(entry, "//d:status").text == "HTTP/1.1 404 Not Found")
        deletions.push(
          REXML::XPath.first(entry, "//d:href").text.to_s.split("/").last)
    else
      uri  = REXML::XPath.first(entry, "//d:href").text.split("/").last
      etag = REXML::XPath.first(entry, "//d:getetag").text
      etag = %Q/#{etag.gsub(/\A['"]+|['"]+\Z/, "")}/ unless etag.nil?

      changes.push(
        {
          uri: uri,
          etag: etag
        })
    end
  end

  {
    changes: changes,
    deletions: deletions,
    sync_token: REXML::XPath.first(xml, "//d:sync-token").text
  }
end
fetch_sharees() click to toggle source
# File lib/sabredav_client/calendar.rb, line 89
def fetch_sharees
  body    = SabredavClient::XmlRequestBuilder::PropfindInvite.new.to_xml
  header  = {content_type: "application/xml", depth: "0"}

  req     = client.create_request(:propfind, header: header, body: body)

  res     = req.run

  SabredavClient::Errors::errorhandling(res)

  sharees   = []
  xml       = REXML::Document.new(res.body)

  REXML::XPath.each(xml, "//cs:user/", {"cs"=> "http://calendarserver.org/ns/"}) do |user|
    entry = REXML::Document.new.add(user)
    sharee = {
      href:           REXML::XPath.first(entry, "//d:href").text,
    }
    access          = REXML::XPath.first(entry, "//d:access").elements[1].to_s
    sharee[:access] = access.gsub(/\A[<cs:]+|[\/>]+\Z/, "")

    # So far Sabredav accepts every invite by default
    sharee[:status] = !REXML::XPath.first(entry, "//cs:invite-accepted").nil? ? :accepted : nil

    sharee[:common_name] = !REXML::XPath.first(entry, "//d:common-name").nil? ? REXML::XPath.first(entry, "//d:common-name").text : nil

    # URI depends on a custom plugin
    sharee[:uri] = !REXML::XPath.first(entry, "//cs:uri").nil? ? REXML::XPath.first(entry, "//cs:uri").text : nil

    # URI depends on a custom plugin
    sharee[:principal] = !REXML::XPath.first(entry, "//cs:principal").nil? ? REXML::XPath.first(entry, "//cs:principal").text : nil

    sharees.push(sharee)
  end

  {
    sharees: sharees,
    organizer: {
                href: REXML::XPath.first(xml, "//cs:organizer").elements[2].text,
                uri:  REXML::XPath.first(xml, "//cs:uri").text
              }
  }

end
info() click to toggle source
# File lib/sabredav_client/calendar.rb, line 14
def info
  header  = {content_type: "application/xml"}
  body    = SabredavClient::XmlRequestBuilder::PROPFINDCalendar.new(properties: [:displayname, :sync_token, :getctag]).to_xml

  req = client.create_request(:propfind, header: header, body: body)
  res = req.run

  SabredavClient::Errors::errorhandling(res)

  xml = REXML::Document.new(res.body)
  {
    displayname: REXML::XPath.first(xml, "//d:displayname").text,
    ctag: REXML::XPath.first(xml, "//cs:getctag").text,
    sync_token: REXML::XPath.first(xml, "//d:sync-token").text
  }
end
share(adds: [], removes: [], summary: nil, common_name: nil, privilege: "write-read", type: nil) click to toggle source
# File lib/sabredav_client/calendar.rb, line 69
def share(adds: [], removes: [], summary: nil, common_name: nil,
  privilege: "write-read", type: nil)

  header  = {content_length: "xxxx", content_type: "application/xml"}
  body    = SabredavClient::XmlRequestBuilder::PostSharing.new(
    adds, summary, common_name, privilege, removes).to_xml

  req = client.create_request(:post, header: header, body: body)

  res = req.run

  raise SabredavClient::Errors::ShareeTypeNotSupportedError if type && type != :email

  if res.code.to_i.between?(200,299)
    true
  else
    SabredavClient::Errors::errorhandling(res)
  end
end
update(displayname: nil, description: nil) click to toggle source
# File lib/sabredav_client/calendar.rb, line 43
def update(displayname: nil, description: nil)
  body = XmlRequestBuilder::ProppatchCalendar.new(displayname, description).to_xml
  header = {content_type: "application/xml"}

  req = client.create_request(:proppatch, header: header, body: body)

  res = req.run

  if res.code.to_i.between?(200,299)
    true
  else
    SabredavClient::Errors::errorhandling(res)
  end
end