Class: SabredavClient::Events

Inherits:
Object
  • Object
show all
Defined in:
lib/sabredav_client/events.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Events

Returns a new instance of Events.



6
7
8
# File 'lib/sabredav_client/events.rb', line 6

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



4
5
6
# File 'lib/sabredav_client/events.rb', line 4

def client
  @client
end

Instance Method Details

#create_update(uri, event_ics, etag = nil) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/sabredav_client/events.rb', line 90

def create_update(uri, event_ics, etag = nil)
  header  = {content_type: "text/calendar"}
  body    = event_ics

  if etag
    header[:if_match] = %Q/"#{etag.gsub(/\A['"]+|['"]+\Z/, "")}"/
  end

  req = client.create_request(:put,header: header, body: body, path: uri)
  res = req.run

  SabredavClient::Errors::errorhandling(res)
  etag = res['etag']
  %Q/#{etag.gsub(/\A['"]+|['"]+\Z/, "")}/ unless etag.nil?
end

#delete(uri) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/sabredav_client/events.rb', line 77

def delete(uri)
  raise SabredavClient::Errors::SabredavClientError if uri.nil? || !uri.end_with?(".ics")

  req = client.create_request(:delete, path: uri)
  res = req.run

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

#find(uri) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/sabredav_client/events.rb', line 10

def find(uri)
  req = client.create_request(:get, path: uri)
  res = req.run

  SabredavClient::Errors::errorhandling(res)

  etag = res.header["etag"]
  etag = %Q/#{etag.gsub(/\A['"]+|['"]+\Z/, "")}/ unless etag.nil?

  {
    ics: res.body,
    etag: etag
  }
end

#find_multiple(starts: "", ends: "") ⇒ Object



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/sabredav_client/events.rb', line 25

def find_multiple(starts: "", ends: "")
  events  = []
  header  = {depth: "1", content_type: "application/xml"}

  if starts.is_a? Integer
    body = SabredavClient::XmlRequestBuilder::ReportVEVENT.new(Time.at(starts).utc.strftime("%Y%m%dT%H%M%S"),
                                                  Time.at(ends).utc.strftime("%Y%m%dT%H%M%S") ).to_xml
  else
    body = SabredavClient::XmlRequestBuilder::ReportVEVENT.new(Time.parse(starts).utc.strftime("%Y%m%dT%H%M%S"),
                                                  Time.parse(ends).utc.strftime("%Y%m%dT%H%M%S") ).to_xml
  end

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

  SabredavClient::Errors::errorhandling(res)
  result = ""

  xml = REXML::Document.new(res.body)
  REXML::XPath.each( xml, '//c:calendar-data/', {"c"=>"urn:ietf:params:xml:ns:caldav"} ){|c| result << c.text}

  result
end

#owner(uri) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sabredav_client/events.rb', line 49

def owner(uri)
  # Warning: This is not a standard request. It only works if your sabredav
  # server uses a certain OwnerPlugin
  header = {content_type: "application/xml"}
  body = XmlRequestBuilder::PropfindOwner.new.to_xml
  req = client.create_request(:propfind, path: uri, header: header, body: body)
  res = req.run

  SabredavClient::Errors::errorhandling(res)
  xml = REXML::Document.new(res.body)
  REXML::XPath.first(xml, "//cs:objectOwner").text
end

#update_owner(uri, owner) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/sabredav_client/events.rb', line 62

def update_owner(uri, owner)
  # Warning: This is not a standard request. It only works if your sabredav
  # server uses a certain OwnerPlugin
  header = {content_type: "application/xml"}
  body = XmlRequestBuilder::ProppatchEventsOwner.new(owner).to_xml
  req = client.create_request(:proppatch, path: uri, header: header, body: body)
  res = req.run

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