Class: FlycalCli::SlotFinder

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

Constant Summary collapse

DEFAULT_HOURS =
[[9, 0, 18, 0]].freeze
DEFAULT_DAYS =
[1, 2, 3, 4, 5].freeze
STEP_SECONDS =
900

Instance Method Summary collapse

Constructor Details

#initialize(events:, time_min:, time_max:, slot_duration_seconds:, hours: DEFAULT_HOURS, days: DEFAULT_DAYS, free_before_seconds: 0, free_after_seconds: 0) ⇒ SlotFinder

Returns a new instance of SlotFinder.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/flycal_cli/slot_finder.rb', line 9

def initialize(
  events:,
  time_min:,
  time_max:,
  slot_duration_seconds:,
  hours: DEFAULT_HOURS,
  days: DEFAULT_DAYS,
  free_before_seconds: 0,
  free_after_seconds: 0
)
  @events = events
  @time_min = time_min
  @time_max = time_max
  @slot_duration_seconds = slot_duration_seconds
  @hours = hours
  @days = Array(days).map(&:to_i)
  @free_before_seconds = free_before_seconds
  @free_after_seconds = free_after_seconds
end

Instance Method Details

#slots_by_dayObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/flycal_cli/slot_finder.rb', line 29

def slots_by_day
  result = {}

  each_template_day do |date|
    slots = []
    day_intervals(date).each do |day_start, day_end|
      next if day_end <= day_start

      busy = busy_intervals_for(date, day_start, day_end)
      gaps = free_gaps(day_start, day_end, busy)
      gaps.each do |gap_start, gap_end|
        slots.concat(slots_in_gap(gap_start, gap_end))
      end
    end
    result[date] = slots unless slots.empty?
  end

  result
end