Class: Gmeet::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/utilities/gmeet/service.rb

Constant Summary collapse

SCOPE =
"https://www.googleapis.com/auth/calendar.readonly"

Instance Method Summary collapse

Constructor Details

#initialize(skip_auth: false) ⇒ Service

Returns a new instance of Service.



17
18
19
20
21
# File 'lib/utilities/gmeet/service.rb', line 17

def initialize(skip_auth: false)
  ensure_env!
  @service = Google::Apis::CalendarV3::CalendarService.new
  @service.authorization = authorize unless skip_auth
end

Instance Method Details

#authenticateObject



192
193
194
195
196
197
# File 'lib/utilities/gmeet/service.rb', line 192

def authenticate
  perform_auth_flow
  {success: true}
rescue => e
  {success: false, error: e.message}
end

#get_meeting_url(event_id, calendar_id: "primary") ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/utilities/gmeet/service.rb', line 156

def get_meeting_url(event_id, calendar_id: "primary")
  event = @service.get_event(calendar_id, event_id)
  meet_link = extract_meet_link(event)

  unless meet_link
    raise "No Google Meet link found for this event"
  end

  {
    event_id: event_id,
    summary: event.summary,
    meet_link: meet_link,
    start: format_event_time(event.start),
    end: format_event_time(event.end)
  }
rescue Google::Apis::Error => e
  raise "Google Calendar API Error: #{e.message}"
rescue => e
  log_error("get_meeting_url", e)
  raise e
end

#list_meetings(start_date: nil, end_date: nil, max_results: 20, calendar_id: "primary") ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/utilities/gmeet/service.rb', line 23

def list_meetings(start_date: nil, end_date: nil, max_results: 20, calendar_id: "primary")
  # Default to today if no start date provided
  now = Time.now
  start_time = start_date ? Time.parse("#{start_date} 00:00:00") : Time.new(now.year, now.month, now.day, 0, 0, 0)
  # Default to 7 days from start if no end date provided
  end_time = end_date ? Time.parse("#{end_date} 23:59:59") : start_time + 7 * 24 * 60 * 60

  results = @service.list_events(
    calendar_id,
    max_results: max_results,
    single_events: true,
    order_by: "startTime",
    time_min: start_time.utc.iso8601,
    time_max: end_time.utc.iso8601
  )

  # Filter for events that have Google Meet links
  meetings = results.items.filter_map do |event|
    meet_link = extract_meet_link(event)
    next unless meet_link

    {
      id: event.id,
      summary: event.summary,
      description: event.description,
      location: event.location,
      start: format_event_time(event.start),
      end: format_event_time(event.end),
      attendees: format_attendees(event.attendees),
      html_link: event.html_link,
      meet_link: meet_link,
      calendar_id: calendar_id
    }
  end

  {meetings: meetings, count: meetings.length}
rescue Google::Apis::Error => e
  raise "Google Calendar API Error: #{e.message}"
rescue => e
  log_error("list_meetings", e)
  raise e
end

#perform_auth_flowObject



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/utilities/gmeet/service.rb', line 199

def perform_auth_flow
  client_id = Mcpeasy::Config.google_client_id
  client_secret = Mcpeasy::Config.google_client_secret

  unless client_id && client_secret
    raise "Google credentials not found. Please save your credentials.json file using: mcpz config set_google_credentials <path_to_credentials.json>"
  end

  # Create credentials using OAuth2 flow with localhost redirect
  redirect_uri = "http://localhost:8080"
  client = Signet::OAuth2::Client.new(
    client_id: client_id,
    client_secret: client_secret,
    scope: SCOPE,
    redirect_uri: redirect_uri,
    authorization_uri: "https://accounts.google.com/o/oauth2/auth",
    token_credential_uri: "https://oauth2.googleapis.com/token"
  )

  # Generate authorization URL
  url = client.authorization_uri.to_s

  puts "DEBUG: Client ID: #{client_id[0..20]}..."
  puts "DEBUG: Scope: #{SCOPE}"
  puts "DEBUG: Redirect URI: #{redirect_uri}"
  puts

  # Start callback server to capture OAuth code
  puts "Starting temporary web server to capture OAuth callback..."
  puts "Opening authorization URL in your default browser..."
  puts url
  puts

  # Automatically open URL in default browser on macOS/Unix
  if system("which open > /dev/null 2>&1")
    system("open", url)
  else
    puts "Could not automatically open browser. Please copy the URL above manually."
  end
  puts
  puts "Waiting for OAuth callback... (will timeout in 60 seconds)"

  # Wait for the authorization code with timeout
  code = GoogleAuthServer.capture_auth_code

  unless code
    raise "Failed to receive authorization code. Please try again."
  end

  puts "✅ Authorization code received!"
  client.code = code
  client.fetch_access_token!

  # Save credentials to config
  credentials_data = {
    client_id: client.client_id,
    client_secret: client.client_secret,
    scope: client.scope,
    refresh_token: client.refresh_token,
    access_token: client.access_token,
    expires_at: client.expires_at
  }

  Mcpeasy::Config.save_google_token(credentials_data)
  puts "✅ Authentication successful! Token saved to config"

  client
rescue => e
  log_error("perform_auth_flow", e)
  raise "Authentication flow failed: #{e.message}"
end

#search_meetings(query, start_date: nil, end_date: nil, max_results: 10) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/utilities/gmeet/service.rb', line 112

def search_meetings(query, start_date: nil, end_date: nil, max_results: 10)
  # Default to today if no start date provided
  now = Time.now
  start_time = start_date ? Time.parse("#{start_date} 00:00:00") : Time.new(now.year, now.month, now.day, 0, 0, 0)
  # Default to 30 days from start if no end date provided
  end_time = end_date ? Time.parse("#{end_date} 23:59:59") : start_time + 30 * 24 * 60 * 60

  results = @service.list_events(
    "primary",
    q: query,
    max_results: max_results,
    single_events: true,
    order_by: "startTime",
    time_min: start_time.utc.iso8601,
    time_max: end_time.utc.iso8601
  )

  # Filter for events that have Google Meet links
  meetings = results.items.filter_map do |event|
    meet_link = extract_meet_link(event)
    next unless meet_link

    {
      id: event.id,
      summary: event.summary,
      description: event.description,
      location: event.location,
      start: format_event_time(event.start),
      end: format_event_time(event.end),
      attendees: format_attendees(event.attendees),
      html_link: event.html_link,
      meet_link: meet_link,
      calendar_id: "primary"
    }
  end

  {meetings: meetings, count: meetings.length}
rescue Google::Apis::Error => e
  raise "Google Calendar API Error: #{e.message}"
rescue => e
  log_error("search_meetings", e)
  raise e
end

#test_connectionObject



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/utilities/gmeet/service.rb', line 178

def test_connection
  calendar = @service.get_calendar("primary")
  {
    ok: true,
    user: calendar.summary,
    email: calendar.id
  }
rescue Google::Apis::Error => e
  raise "Google Calendar API Error: #{e.message}"
rescue => e
  log_error("test_connection", e)
  raise e
end

#upcoming_meetings(max_results: 10, calendar_id: "primary") ⇒ Object



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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/utilities/gmeet/service.rb', line 66

def upcoming_meetings(max_results: 10, calendar_id: "primary")
  # Get meetings starting from now
  start_time = Time.now
  # Look ahead 24 hours by default
  end_time = start_time + 24 * 60 * 60

  results = @service.list_events(
    calendar_id,
    max_results: max_results,
    single_events: true,
    order_by: "startTime",
    time_min: start_time.utc.iso8601,
    time_max: end_time.utc.iso8601
  )

  # Filter for events that have Google Meet links and are upcoming
  meetings = results.items.filter_map do |event|
    meet_link = extract_meet_link(event)
    next unless meet_link

    event_start_time = event.start.date_time&.to_time || (event.start.date ? Time.parse(event.start.date) : nil)
    next unless event_start_time && event_start_time >= start_time

    {
      id: event.id,
      summary: event.summary,
      description: event.description,
      location: event.location,
      start: format_event_time(event.start),
      end: format_event_time(event.end),
      attendees: format_attendees(event.attendees),
      html_link: event.html_link,
      meet_link: meet_link,
      calendar_id: calendar_id,
      time_until_start: time_until_event(event_start_time)
    }
  end

  {meetings: meetings, count: meetings.length}
rescue Google::Apis::Error => e
  raise "Google Calendar API Error: #{e.message}"
rescue => e
  log_error("upcoming_meetings", e)
  raise e
end