Class: FlycalCli::CalendarService

Inherits:
Object
  • Object
show all
Defined in:
lib/flycal_cli/calendar_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(credentials) ⇒ CalendarService

Returns a new instance of CalendarService.



7
8
9
10
# File 'lib/flycal_cli/calendar_service.rb', line 7

def initialize(credentials)
  @service = Google::Apis::CalendarV3::CalendarService.new
  @service.authorization = credentials
end

Instance Method Details

#get_calendar(calendar_id) ⇒ Object



17
18
19
# File 'lib/flycal_cli/calendar_service.rb', line 17

def get_calendar(calendar_id)
  @service.get_calendar(calendar_id)
end

#list_all_events(calendar_ids, time_min:, time_max:, query: nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/flycal_cli/calendar_service.rb', line 45

def list_all_events(calendar_ids, time_min:, time_max:, query: nil)
  all_events = []

  # When --description is used: fetch all events and filter client-side to guarantee
  # contains/like behavior. We do not use the API's q param so we can ensure we
  # return all events where the string appears in summary or description (case-insensitive).
  calendar_ids.each do |cal_id|
    begin
      events = list_events(cal_id, time_min: time_min, time_max: time_max, query: nil)
      events.each { |e| all_events << { calendar_id: cal_id, event: e } }
    rescue Google::Apis::Errors::Error => e
      warn "Error in calendar #{cal_id}: #{e.message}"
    end
  end

  # Filter: only events where summary or description contains the search string (case-insensitive)
  if query && !query.to_s.strip.empty?
    q = query.strip.downcase
    all_events.select! do |item|
      event = item[:event]
      summary = (event.summary || "").downcase
      description = (event.description || "").downcase
      summary.include?(q) || description.include?(q)
    end
  end

  all_events
end

#list_calendarsObject



12
13
14
15
# File 'lib/flycal_cli/calendar_service.rb', line 12

def list_calendars
  result = @service.list_calendar_lists
  result.items || []
end

#list_events(calendar_id, time_min:, time_max:, query: nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/flycal_cli/calendar_service.rb', line 21

def list_events(calendar_id, time_min:, time_max:, query: nil)
  events = []
  page_token = nil

  loop do
    result = @service.list_events(
      calendar_id,
      time_min: time_min.iso8601,
      time_max: time_max.iso8601,
      q: query,
      single_events: true,
      order_by: "startTime",
      page_token: page_token
    )

    events.concat(result.items || [])

    page_token = result.next_page_token
    break if page_token.nil? || page_token.empty?
  end

  events
end