Class: Calendly::Client

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/calendly/client.rb

Overview

Calendly apis client.

Constant Summary collapse

API_HOST =
'https://api.calendly.com'
AUTH_API_HOST =
'https://auth.calendly.com'

Instance Method Summary collapse

Methods included from Loggable

#debug_log, #error_log, #info_log, #warn_log

Constructor Details

#initialize(token = nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • token (String) (defaults to: nil)

    a Calendly’s access token.

Raises:



14
15
16
17
18
19
20
# File 'lib/calendly/client.rb', line 14

def initialize(token = nil)
  @config = Calendly.configuration
  @token = token || Calendly.configuration.token

  check_not_empty @token, 'token'
  check_token
end

Instance Method Details

#access_tokenOAuth2::AccessToken

Get access token object.

Returns:

  • (OAuth2::AccessToken)

Since:

  • 0.0.1



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/calendly/client.rb', line 27

def access_token
  return @access_token if defined? @access_token

  client = OAuth2::Client.new(@config.client_id,
                              @config.client_secret, client_options)
  @access_token = OAuth2::AccessToken.new(
    client, @token,
    refresh_token: @config.refresh_token,
    expires_at: @config.token_expires_at
  )
end

#create_invitation(uuid, email) ⇒ Calendly::OrganizationInvitation

Invite a person to an Organization.

Parameters:

  • uuid (String)

    the specified organization (organization’s uuid).

  • email (String)

    Email of the person being invited.

Returns:

Raises:

Since:

  • 0.0.7



393
394
395
396
397
398
399
400
401
402
# File 'lib/calendly/client.rb', line 393

def create_invitation(uuid, email)
  check_not_empty uuid, 'uuid'
  check_not_empty email, 'email'
  body = request(
    :post,
    "organizations/#{uuid}/invitations",
    body: {email: email}
  )
  OrganizationInvitation.new body[:resource], self
end

#create_webhook(url, events, org_uri, user_uri = nil) ⇒ Calendly::WebhookSubscription

Create a webhook subscription for an organization or user.

Parameters:

  • url (String)

    Canonical reference (unique identifier) for the resource.

  • events (Array<String>)

    List of user events to subscribe to. options: invitee.created or invitee.canceled

  • org_uri (String)

    The unique reference to the organization that the webhook will be tied to.

  • user_uri (String) (defaults to: nil)

    The unique reference to the user that the webhook will be tied to.

Returns:

Raises:

Since:

  • 0.1.3



503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# File 'lib/calendly/client.rb', line 503

def create_webhook(url, events, org_uri, user_uri = nil)
  check_not_empty url, 'url'
  check_not_empty events, 'events'
  check_not_empty org_uri, 'org_uri'

  params = {url: url, events: events, organization: org_uri}
  if user_uri
    params[:scope] = 'user'
    params[:user] = user_uri
  else
    params[:scope] = 'organization'
  end
  body = request(:post, 'webhook_subscriptions', body: params)
  WebhookSubscription.new body[:resource], self
end

#current_userCalendly::User Also known as: me

Get basic information about current user.

Returns:

Raises:

Since:

  • 0.0.1



61
62
63
64
65
# File 'lib/calendly/client.rb', line 61

def current_user
  return @cached_current_user if @cached_current_user

  @cached_current_user = user
end

#current_user!Object Also known as: me!

Since:

  • 0.2.0



70
71
72
73
# File 'lib/calendly/client.rb', line 70

def current_user!
  @cached_current_user = nil
  current_user
end

#delete_invitation(org_uuid, inv_uuid) ⇒ true

Revoke Organization Invitation.

Parameters:

  • org_uuid (String)

    the specified organization (organization’s uuid).

  • inv_uuid (String)

    the specified invitation (organization invitation’s uuid).

Returns:

  • (true)

Raises:

Since:

  • 0.0.7



414
415
416
417
418
419
# File 'lib/calendly/client.rb', line 414

def delete_invitation(org_uuid, inv_uuid)
  check_not_empty org_uuid, 'org_uuid'
  check_not_empty inv_uuid, 'inv_uuid'
  request :delete, "organizations/#{org_uuid}/invitations/#{inv_uuid}"
  true
end

#delete_membership(uuid) ⇒ true

Remove a User from an Organization.

Parameters:

  • uuid (String)

    the specified memberhip (organization memberhips’s uuid).

Returns:

  • (true)

Raises:

Since:

  • 0.0.7



331
332
333
334
335
# File 'lib/calendly/client.rb', line 331

def delete_membership(uuid)
  check_not_empty uuid, 'uuid'
  request :delete, "organization_memberships/#{uuid}"
  true
end

#delete_webhook(uuid) ⇒ true

Delete a webhook subscription for an organization or user with a specified UUID.

Parameters:

  • uuid (String)

    the specified webhook (webhook’s uuid).

Returns:

  • (true)

Raises:

Since:

  • 0.1.3



527
528
529
530
531
# File 'lib/calendly/client.rb', line 527

def delete_webhook(uuid)
  check_not_empty uuid, 'uuid'
  request :delete, "webhook_subscriptions/#{uuid}"
  true
end

#event_invitee(ev_uuid, inv_uuid) ⇒ Calendly::Invitee

Get Invitee of an Event Returns a single Invitee by their URI.

Parameters:

  • ev_uuid (String)

    the specified event (event’s uuid).

  • inv_uuid (String)

    the specified invitee (invitee’s uuid).

Returns:

Raises:

Since:

  • 0.0.4



220
221
222
223
224
225
# File 'lib/calendly/client.rb', line 220

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}"
  Invitee.new body[:resource], self
end

#event_invitees(uuid, opts = {}) ⇒ Array<Array<Calendly::Invitee>, Hash>

Get List of Event Invitees.

Parameters:

  • uuid (String)

    the specified event (event’s uuid).

  • opts (Hash) (defaults to: {})

    the optional request parameters.

Options Hash (opts):

  • :count (Integer)

    Number of rows to return.

  • :email (String)

    Filter by email.

  • :page_token (String)

    Pass this to get the next portion of collection.

  • :sort (String)

    Order results by the specified field and directin. Accepts comma-separated list of field:direction values.

  • :status (String)

    Whether the scheduled event is active or canceled.

Returns:

  • (Array<Array<Calendly::Invitee>, Hash>)
    • Array<Calendly::Invitee>

      invitees

    • Hash

      next_params the parameters to get next data. if thre is no next it returns nil.

Raises:

Since:

  • 0.0.4



243
244
245
246
247
248
249
250
251
252
253
# File 'lib/calendly/client.rb', line 243

def event_invitees(uuid, opts = {})
  check_not_empty uuid, 'uuid'

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

  items = body[:collection] || []
  evs = items.map { |item| Invitee.new item, self }
  [evs, next_page_params(body)]
end

#event_type(uuid) ⇒ Calendly::EventType

Returns a single Event Type by its UUID.

Parameters:

  • uuid (String)

    the specified event type (event type’s uuid).

Returns:

Raises:

Since:

  • 0.4.1



99
100
101
102
103
# File 'lib/calendly/client.rb', line 99

def event_type(uuid)
  check_not_empty uuid, 'uuid'
  body = request :get, "event_types/#{uuid}"
  EventType.new body[:resource], self
end

#event_types(user_uri, opts = {}) ⇒ Array<Array<Calendly::EventType>, Hash>

Returns all Event Types associated with a specified User.

Parameters:

  • user_uri (String)

    the specified user (user’s uri).

  • opts (Hash) (defaults to: {})

    the optional request parameters.

Options Hash (opts):

  • :count (Integer)

    Number of rows to return.

  • :page_token (String)

    Pass this to get the next portion of collection.

  • :sort (String)

    Order results by the specified field and direction. Accepts comma-separated list of field:direction values.

Returns:

  • (Array<Array<Calendly::EventType>, Hash>)
    • Array<Calendly::EventType>

      event_types

    • Hash

      next_params the parameters to get next data. if thre is no next it returns nil.

Raises:

Since:

  • 0.0.2



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/calendly/client.rb', line 119

def event_types(user_uri, opts = {})
  check_not_empty user_uri, 'user_uri'

  opts_keys = %i[count page_token sort]
  params = {user: user_uri}
  params = merge_options opts, opts_keys, params
  body = request :get, 'event_types', params: params

  items = body[:collection] || []
  ev_types = items.map { |item| EventType.new item, self }
  [ev_types, next_page_params(body)]
end

#invitation(org_uuid, inv_uuid) ⇒ Calendly::OrganizationInvitation

Returns an Organization Invitation.

Parameters:

  • org_uuid (String)

    the specified organization (organization’s uuid).

  • inv_uuid (String)

    the specified invitation (organization invitation’s uuid).

Returns:

Raises:

Since:

  • 0.0.6



347
348
349
350
351
352
353
# File 'lib/calendly/client.rb', line 347

def invitation(org_uuid, inv_uuid)
  check_not_empty org_uuid, 'org_uuid'
  check_not_empty inv_uuid, 'inv_uuid'

  body = request :get, "organizations/#{org_uuid}/invitations/#{inv_uuid}"
  OrganizationInvitation.new body[:resource], self
end

#invitations(uuid, opts = {}) ⇒ <Array<Array<Calendly::OrganizationInvitation>, Hash>] - [Array<Calendly::OrganizationInvitation>] organizations - [Hash] next_params the parameters to get next data. if thre is no next it returns nil.

Get Organization Invitations.

Parameters:

  • uuid (String)

    the specified organization (organization’s uuid).

  • opts (Hash) (defaults to: {})

    the optional request parameters.

Options Hash (opts):

  • :count (Integer)

    Number of rows to return.

  • :email (String)

    Filter by email.

  • :page_token (String)

    Pass this to get the next portion of collection.

  • :sort (String)

    Order results by the specified field and directin. Accepts comma-separated list of field:direction values.

  • :status (String)

    Filter by status.

Returns:

  • (<Array<Array<Calendly::OrganizationInvitation>, Hash>] - [Array<Calendly::OrganizationInvitation>] organizations - [Hash] next_params the parameters to get next data. if thre is no next it returns nil.)

    <Array<Array<Calendly::OrganizationInvitation>, Hash>]

    • Array<Calendly::OrganizationInvitation>

      organizations

    • Hash

      next_params the parameters to get next data. if thre is no next it returns nil.

Raises:

Since:

  • 0.0.6



371
372
373
374
375
376
377
378
379
380
381
# File 'lib/calendly/client.rb', line 371

def invitations(uuid, opts = {})
  check_not_empty uuid, 'uuid'

  opts_keys = %i[count email page_token sort status]
  params = merge_options opts, opts_keys

  body = request :get, "organizations/#{uuid}/invitations", params: params
  items = body[:collection] || []
  evs = items.map { |item| OrganizationInvitation.new item, self }
  [evs, next_page_params(body)]
end

#membership(uuid) ⇒ Calendly::OrganizationMembership

Returns information about a user’s organization membership

Parameters:

  • uuid (String)

    the specified membership (organization membership’s uuid).

Returns:

Raises:

Since:

  • 0.0.5



263
264
265
266
267
# File 'lib/calendly/client.rb', line 263

def membership(uuid)
  check_not_empty uuid, 'uuid'
  body = request :get, "organization_memberships/#{uuid}"
  OrganizationMembership.new body[:resource], self
end

#memberships(org_uri, opts = {}) ⇒ Array<Array<Calendly::OrganizationMembership>, Hash>

Get List of memberships belonging to specific an organization.

Parameters:

  • org_uri (String)

    the specified organization (organization’s uri).

  • opts (Hash) (defaults to: {})

    the optional request parameters.

Options Hash (opts):

  • :count (Integer)

    Number of rows to return.

  • :email (String)

    Filter by email.

  • :page_token (String)

    Pass this to get the next portion of collection.

Returns:

  • (Array<Array<Calendly::OrganizationMembership>, Hash>)
    • Array<Calendly::OrganizationMembership>

      memberships

    • Hash

      next_params the parameters to get next data. if thre is no next it returns nil.

Raises:

Since:

  • 0.0.5



283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/calendly/client.rb', line 283

def memberships(org_uri, opts = {})
  check_not_empty org_uri, 'org_uri'

  opts_keys = %i[count email page_token]
  params = {organization: org_uri}
  params = merge_options opts, opts_keys, params
  body = request :get, 'organization_memberships', params: params

  items = body[:collection] || []
  memberships = items.map { |item| OrganizationMembership.new item, self }
  [memberships, next_page_params(body)]
end

#memberships_by_user(user_uri, opts = {}) ⇒ Array<Array<Calendly::OrganizationMembership>, Hash>

Get List of memberships belonging to specific a user.

Parameters:

  • user_uri (String)

    the specified user (user’s uri).

  • opts (Hash) (defaults to: {})

    the optional request parameters.

Options Hash (opts):

  • :count (Integer)

    Number of rows to return.

  • :email (String)

    Filter by email.

  • :page_token (String)

    Pass this to get the next portion of collection.

Returns:

  • (Array<Array<Calendly::OrganizationMembership>, Hash>)
    • Array<Calendly::OrganizationMembership>

      memberships

    • Hash

      next_params the parameters to get next data. if thre is no next it returns nil.

Raises:

Since:

  • 0.0.5



310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/calendly/client.rb', line 310

def memberships_by_user(user_uri, opts = {})
  check_not_empty user_uri, 'user_uri'

  opts_keys = %i[count email page_token]
  params = {user: user_uri}
  params = merge_options opts, opts_keys, params
  body = request :get, 'organization_memberships', params: params

  items = body[:collection] || []
  memberships = items.map { |item| OrganizationMembership.new item, self }
  [memberships, next_page_params(body)]
end

#refresh!Object

Refresh access token.

Raises:

Since:

  • 0.0.7



46
47
48
49
50
51
52
53
# File 'lib/calendly/client.rb', line 46

def refresh!
  check_not_empty @config.client_id, 'client_id'
  check_not_empty @config.client_secret, 'client_secret'
  @access_token = access_token.refresh!
rescue OAuth2::Error => e
  res = e.response.response
  raise ApiError.new res, e
end

#scheduled_event(uuid) ⇒ Calendly::Event

Returns a single Event by its URI.

Parameters:

  • uuid (String)

    the specified event (event’s uuid).

Returns:

Raises:

Since:

  • 0.0.3



140
141
142
143
144
# File 'lib/calendly/client.rb', line 140

def scheduled_event(uuid)
  check_not_empty uuid, 'uuid'
  body = request :get, "scheduled_events/#{uuid}"
  Event.new body[:resource], self
end

#scheduled_events(org_uri, opts = {}) ⇒ Array<Array<Calendly::Event>, Hash>

Get List of scheduled events belonging to a specific organization.

Parameters:

  • org_uri (String)

    the specified organization (organization’s uri).

  • opts (Hash) (defaults to: {})

    the optional request parameters.

Options Hash (opts):

  • :count (Integer)

    Number of rows to return.

  • :invitee_email (String)

    Return events scheduled with the specified invitee email.

  • :max_start_time (String)

    Upper bound (inclusive) for an event’s start time to filter by.

  • :min_start_time (String)

    Lower bound (inclusive) for an event’s start time to filter by.

  • :page_token (String)

    Pass this to get the next portion of collection.

  • :sort (String)

    Order results by the specified field and directin. Accepts comma-separated list of field:direction values.

  • :status (String)

    Whether the scheduled event is active or canceled.

Returns:

  • (Array<Array<Calendly::Event>, Hash>)
    • Array<Calendly::Event>

      events

    • Hash

      next_params the parameters to get next data. if thre is no next it returns nil.

Raises:

Since:

  • 0.5.0



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/calendly/client.rb', line 164

def scheduled_events(org_uri, opts = {})
  check_not_empty org_uri, 'org_uri'

  opts_keys = %i[count invitee_email max_start_time min_start_time page_token sort status]
  params = {organization: org_uri}
  params = merge_options opts, opts_keys, params
  body = request :get, 'scheduled_events', params: params

  items = body[:collection] || []
  evs = items.map { |item| Event.new item, self }
  [evs, next_page_params(body)]
end

#scheduled_events_by_user(user_uri, opts = {}) ⇒ Array<Array<Calendly::Event>, Hash>

Get List of scheduled events belonging to a specific user.

Parameters:

  • user_uri (String)

    the specified user (user’s uri).

  • opts (Hash) (defaults to: {})

    the optional request parameters.

Options Hash (opts):

  • :organization (String)

    the specified organization (organization’s uri).

  • :count (Integer)

    Number of rows to return.

  • :invitee_email (String)

    Return events scheduled with the specified invitee email.

  • :max_start_time (String)

    Upper bound (inclusive) for an event’s start time to filter by.

  • :min_start_time (String)

    Lower bound (inclusive) for an event’s start time to filter by.

  • :page_token (String)

    Pass this to get the next portion of collection.

  • :sort (String)

    Order results by the specified field and directin. Accepts comma-separated list of field:direction values.

  • :status (String)

    Whether the scheduled event is active or canceled.

Returns:

  • (Array<Array<Calendly::Event>, Hash>)
    • Array<Calendly::Event>

      events

    • Hash

      next_params the parameters to get next data. if thre is no next it returns nil.

Raises:

Since:

  • 0.0.3



196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/calendly/client.rb', line 196

def scheduled_events_by_user(user_uri, opts = {})
  check_not_empty user_uri, 'user_uri'

  opts_keys = %i[organization count invitee_email max_start_time min_start_time page_token sort status]
  params = {user: user_uri}
  params = merge_options opts, opts_keys, params
  body = request :get, 'scheduled_events', params: params

  items = body[:collection] || []
  evs = items.map { |item| Event.new item, self }
  [evs, next_page_params(body)]
end

#user(uuid = 'me') ⇒ Calendly::User

Get basic information about a user

Parameters:

  • uuid (String) (defaults to: 'me')

    User unique identifier, or the constant “me” to reference the caller

Returns:

Raises:

Since:

  • 0.0.1



85
86
87
88
89
# File 'lib/calendly/client.rb', line 85

def user(uuid = 'me')
  check_not_empty uuid, 'uuid'
  body = request :get, "users/#{uuid}"
  User.new body[:resource], self
end

#user_scope_webhooks(org_uri, user_uri, opts = {}) ⇒ Array<Array<Calendly::WebhookSubscription>, Hash>

Get List of user scope Webhooks.

Parameters:

  • org_uri (String)

    the specified organization (organization’s uri).

  • user_uri (String)

    the specified user (user’s uri).

  • opts (Hash) (defaults to: {})

    the optional request parameters.

Options Hash (opts):

  • :count (Integer)

    Number of rows to return.

  • :page_token (String)

    Pass this to get the next portion of collection.

  • :sort (String)

    Order results by the specified field and directin. Accepts comma-separated list of field:direction values.

Returns:

  • (Array<Array<Calendly::WebhookSubscription>, Hash>)
    • Array<Calendly::WebhookSubscription>

      webhooks

    • Hash

      next_params the parameters to get next data. if thre is no next it returns nil.

Raises:

Since:

  • 0.1.3



477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/calendly/client.rb', line 477

def user_scope_webhooks(org_uri, user_uri, opts = {})
  check_not_empty org_uri, 'org_uri'
  check_not_empty user_uri, 'user_uri'

  opts_keys = %i[count page_token sort]
  params = {organization: org_uri, user: user_uri, scope: 'user'}
  params = merge_options opts, opts_keys, params
  body = request :get, 'webhook_subscriptions', params: params
  items = body[:collection] || []
  evs = items.map { |item| WebhookSubscription.new item, self }
  [evs, next_page_params(body)]
end

#webhook(uuid) ⇒ Calendly::WebhookSubscription

Get a webhook subscription for an organization or user with a specified UUID.

Parameters:

  • uuid (String)

    the specified webhook (webhook’s uuid).

Returns:

Raises:

Since:

  • 0.1.3



429
430
431
432
433
# File 'lib/calendly/client.rb', line 429

def webhook(uuid)
  check_not_empty uuid, 'uuid'
  body = request :get, "webhook_subscriptions/#{uuid}"
  WebhookSubscription.new body[:resource], self
end

#webhooks(org_uri, opts = {}) ⇒ Array<Array<Calendly::WebhookSubscription>, Hash>

Get List of organization scope Webhooks.

Parameters:

  • org_uri (String)

    the specified organization (organization’s uri).

  • opts (Hash) (defaults to: {})

    the optional request parameters.

Options Hash (opts):

  • :count (Integer)

    Number of rows to return.

  • :page_token (String)

    Pass this to get the next portion of collection.

  • :sort (String)

    Order results by the specified field and directin. Accepts comma-separated list of field:direction values.

Returns:

  • (Array<Array<Calendly::WebhookSubscription>, Hash>)
    • Array<Calendly::WebhookSubscription>

      webhooks

    • Hash

      next_params the parameters to get next data. if thre is no next it returns nil.

Raises:

Since:

  • 0.1.3



449
450
451
452
453
454
455
456
457
458
459
# File 'lib/calendly/client.rb', line 449

def webhooks(org_uri, opts = {})
  check_not_empty org_uri, 'org_uri'

  opts_keys = %i[count page_token sort]
  params = {organization: org_uri, scope: 'organization'}
  params = merge_options opts, opts_keys, params
  body = request :get, 'webhook_subscriptions', params: params
  items = body[:collection] || []
  evs = items.map { |item| WebhookSubscription.new item, self }
  [evs, next_page_params(body)]
end