Module: Flycal::CalendarQuery

Defined in:
lib/flycal/calendar_query.rb

Overview

Parses and resolves calendar selection options for CLI and HTTP API.

Accepts:

- a single string: "Work"
- pipe/comma OR list: "Work|Personal" or "Work,Personal"
- an array (e.g. repeated --calendar flags): ["Work", "Personal"]
- mixed: ["Work|Gym", "Personal"]

Class Method Summary collapse

Class Method Details

.calendar_id(cal) ⇒ Object



46
47
48
49
50
51
# File 'lib/flycal/calendar_query.rb', line 46

def calendar_id(cal)
  return nil if cal.nil?
  return cal.id if cal.respond_to?(:id)

  cal[:id] || cal["id"]
end

.calendar_summary(cal) ⇒ Object



53
54
55
56
57
58
# File 'lib/flycal/calendar_query.rb', line 53

def calendar_summary(cal)
  return nil if cal.nil?
  return cal.summary if cal.respond_to?(:summary)

  cal[:summary] || cal["summary"] || cal[:name] || cal["name"]
end

.refs(value) ⇒ Object

Expand raw option value(s) into calendar reference strings.



15
16
17
18
19
20
# File 'lib/flycal/calendar_query.rb', line 15

def refs(value)
  Array(value).flat_map { |entry| entry.to_s.split(/[|,]/) }
              .map(&:strip)
              .reject(&:empty?)
              .uniq
end

.resolve_ids(calendars, value) ⇒ Object

Resolve refs to calendar IDs against a list of calendar objects (must respond to #id / #summary, or hash with those keys).



24
25
26
# File 'lib/flycal/calendar_query.rb', line 24

def resolve_ids(calendars, value)
  refs(value).filter_map { |ref| resolve_ref(calendars, ref) }.uniq
end

.resolve_ref(calendars, ref) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/flycal/calendar_query.rb', line 28

def resolve_ref(calendars, ref)
  ref = ref.to_s.strip
  return nil if ref.empty?

  exact = calendars.find { |cal| calendar_id(cal) == ref }
  return calendar_id(exact) if exact

  exact_name = calendars.find { |cal| calendar_summary(cal).to_s.casecmp?(ref) }
  return calendar_id(exact_name) if exact_name

  needle = ref.downcase
  partial = calendars.find do |cal|
    summary = calendar_summary(cal).to_s
    !summary.empty? && summary.downcase.include?(needle)
  end
  calendar_id(partial)
end