Skip to content
This repository was archived by the owner on Jun 29, 2024. It is now read-only.
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
90 changes: 50 additions & 40 deletions lib/calendly/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ def current_user!
# @raise [Calendly::Error] if the uuid arg is empty.
# @raise [Calendly::ApiError] if the api returns error code.
# @since 0.0.1
def user(uuid = 'me')
check_not_empty uuid, 'uuid'
body = request :get, "users/#{uuid}"
def user(uuid_or_uri = 'me')
uri = to_request_uri(uuid_or_uri) { |uuid| "users/#{uuid}" }
body = request :get, uri
User.new body[:resource], self
end

Expand All @@ -98,9 +98,9 @@ def user(uuid = 'me')
# @raise [Calendly::Error] if the uuid arg is empty.
# @raise [Calendly::ApiError] if the api returns error code.
# @since 0.4.1
def event_type(uuid)
check_not_empty uuid, 'uuid'
body = request :get, "event_types/#{uuid}"
def event_type(uuid_or_uri)
uri = to_request_uri(uuid_or_uri) { |uuid| "event_types/#{uuid}" }
body = request :get, uri
EventType.new body[:resource], self
end

Expand Down Expand Up @@ -197,9 +197,9 @@ def event_type_available_times(event_type_uri, start_time: nil, end_time: nil)
# @raise [Calendly::Error] if the uuid arg is empty.
# @raise [Calendly::ApiError] if the api returns error code.
# @since 0.0.3
def scheduled_event(uuid)
check_not_empty uuid, 'uuid'
body = request :get, "scheduled_events/#{uuid}"
def scheduled_event(uuid_or_uri)
uri = to_request_uri(uuid_or_uri) { |uuid| "scheduled_events/#{uuid}" }
body = request :get, uri
Event.new body[:resource], self
end

Expand Down Expand Up @@ -244,13 +244,13 @@ def scheduled_events(org_uri, options: nil)
# @raise [Calendly::Error] if the uuid arg is empty.
# @raise [Calendly::ApiError] if the api returns error code.
# @since 0.11.0
def cancel_event(uuid, options: nil)
check_not_empty uuid, 'uuid'
def cancel_event(uuid_or_uri, options: nil)
uri = to_request_uri(uuid_or_uri) { |uuid| "scheduled_events/#{uuid}/cancellation" }

opts_keys = %i[reason]
params = merge_options options, opts_keys

body = request :post, "scheduled_events/#{uuid}/cancellation", body: params
body = request :post, uri, body: params
InviteeCancellation.new body[:resource], self
end

Expand Down Expand Up @@ -297,10 +297,13 @@ def scheduled_events_by_user(user_uri, options: nil)
# @raise [Calendly::Error] if the inv_uuid arg is empty.
# @raise [Calendly::ApiError] if the api returns error code.
# @since 0.0.4
def event_invitee(ev_uuid, inv_uuid)
check_not_empty ev_uuid, 'ev_uuid'
check_not_empty inv_uuid, 'inv_uuid'
body = request :get, "scheduled_events/#{ev_uuid}/invitees/#{inv_uuid}"
def event_invitee(invitee_uri_or_ev_uuid, inv_uuid = nil)
check_not_empty invitee_uri_or_ev_uuid, 'invitee_uri_or_ev_uuid'
uri = to_request_uri(invitee_uri_or_ev_uuid) do |ev_uuid|
check_not_empty inv_uuid, 'inv_uuid'
"scheduled_events/#{ev_uuid}/invitees/#{inv_uuid}"
end
body = request :get, uri
Invitee.new body[:resource], self
end

Expand All @@ -320,12 +323,12 @@ def event_invitee(ev_uuid, inv_uuid)
# @raise [Calendly::Error] if the uuid arg is empty.
# @raise [Calendly::ApiError] if the api returns error code.
# @since 0.0.4
def event_invitees(uuid, options: nil)
check_not_empty uuid, 'uuid'
def event_invitees(uuid_or_uri, options: nil)
uri = to_request_uri(uuid_or_uri) { |uuid| "scheduled_events/#{uuid}/invitees" }

opts_keys = %i[count email page_token sort status]
params = merge_options options, opts_keys
body = request :get, "scheduled_events/#{uuid}/invitees", params: params
body = request :get, uri, params: params

items = body[:collection] || []
evs = items.map { |item| Invitee.new item, self }
Expand All @@ -341,9 +344,9 @@ def event_invitees(uuid, options: nil)
# @raise [Calendly::Error] if the uuid arg is empty.
# @raise [Calendly::ApiError] if the api returns error code.
# @since 0.9.0
def invitee_no_show(uuid)
check_not_empty uuid, 'uuid'
body = request :get, "invitee_no_shows/#{uuid}"
def invitee_no_show(uuid_or_uri)
uri = to_request_uri(uuid_or_uri) { |uuid| "invitee_no_shows/#{uuid}" }
body = request :get, uri
InviteeNoShow.new body[:resource], self
end

Expand Down Expand Up @@ -442,9 +445,9 @@ def activity_log_entries(org_uri, options: nil)
# @raise [Calendly::Error] if the uuid arg is empty.
# @raise [Calendly::ApiError] if the api returns error code.
# @since 0.0.5
def membership(uuid)
check_not_empty uuid, 'uuid'
body = request :get, "organization_memberships/#{uuid}"
def membership(uuid_or_uri)
uri = to_request_uri(uuid_or_uri) { |uuid| "organization_memberships/#{uuid}" }
body = request :get, uri
OrganizationMembership.new body[:resource], self
end

Expand Down Expand Up @@ -510,9 +513,9 @@ def memberships_by_user(user_uri, options: nil)
# @raise [Calendly::Error] if the uuid arg is empty.
# @raise [Calendly::ApiError] if the api returns error code.
# @since 0.0.7
def delete_membership(uuid)
check_not_empty uuid, 'uuid'
request :delete, "organization_memberships/#{uuid}"
def delete_membership(uuid_or_uri)
uri = to_request_uri(uuid_or_uri) { |uuid| "organization_memberships/#{uuid}" }
request :delete, uri
true
end

Expand Down Expand Up @@ -608,9 +611,9 @@ def delete_invitation(org_uuid, inv_uuid)
# @raise [Calendly::Error] if the uuid arg is empty.
# @raise [Calendly::ApiError] if the api returns error code.
# @since 0.1.3
def webhook(uuid)
check_not_empty uuid, 'uuid'
body = request :get, "webhook_subscriptions/#{uuid}"
def webhook(uuid_or_uri)
uri = to_request_uri(uuid_or_uri) { |uuid| "webhook_subscriptions/#{uuid}" }
body = request :get, uri
WebhookSubscription.new body[:resource], self
end

Expand Down Expand Up @@ -709,9 +712,9 @@ def create_webhook(url, events, org_uri, user_uri: nil, signing_key: nil) # rubo
# @raise [Calendly::Error] if the uuid arg is empty.
# @raise [Calendly::ApiError] if the api returns error code.
# @since 0.1.3
def delete_webhook(uuid)
check_not_empty uuid, 'uuid'
request :delete, "webhook_subscriptions/#{uuid}"
def delete_webhook(uuid_or_uri)
uri = to_request_uri(uuid_or_uri) { |uuid| "webhook_subscriptions/#{uuid}" }
request :delete, uri
true
end

Expand All @@ -723,9 +726,9 @@ def delete_webhook(uuid)
# @raise [Calendly::Error] if the uuid arg is empty.
# @raise [Calendly::ApiError] if the api returns error code.
# @since 0.12.0
def routing_form(uuid)
check_not_empty uuid, 'uuid'
body = request :get, "routing_forms/#{uuid}"
def routing_form(uuid_or_uri)
uri = to_request_uri(uuid_or_uri) { |uuid| "routing_forms/#{uuid}" }
body = request :get, uri
RoutingForm.new body[:resource], self
end

Expand Down Expand Up @@ -764,9 +767,9 @@ def routing_forms(org_uri, options: nil)
# @raise [Calendly::Error] if the uuid arg is empty.
# @raise [Calendly::ApiError] if the api returns error code.
# @since 0.12.0
def routing_form_submission(uuid)
check_not_empty uuid, 'uuid'
body = request :get, "routing_form_submissions/#{uuid}"
def routing_form_submission(uuid_or_uri)
uri = to_request_uri(uuid_or_uri) { |uuid| "routing_form_submissions/#{uuid}" }
body = request :get, uri
RoutingFormSubmission.new body[:resource], self
end

Expand Down Expand Up @@ -830,6 +833,13 @@ def create_schedule_link(uri, max_event_count: 1, owner_type: 'EventType')

private

def to_request_uri(uuid_or_uri)
check_not_empty uuid_or_uri, 'uuid_or_uri'
return uuid_or_uri if uuid_or_uri.start_with?('http')

yield uuid_or_uri
end

def request(method, path, params: nil, body: nil)
debug_log "Request #{method.to_s.upcase} #{API_HOST}/#{path} params:#{params}, body:#{body}"
res = access_token.request method, path, params: params, body: body
Expand Down
28 changes: 14 additions & 14 deletions test/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ def test_that_it_raises_an_argument_error_on_user
proc_arg_is_empty = proc do
@client.user ''
end
assert_required_error proc_arg_is_nil, 'uuid'
assert_required_error proc_arg_is_empty, 'uuid'
assert_required_error proc_arg_is_nil, 'uuid_or_uri'
assert_required_error proc_arg_is_empty, 'uuid_or_uri'
end

#
Expand Down Expand Up @@ -152,7 +152,7 @@ def test_that_it_raises_an_argument_error_on_event_type
proc_arg_is_empty = proc do
@client.event_type ''
end
assert_required_error proc_arg_is_empty, 'uuid'
assert_required_error proc_arg_is_empty, 'uuid_or_uri'
end

#
Expand Down Expand Up @@ -395,7 +395,7 @@ def test_that_it_raises_an_argument_error_on_event
proc_arg_is_empty = proc do
@client.scheduled_event ''
end
assert_required_error proc_arg_is_empty, 'uuid'
assert_required_error proc_arg_is_empty, 'uuid_or_uri'
end

#
Expand Down Expand Up @@ -557,7 +557,7 @@ def test_that_it_raises_an_argument_error_on_cancel_event
proc_arg_is_empty = proc do
@client.cancel_event ''
end
assert_required_error proc_arg_is_empty, 'uuid'
assert_required_error proc_arg_is_empty, 'uuid_or_uri'
end

#
Expand All @@ -583,7 +583,7 @@ def test_that_it_raises_an_argument_error_on_event_invitee
proc_inv_uuid_arg_is_empty = proc do
@client.event_invitee 'EV001', ''
end
assert_required_error proc_ev_uuid_arg_is_empty, 'ev_uuid'
assert_required_error proc_ev_uuid_arg_is_empty, 'invitee_uri_or_ev_uuid'
assert_required_error proc_inv_uuid_arg_is_empty, 'inv_uuid'
end

Expand Down Expand Up @@ -643,7 +643,7 @@ def test_that_it_raises_an_argument_error_on_event_invitees
proc_arg_is_empty = proc do
@client.event_invitees ''
end
assert_required_error proc_arg_is_empty, 'uuid'
assert_required_error proc_arg_is_empty, 'uuid_or_uri'
end

#
Expand All @@ -665,7 +665,7 @@ def test_that_it_raises_an_argument_error_on_invitee_no_show
proc_arg_is_empty = proc do
@client.invitee_no_show ''
end
assert_required_error proc_arg_is_empty, 'uuid'
assert_required_error proc_arg_is_empty, 'uuid_or_uri'
end

#
Expand Down Expand Up @@ -826,7 +826,7 @@ def test_that_it_raises_an_argument_error_on_membership
proc_arg_is_empty = proc do
@client.membership ''
end
assert_required_error proc_arg_is_empty, 'uuid'
assert_required_error proc_arg_is_empty, 'uuid_or_uri'
end

#
Expand Down Expand Up @@ -915,7 +915,7 @@ def test_that_it_raises_an_argument_error_on_delete_membership
proc_arg_is_empty = proc do
@client.delete_membership ''
end
assert_required_error proc_arg_is_empty, 'uuid'
assert_required_error proc_arg_is_empty, 'uuid_or_uri'
end

#
Expand Down Expand Up @@ -1067,7 +1067,7 @@ def test_that_it_raises_an_argument_error_on_webhook
proc_uuid_arg_is_empty = proc do
@client.webhook ''
end
assert_required_error proc_uuid_arg_is_empty, 'uuid'
assert_required_error proc_uuid_arg_is_empty, 'uuid_or_uri'
end

#
Expand Down Expand Up @@ -1308,7 +1308,7 @@ def test_that_it_raises_an_argument_error_on_delete_webhook
proc_uuid_arg_is_empty = proc do
@client.delete_webhook ''
end
assert_required_error proc_uuid_arg_is_empty, 'uuid'
assert_required_error proc_uuid_arg_is_empty, 'uuid_or_uri'
end

#
Expand All @@ -1328,7 +1328,7 @@ def test_that_it_raises_an_argument_error_on_routing_form
proc_uuid_arg_is_empty = proc do
@client.routing_form ''
end
assert_required_error proc_uuid_arg_is_empty, 'uuid'
assert_required_error proc_uuid_arg_is_empty, 'uuid_or_uri'
end

#
Expand Down Expand Up @@ -1409,7 +1409,7 @@ def test_that_it_raises_an_argument_error_on_routing_form_submission
proc_uuid_arg_is_empty = proc do
@client.routing_form_submission ''
end
assert_required_error proc_uuid_arg_is_empty, 'uuid'
assert_required_error proc_uuid_arg_is_empty, 'uuid_or_uri'
end

#
Expand Down