Class: Calendav::Clients::CalendarsClient

Inherits:
Object
  • Object
show all
Defined in:
lib/calendav/clients/calendars_client.rb

Constant Summary collapse

DEFAULT_ATTRIBUTES =
%i[
  display_name resource_type etag ctag color components reports
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(client, endpoint, credentials) ⇒ CalendarsClient

Returns a new instance of CalendarsClient.



20
21
22
23
24
# File 'lib/calendav/clients/calendars_client.rb', line 20

def initialize(client, endpoint, credentials)
  @client = client
  @endpoint = endpoint
  @credentials = credentials
end

Instance Method Details

#create(identifier, attributes) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/calendav/clients/calendars_client.rb', line 42

def create(identifier, attributes)
  request = Requests::MakeCalendar.call(attributes)
  url = merged_url(identifier)
  result = endpoint.mkcalendar(request.to_xml, url: url)

  result.headers["Location"] || url
end

#create?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/calendav/clients/calendars_client.rb', line 38

def create?
  options.include?("MKCOL") || options.include?("MKCALENDAR")
end

#delete(url) ⇒ Object



50
51
52
# File 'lib/calendav/clients/calendars_client.rb', line 50

def delete(url)
  endpoint.delete(url: url)
end

#find(url, attributes: DEFAULT_ATTRIBUTES, sync: false) ⇒ Object



54
55
56
57
58
# File 'lib/calendav/clients/calendars_client.rb', line 54

def find(url, attributes: DEFAULT_ATTRIBUTES, sync: false)
  attributes = (attributes.dup << :sync_token) if sync

  list(url, depth: 0, attributes: attributes).first
end

#home_urlObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/calendav/clients/calendars_client.rb', line 26

def home_url
  @home_url ||= begin
    request = Requests::CalendarHomeSet.call
    response = endpoint.propfind(request.to_xml, url: principal_url).first

    ContextualURL.call(
      credentials.host,
      response.xpath(".//caldav:calendar-home-set/dav:href").text
    )
  end
end

#list(url = home_url, depth: 1, attributes: DEFAULT_ATTRIBUTES) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/calendav/clients/calendars_client.rb', line 60

def list(url = home_url, depth: 1, attributes: DEFAULT_ATTRIBUTES)
  request = Requests::ListCalendars.call(attributes)
  calendar_xpath = ".//dav:resourcetype/caldav:calendar"

  endpoint
    .propfind(request.to_xml, url: url, depth: depth)
    .select { |node| node.xpath(calendar_xpath).any? }
    .collect { |node| Calendar.from_xml(url, node) }
end

#optionsObject



70
71
72
73
74
75
# File 'lib/calendav/clients/calendars_client.rb', line 70

def options
  endpoint
    .options(url: home_url)
    .headers["Allow"]
    .split(", ")
end

#sync(url, token) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/calendav/clients/calendars_client.rb', line 77

def sync(url, token)
  request = Requests::SyncCollection.call(token)

  Parsers::SyncXML.call(
    url, endpoint.report(request.to_xml, url: url, depth: nil)
  )
end

#update(url, attributes) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/calendav/clients/calendars_client.rb', line 85

def update(url, attributes)
  request = Requests::UpdateCalendar.call(attributes)
  endpoint
    .proppatch(request.to_xml, url: url)
    .first
    .xpath(".//dav:status")
    .text["200 OK"] == "200 OK"
end