Class: Flycal::CalendarService

Inherits:
Object
  • Object
show all
Defined in:
lib/flycal/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/calendar_service.rb', line 7

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

Instance Method Details

#create_event(calendar_id: "primary", summary:, start_time:, end_time:, all_day: false) ⇒ Object



75
76
77
78
79
# File 'lib/flycal/calendar_service.rb', line 75

def create_event(calendar_id: "primary", summary:, start_time:, end_time:, all_day: false)
  event = Google::Apis::CalendarV3::Event.new(summary: summary)
  apply_event_times!(event, start_time: start_time, end_time: end_time, all_day: all_day)
  @service.insert_event(calendar_id, event)
end

#get_calendar(calendar_id) ⇒ Object



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

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

#get_event(calendar_id, event_id) ⇒ Object



21
22
23
# File 'lib/flycal/calendar_service.rb', line 21

def get_event(calendar_id, event_id)
  @service.get_event(calendar_id, event_id)
end

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



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

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::Error => e
      warn "Error in calendar #{cal_id}: #{e.message}"
    end
  end

  # Filter: summary/description contains any OR term from query ("a|b"), case-insensitive.
  if query && !query.to_s.strip.empty?
    all_events.select! do |item|
      event = item[:event]
      DescriptionQuery.match?(event.summary, event.description, query)
    end
  end

  all_events
end

#list_calendarsObject



12
13
14
15
# File 'lib/flycal/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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/flycal/calendar_service.rb', line 25

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: iso8601(time_min),
      time_max: iso8601(time_max),
      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

#update_event(calendar_id: "primary", event_id:, summary:, start_time:, end_time:, all_day: false) ⇒ Object



81
82
83
84
85
86
# File 'lib/flycal/calendar_service.rb', line 81

def update_event(calendar_id: "primary", event_id:, summary:, start_time:, end_time:, all_day: false)
  event = get_event(calendar_id, event_id)
  event.summary = summary if summary && !summary.to_s.empty?
  apply_event_times!(event, start_time: start_time, end_time: end_time, all_day: all_day)
  @service.update_event(calendar_id, event_id, event)
end