class Calendav::Endpoint

Constants

CONTENT_TYPES

Attributes

credentials[R]

Public Class Methods

new(credentials) click to toggle source
# File lib/calendav/endpoint.rb, line 14
def initialize(credentials)
  @credentials = credentials
end

Public Instance Methods

delete(url:, etag: nil) click to toggle source
# File lib/calendav/endpoint.rb, line 18
def delete(url:, etag: nil)
  request(:delete, url: url, http: with_headers(etag: etag))
    .status
    .success?
end
get(url:) click to toggle source
# File lib/calendav/endpoint.rb, line 24
def get(url:)
  parse request(:get, url: url)
end
mkcalendar(body, url:) click to toggle source
# File lib/calendav/endpoint.rb, line 28
def mkcalendar(body, url:)
  parse request(
    :mkcalendar,
    body,
    url: url,
    http: with_headers(content_type: :xml)
  )
end
options(url:) click to toggle source
# File lib/calendav/endpoint.rb, line 37
def options(url:)
  request(:options, url: url)
end
propfind(body, url: nil, depth: 0) click to toggle source
# File lib/calendav/endpoint.rb, line 41
def propfind(body, url: nil, depth: 0)
  parse request(
    :propfind,
    body,
    url: url,
    http: with_headers(depth: depth, content_type: :xml)
  )
end
proppatch(body, url: nil) click to toggle source
# File lib/calendav/endpoint.rb, line 50
def proppatch(body, url: nil)
  parse request(
    :proppatch,
    body,
    url: url,
    http: with_headers(content_type: :xml)
  )
end
put(body, url:, content_type: nil, etag: nil) click to toggle source
# File lib/calendav/endpoint.rb, line 59
def put(body, url:, content_type: nil, etag: nil)
  parse request(
    :put,
    body,
    url: url,
    http: with_headers(content_type: content_type, etag: etag)
  )
end
report(body, url: nil, depth: 0) click to toggle source
# File lib/calendav/endpoint.rb, line 68
def report(body, url: nil, depth: 0)
  parse request(
    :report,
    body,
    url: url,
    http: with_headers(depth: depth, content_type: :xml)
  )
end

Private Instance Methods

authenticated() click to toggle source
# File lib/calendav/endpoint.rb, line 81
def authenticated
  case credentials.authentication
  when :basic_auth
    HTTP.basic_auth(user: credentials.username, pass: credentials.password)
  when :bearer_token
    HTTP.auth("Bearer #{credentials.password}")
  else
    raise "Unexpected authentication approach: " \
          "#{credentials.authentication}"
  end
end
parse(response) click to toggle source
# File lib/calendav/endpoint.rb, line 117
def parse(response)
  return response if response.content_length&.zero? || response.body.empty?

  if response.content_type.mime_type == "text/calendar"
    response
  else
    Parsers::ResponseXML.call(response.body)
  end
end
request(verb, body = nil, url:, http: with_headers) click to toggle source
# File lib/calendav/endpoint.rb, line 105
def request(verb, body = nil, url:, http: with_headers)
  response = http.request(
    verb, ContextualURL.call(credentials.host, url), body: body
  )

  return response if response.status.success?

  raise PreconditionError, response if response.status.code == 412

  raise RequestError, response
end
with_headers(content_type: nil, depth: nil, etag: nil) click to toggle source
# File lib/calendav/endpoint.rb, line 93
def with_headers(content_type: nil, depth: nil, etag: nil)
  http = authenticated

  http = http.headers(depth: depth) if depth
  http = http.headers("If-Match" => etag) if etag
  if content_type
    http = http.headers("Content-Type" => CONTENT_TYPES[content_type])
  end

  http
end