Class: Tightknit::Resources::CalendarEvents

Inherits:
Object
  • Object
show all
Defined in:
lib/tightknit/resources/calendar_events.rb

Overview

The CalendarEvents class provides methods for interacting with the calendar events API. It allows listing, creating, updating, and deleting calendar events.

Examples:

List upcoming events

client = Tightknit.client
events = client.calendar_events.list(time_filter: "upcoming")

Get a specific event

client = Tightknit.client
event = client.calendar_events.get("event_id")

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ CalendarEvents

Initialize a new CalendarEvents resource

Parameters:



22
23
24
# File 'lib/tightknit/resources/calendar_events.rb', line 22

def initialize(client)
  @client = client
end

Instance Method Details

#all(options = {}) ⇒ Hash

Get both past and upcoming events

Parameters:

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

    Options for filtering events

Options Hash (options):

  • :page (Integer) — default: 0

    Page number

  • :per_page (Integer) — default: 10

    Number of records per page

  • :status (String) — default: 'published'

    Filter events by status

Returns:

  • (Hash)

    Combined response containing both past and upcoming events



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/tightknit/resources/calendar_events.rb', line 59

def all(options = {})
  # Get upcoming events
  upcoming_options = options.dup
  upcoming_options[:time_filter] = "upcoming"
  upcoming_response = list(upcoming_options)

  # Get past events
  past_options = options.dup
  past_options[:time_filter] = "past"
  past_response = list(past_options)

  # Combine the results
  if upcoming_response[:success] && past_response[:success]
    combined_records = []

    if upcoming_response[:data] && upcoming_response[:data][:records]
      combined_records += upcoming_response[:data][:records]
    end

    if past_response[:data] && past_response[:data][:records]
      combined_records += past_response[:data][:records]
    end

    # Create a combined response with the same structure
    {
      success: true,
      data: {
        records: combined_records,
        total: combined_records.length
      }
    }
  else
    # If either request failed, return the successful one or the first error
    upcoming_response[:success] ? upcoming_response : past_response
  end
end

#format_data(event_data) ⇒ Hash

Format event data for the API

Parameters:

  • event_data (Hash)

    The raw event data

Returns:

  • (Hash)

    The formatted event data



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/tightknit/resources/calendar_events.rb', line 110

def format_data(event_data)
  # Create a new hash to avoid modifying the original
  formatted = event_data.dup

  # Format description if it's a string
  if formatted[:description].is_a?(String)
    formatted[:description] = {text: formatted[:description]}
  end

  # Format recap if it's a string
  if formatted[:recap].is_a?(String)
    formatted[:recap] = {text: formatted[:recap]}
  end

  # Format hosts if it's an array of IDs
  if formatted[:hosts].is_a?(Array)
    formatted[:hosts] = {slack_user_ids: formatted[:hosts]}
  end

  # Format speakers if it's an array of IDs
  if formatted[:speakers].is_a?(Array)
    formatted[:speakers] = {slack_user_ids: formatted[:speakers]}
  end

  formatted
end

#get(event_id) ⇒ Hash

Get a specific calendar event

Parameters:

  • event_id (String)

    The ID of the event to retrieve

Returns:

  • (Hash)

    Response containing event data

Raises:



101
102
103
# File 'lib/tightknit/resources/calendar_events.rb', line 101

def get(event_id)
  @client.get("calendar_events/#{event_id}")
end

#list(options = {}) ⇒ Hash

Get a list of calendar events

Parameters:

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

    Options for filtering events

Options Hash (options):

  • :page (Integer) — default: 0

    Page number

  • :per_page (Integer) — default: 10

    Number of records per page

  • :time_filter (String) — default: 'upcoming' or 'past'

    Filter events by time

  • :status (String) — default: 'published'

    Filter events by status

Returns:

  • (Hash)

    Response containing events data



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tightknit/resources/calendar_events.rb', line 34

def list(options = {})
  page = options[:page] || 0
  per_page = options[:per_page] || 10
  time_filter = options[:time_filter]
  status = options[:status] || "published"

  params = {
    page: page,
    per_page: per_page,
    status: status
  }

  # Only add time_filter to params if it's specified
  params[:time_filter] = time_filter if time_filter

  @client.get("calendar_events", params)
end