Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions .github/workflows/main.yml → .github/workflows/rspec.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
name: Ruby
name: RSpec

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest

services:
radicale:
image: xlrl/radicale
ports:
- 8000:8000
env:
RADICALE_USER: test
RADICALE_PASS: test

strategy:
fail-fast: false
matrix:
Expand All @@ -17,18 +26,12 @@ jobs:
steps:
- uses: actions/checkout@v2

- name: Run Radicale
run: docker run -v ${{ github.workspace }}/spec/data/radicale:/var/radicale --name radicale --publish 8000:8000 --detach xlrl/radicale

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true

- name: Rubocop
run: bundle exec rubocop

- name: RSpec
env:
APPLE_USERNAME: ${{ secrets.APPLE_USERNAME }}
Expand All @@ -41,6 +44,3 @@ jobs:
RADICALE_PASSWORD: test
RADICALE_HOST: http://127.0.0.1:8000
run: bundle exec rspec

- name: Stop Radicale
run: docker stop radicale
19 changes: 19 additions & 0 deletions .github/workflows/rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Rubocop

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.1
bundler-cache: true

- name: Rubocop
run: bundle exec rubocop
7 changes: 4 additions & 3 deletions calendav.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_runtime_dependency "http"
spec.add_runtime_dependency "icalendar"
spec.add_runtime_dependency "nokogiri"
spec.add_dependency "http"
spec.add_dependency "icalendar"
spec.add_dependency "nokogiri"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mistake... switch back to add_runtime_dependency


spec.add_development_dependency "debug"
spec.add_development_dependency "dotenv"
spec.add_development_dependency "google-api-client"
spec.add_development_dependency "googleauth"
Expand Down
12 changes: 11 additions & 1 deletion lib/calendav/clients/calendars_client.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# frozen_string_literal: true

require "securerandom"
require "icalendar"

require_relative "../calendar"
require_relative "../components/freebusy"
require_relative "../parsers/sync_xml"
require_relative "../requests/calendar_home_set"
require_relative "../requests/list_calendars"
require_relative "../requests/make_calendar"
require_relative "../requests/sync_collection"
require_relative "../requests/update_calendar"
require_relative "../requests/free_busy_query"

module Calendav
module Clients
Expand Down Expand Up @@ -59,7 +62,8 @@ def find(url, attributes: DEFAULT_ATTRIBUTES, sync: false)

def list(url = home_url, depth: 1, attributes: DEFAULT_ATTRIBUTES)
request = Requests::ListCalendars.call(attributes)
calendar_xpath = ".//dav:resourcetype/caldav:calendar"
calendar_xpath =
"//d:response[d:propstat/d:prop/cal:supported-calendar-component-set]"

endpoint
.propfind(request.to_xml, url: url, depth: depth)
Expand Down Expand Up @@ -91,6 +95,12 @@ def update(url, attributes)
.text["200 OK"] == "200 OK"
end

def freebusy(url, from:, to:)
request = Requests::FreeBusyQuery.call(from: from, to: to)
response = endpoint.report(request.to_xml, url: url, depth: 1)
Components::Freebusy.from_http_response(response)
end

private

attr_reader :client, :endpoint, :credentials
Expand Down
4 changes: 2 additions & 2 deletions lib/calendav/clients/events_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def find(event_url)
)
end

def list(calendar_url, from: nil, to: nil)
request = Requests::ListEvents.call(from: from, to: to)
def list(calendar_url, from: nil, to: nil, expand: false)
request = Requests::ListEvents.call(from: from, to: to, expand: expand)

endpoint
.report(request.to_xml, url: calendar_url, depth: 1)
Expand Down
60 changes: 60 additions & 0 deletions lib/calendav/components/freebusy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# frozen_string_literal: true

require_relative "../properties/freebusy"

module Calendav
module Components
class Freebusy
attr_reader :dtend, :dtstart, :dtstamp, :freebusy, :ical_name, :name,
:parent, :uid

def self.from_http_response(response)
ical_freebusy = Icalendar::Freebusy.parse(response).first

attributes = attributes_from_ical_freebusy(ical_freebusy)
new(attributes)
end

def initialize(attributes)
@dtend = attributes[:dtend].to_time
@dtstart = attributes[:dtstart].to_time
@dtstamp = attributes[:dtstamp].to_time
@freebusy = attributes[:freebusy].map do |fb|
Properties::Freebusy.from_ical_period(fb)
end
@ical_name = attributes[:ical_name]
@name = attributes[:name]
@uid = attributes[:uid]
end

def to_h
{
dtend: dtend,
dtstart: dtstart,
dtstamp: dtstamp,
freebusy: freebusy,
ical_name: ical_name,
name: name,
parent: parent,
uid: uid
}
end

class << self
private

def attributes_from_ical_freebusy(ical_freebusy)
{
dtend: ical_freebusy.dtend,
dtstamp: ical_freebusy.dtstamp,
dtstart: ical_freebusy.dtstart,
freebusy: ical_freebusy.freebusy,
ical_name: ical_freebusy.ical_name,
name: ical_freebusy.name,
uid: ical_freebusy.uid
}
end
end
end
end
end
4 changes: 2 additions & 2 deletions lib/calendav/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ParsingXMLError < Error
attr_reader :xml, :original

def initialize(xml, original)
super original.message
super(original.message)

@xml = xml
@original = original
Expand All @@ -18,7 +18,7 @@ class RequestError < Error
attr_reader :response

def initialize(response)
super response.status.to_s
super(response.status.to_s)

@response = response
end
Expand Down
49 changes: 49 additions & 0 deletions lib/calendav/properties/freebusy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

module Calendav
module Properties
class Freebusy
attr_reader :start_time, :end_time

def self.from_ical_period(period)
new(start_time: period.first.to_time, end_time: period.last.to_time)
end

def initialize(start_time:, end_time:)
validate_arguments(start_time, end_time)

@start_time = if start_time.respond_to?(:to_time)
start_time.to_time
else
start_time
end
@end_time = end_time.respond_to?(:to_time) ? end_time.to_time : end_time
end

def to_h
{
start_time: start_time,
end_time: end_time
}
end

def to_range
Range.new(start_time, end_time)
end

def duration
end_time - start_time
end

private

def validate_arguments(start_time, end_time)
return if (start_time.is_a?(Time) || start_time.respond_to?(:to_time)) &&
(end_time.is_a?(Time) || end_time.respond_to?(:to_time))

raise ArgumentError,
"arguments must be instances of Time or respond to to_time"
end
end
end
end
37 changes: 37 additions & 0 deletions lib/calendav/requests/free_busy_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require "nokogiri"

module Calendav
module Requests
class FreeBusyQuery
def self.call(...)
new(...).call
end

# @param from [Time, DateTime, String YYYYMMDDTHHMMSSZ (ISO8601 w/o dashes/colons)]
# @param to [Time, DateTime, String YYYYMMDDTHHMMSSZ (ISO8601 w/o dashes/colons)]
def initialize(from:, to:)
@from = time_to_formatted_string(from)
@to = time_to_formatted_string(to)
end

def call
Nokogiri::XML::Builder.new do |xml|
xml["c"].public_send(:"free-busy-query",
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe use: ??

xml["caldav"].public_send(:"free-busy-query", NAMESPACES) do

"xmlns:c" => "urn:ietf:params:xml:ns:caldav") do
xml["c"].public_send(:"time-range", start: @from, end: @to)
end
end
end

private

def time_to_formatted_string(time_obj)
return time_obj unless time_obj.respond_to?(:utc)

time_obj.utc.iso8601.delete(":-")
end
end
end
end
11 changes: 9 additions & 2 deletions lib/calendav/requests/list_events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ def self.call(...)
new(...).call
end

def initialize(from:, to:)
def initialize(from:, to:, expand: false)
@from = from
@to = to
@expand = expand
end

def call
Nokogiri::XML::Builder.new do |xml|
xml["caldav"].public_send("calendar-query", NAMESPACES) do
xml["dav"].prop do
xml["dav"].getetag
xml["caldav"].public_send(:"calendar-data")
xml["caldav"].public_send(:"calendar-data") do
xml["caldav"].expand(start: from, end: to) if expand?
end
end
xml["caldav"].filter do
xml["caldav"].public_send(:"comp-filter", name: "VCALENDAR") do
Expand Down Expand Up @@ -55,6 +58,10 @@ def to
def range?
to || from
end

def expand?
@expand
end
end
end
end
10 changes: 0 additions & 10 deletions spec/data/radicale/config.ini

This file was deleted.

1 change: 0 additions & 1 deletion spec/data/radicale/users

This file was deleted.