Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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
9 changes: 9 additions & 0 deletions 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 @@ -91,6 +94,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
58 changes: 58 additions & 0 deletions lib/calendav/components/freebusy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# 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

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
4 changes: 2 additions & 2 deletions lib/calendav/errors.rb

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.

rubocop lint fix

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
43 changes: 43 additions & 0 deletions lib/calendav/properties/freebusy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 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 = start_time
@end_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.instance_of?(Time) && end_time.instance_of?(Time)

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

require "nokogiri"

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

# @param from [Time, String YYYYMMDDTHHMMSSZ (ISO8601 w/o dashes/colons)]
# @param to [Time, String YYYYMMDDTHHMMSSZ (ISO8601 w/o dashes/colons)]
def initialize(from:, to:)
@from = from.instance_of?(Time) ? from.utc.iso8601.delete(":-") : from
@to = to.instance_of?(Time) ? to.utc.iso8601.delete(":-") : 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
end
end
end