Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to pass query_slots parameters #111

Merged
merged 4 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
37 changes: 33 additions & 4 deletions lib/cronofy/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -835,8 +835,10 @@ def resources
# each must specify a start and end Time.
# :start_interval - An Integer representing the start interval
# of minutes for the availability query.
# :buffer - An Hash containing the buffer to apply to
# :buffer - A Hash containing the buffer to apply to
# the availability query.
# :query_slots - A Hash containing the query slots to be
# used in the availability query.
#
# Returns an Array of AvailablePeriods.
#
Expand All @@ -861,7 +863,13 @@ def availability(options = {})
options[:buffer] = map_availability_buffer(buffer)
end

translate_available_periods(options[:query_periods] || options[:available_periods])
if query_periods = options[:query_periods] || options[:available_periods]
translate_available_periods(query_periods)
end

if query_slots = options[:query_slots]
translate_query_slots(query_slots)
end

response = availability_post("/v1/availability", options)

Expand Down Expand Up @@ -1052,7 +1060,9 @@ def add_to_calendar(args = {})
# call
# :required_duration - A hash stating the length of time the event will
# last for
# :query_periods - A hash stating the available periods for the event
# :query_periods - A Hash stating the available periods for the event
# :query_slots - A Hash containing the query slots to be
# used in the availability query.
# :start_interval - An Integer representing the start interval
# of minutes for the availability query.
# :buffer - An Hash containing the buffer to apply to
Expand Down Expand Up @@ -1129,7 +1139,14 @@ def real_time_scheduling(args = {})
end
end

translate_available_periods(availability[:query_periods] || availability[:available_periods])
if query_periods = availability[:query_periods] || availability[:available_periods]
translate_available_periods(query_periods)
end

if query_slots = availability[:query_slots]
translate_query_slots(query_slots)
end

body[:availability] = availability

response = raw_post("/v1/real_time_scheduling", body)
Expand Down Expand Up @@ -1717,6 +1734,14 @@ def translate_available_periods(periods)
end
end

def translate_query_slots(query_slots)
query_slots.each do |params|
QUERY_SLOTS_TIME_PARAMS.select { |tp| params.key?(tp) }.each do |tp|
params[tp] = to_iso8601(params[tp])
end
end
end

def map_availability_participants(participants)
case participants
when Hash
Expand Down Expand Up @@ -1847,6 +1872,10 @@ def map_availability_sequence(sequence)
end
}.freeze

QUERY_SLOTS_TIME_PARAMS = %i{
start
}.freeze

FREE_BUSY_DEFAULT_PARAMS = { tzid: "Etc/UTC" }.freeze
FREE_BUSY_TIME_PARAMS = %i{
from
Expand Down
95 changes: 95 additions & 0 deletions spec/lib/cronofy/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1783,6 +1783,51 @@
it_behaves_like 'a Cronofy request with mapped return value'
end

context 'when given query_slots instead of available_periods with start interval' do
let(:participants) do
{ members: %w{acc_567236000909002 acc_678347111010113} }
end

let(:required_duration) { 60 }

let(:query_slots) do
[
{ start: Time.parse("2017-01-03T09:00:00Z")},
{ start: Time.parse("2017-01-04T09:00:00Z") },
]
end

let(:request_body) do
{
"participants" => [
{
"members" => [
{ "sub" => "acc_567236000909002" },
{ "sub" => "acc_678347111010113" }
],
"required" => "all"
}
],
"query_slots" => [
{ "start" => "2017-01-03T09:00:00Z" },
{ "start" => "2017-01-04T09:00:00Z" }
],
"required_duration" => { "minutes" => 60 },
}
end

subject do
client.availability(
participants: participants,
required_duration: required_duration,
query_slots: query_slots
)
end

it_behaves_like 'a Cronofy request'
it_behaves_like 'a Cronofy request with mapped return value'
end

context "when trying to auth with only an access_token, as originally implemented" do
let(:access_token) { "access_token_123"}
let(:client) { Cronofy::Client.new(access_token: access_token) }
Expand Down Expand Up @@ -2363,6 +2408,56 @@
mapped_availability[:query_periods] = mapped_availability.delete(:available_periods)
end
end

context 'when passing query slots' do
let(:availability) do
{
participants: [
{
members: [{
sub: "acc_567236000909002",
calendar_ids: ["cal_n23kjnwrw2_jsdfjksn234"]
}],
required: 'all'
}
],
required_duration: { minutes: 60 },
query_slots: [
{ start: Time.utc(2017, 1, 1, 9, 00) },
{ start: Time.utc(2017, 1, 1, 17, 00) }
],
buffer: {
before: { minutes: 30 },
after: { minutes: 45 },
}
}
end

let(:mapped_availability) do
{
participants: [
{
members: [{
sub: "acc_567236000909002",
calendar_ids: ["cal_n23kjnwrw2_jsdfjksn234"]
}],
required: 'all'
}
],
required_duration: { minutes: 60 },
buffer: {
before: { minutes: 30 },
after: { minutes: 45 },
},
query_slots: [
{ start: Time.utc(2017, 1, 1, 9, 00) },
{ start: Time.utc(2017, 1, 1, 17, 00) }
],
}
it_behaves_like 'a Cronofy request'
it_behaves_like 'a Cronofy request with mapped return value'
end
end
end

describe "#get_real_time_scheduling_status" do
Expand Down
Loading